+ 1

How to write a c++ code that takes 10 intigers and type them without replication ?


1st Dec 2016, 8:53 PM
ghasan - avatar
3 Answers
+ 2
Insert numbers into std::set<int>. set<int> nums; for (int i = 0; i < 10; i++){ int v; cin >> v; nums.insert(v); } for (auto v : nums) cout << v << endl;;
2nd Dec 2016, 9:22 AM
px16 - avatar
Not sure what you mean by "without replication"; But here's what I understood http://pastebin.com/hpdK7cv9
1st Dec 2016, 9:13 PM
Nedim Kanat
Nedim Kanat - avatar
First initialize an array with a size of 10. Write a loop ( i would prefer FOR for this purpose) which reads an int and inserts it to the arrays current index you iterate with your loop. After everything was read, you create another FOR loop and nest another FOR loop in it, The outer FOR loop iterates over th element to compare. The inner FOR loop compares the element of the outer index with all following elements. For this the outer For-loop starts at an Index i of 0 and goes to 8, The inner for loop starts at an index j of i+1. We now come to our comparison. There are two cases: - First is that array[i] is not equal to array[j] then do nothing - array[i] is equal to array[j] then you set array[j] to -1 (-1 is only possible for the case all other integers are positive values or cannot be -1). But now we will get a problem when your array[i] is -1. That is why we nest the comparison into an IF-statement to check whether array[i] is -1. If it is we break, if its not, we compare. Output: Write a for loop which iterates from 0 to 9. Create an if-statement to check whether the element at your current position (counted by the FOR-loop) is -1 or not, If it's not, use cout to print else you do nothing
1st Dec 2016, 9:15 PM
Andreas K
Andreas K - avatar