How to write a c++ code that takes 10 integers and type them without repetition ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to write a c++ code that takes 10 integers and type them without repetition ?

Thanks

2nd Dec 2016, 6:35 AM
ghasan
ghasan - avatar
11 Answers
+ 3
#include <iostream.h> void main(){ int nos[10], j=0, i=0; int repeat=-1; for(i=0; i<=10; i++){ // to input nos. in array cout<<"Enter the no."; cin>>nos[i] //nos are stored } cout<<"The nos you typed are"; for(i=0!; i<=10; i++){ //comparing no with old no. for(j=0; j<i; j++){ if( nos[I]==nos[j]) repeat=i; break; } //this will check its repeat or not if(i!=repeat) cout<<nos[i]; } } // it can be by flags too thanks to Abhishek for suggestions c++ has inbuilt feature to do this so px16 has the normal code
2nd Dec 2016, 7:51 AM
Sandeep Chatterjee
+ 2
#include <iostream> using namespace std; int main() { int arr[10]; bool flag = false; for(int i=0;i<10;i++) { cin >> arr[i] ; } for(int i=0;i<10;i++) { flag = false; for(int j=i-1;j>=0;j--) { if(arr[i] == arr[j]) { flag = true; break; } } if(!flag) cout << arr[i] << " "; } return 0; } Plz VOTE UP if this helped :)
2nd Dec 2016, 7:43 AM
Abhishek Kumar
Abhishek Kumar - avatar
+ 2
#include <iostream> using namespace std; #include <set> using std::set; int main() { set<int> nums; for (int i = 0; i < 10; i++){ int v; cin >> v; nums.insert(v); } for (auto v : nums) cout << v << endl;; return 0; } There is used std::set to make numbers unique. If you need to keep order, you may use set to verify if number was already provided and push back number to result std::vector or list. set<int> nums; vector<int> result; for (int i = 0; i < 10; i++){ int v; cin >> v; if (nums.find(v) == nums.end()) result.push_back(v); nums.insert(v); } for (auto v : result) cout << v << endl;;
2nd Dec 2016, 8:32 AM
px16
px16 - avatar
+ 2
Abhishek thanks for your suggestion the loop statement has been corrected it was to compare all no it's j is i-1 [6, 2, 6] the idea is , in loop it will check nos[2] is equal to nos[0] or not if yes it will mark this element as repeat now after check loop ends it will not print nos[2 ] like nos[0] and nos[1] as nos[2] is marked as repeat. the small loop should check all previous nos and report repetition.
2nd Dec 2016, 9:24 AM
Sandeep Chatterjee
+ 2
yes please suggest how to optimize
2nd Dec 2016, 9:39 AM
Sandeep Chatterjee
+ 1
i am not good in c++ but i recommend u to use int array, the length is 10 , then every time u need e for loop, in oder to check the value, because u do not wanna same value, so u need if, i hope , i can help u
2nd Dec 2016, 7:05 AM
Ozan Karataş
Ozan Karataş - avatar
+ 1
@sandeep chatterjee in your code at some point 'j' value will become same as 'i' so your below condition will always be true. if( nos[I]==nos[j]) repeat=i; which will set your repeat to i thus you will not get any output
2nd Dec 2016, 8:18 AM
Abhishek Kumar
Abhishek Kumar - avatar
+ 1
@sandeep chatterjee I hope you know if i==j then nos[i] will be equal to nos[j] since both i and j have same index value
2nd Dec 2016, 9:31 AM
Abhishek Kumar
Abhishek Kumar - avatar
+ 1
@sandeep chatterjee I see now you have changed condition in second loop condition now it is correct but you can still optimize it
2nd Dec 2016, 9:38 AM
Abhishek Kumar
Abhishek Kumar - avatar
+ 1
@sandeep chatterjee for(j=0; j<i; j++){ if( nos[I]==nos[j]){ repeat=i; break; } } break will stop the loop once it finds the number is a repeat
2nd Dec 2016, 9:43 AM
Abhishek Kumar
Abhishek Kumar - avatar
+ 1
good suggestion ok Abhishek that's good but I think it will work without it too rather it can be extended to count the missing no. if we add a counter that counts missing no. I mean there is no problem in small program to mark repeat again repeat should be reported at least one time. yes break or continue will give fast result but I have left this for counter
2nd Dec 2016, 9:47 AM
Sandeep Chatterjee