454,605 Members | 1,473 Online Need help? Post your question and get tips & solutions from a community of 454,605 IT Pros & Developers. It's quick & easy.

 P: 43 Im writing a program that accepts a sequence of integers (some of which may repeat) as input and prints every integer exactly once. You can assume that there are no more than 20 integers in the input. But there may be less. Zero signifies the end of input. It should NOT be printed. I have the first half of the program written which accept the input into the array but I can't figure out how to ouput it. I have one of the examples that I tried below but the output is all messed up...Any help would be greatly appreciated...Thanks #include using namespace std; int main() { // Declare array and number of elements in it. int array; int i = 0, length; // Instructing user to enter in a series of numbers' cout << "Please enter in a series of numbers ending with a 0." << endl; do { cin >> array[i]; i++; } while(array[i-1] != 0 && i < 20); if (array != 0) do { cout << array[i]; i++; } while (array[i] != 0 && i < 20); } I can't figure out what is wrong with this???? heres the output: Please enter in a series of numbers ending with a 0. 3 4 5 6 7 8 9 0 134515120134519976-107582498413451415397860401-1075824952134515146966080858900361345197245890036-bash-3.00\$ Nov 13 '06 #1
8 Replies

 P: 66 Im writing a program that accepts a sequence of integers (some of which may repeat) as input and prints every integer exactly once. You can assume that there are no more than 20 integers in the input. But there may be less. Zero signifies the end of input. It should NOT be printed. I have the first half of the program written which accept the input into the array but I can't figure out how to ouput it. I have one of the examples that I tried below but the output is all messed up...Any help would be greatly appreciated...Thanks #include using namespace std; int main() { // Declare array and number of elements in it. int array; int i = 0, length; // Instructing user to enter in a series of numbers' cout << "Please enter in a series of numbers ending with a 0." << endl; do { cin >> array[i]; i++; } while(array[i-1] != 0 && i < 20); if (array != 0) do { cout << array[i]; i++; } while (array[i] != 0 && i < 20); } I can't figure out what is wrong with this???? heres the output: Please enter in a series of numbers ending with a 0. 3 4 5 6 7 8 9 0 134515120134519976-107582498413451415397860401-1075824952134515146966080858900361345197245890036-bash-3.00\$ Hello, After the numbers are input, before printing the values, variable 'i' is not initailized to zero. Nov 13 '06 #2

 10K+ P: 13,264 Im writing a program that accepts a sequence of integers (some of which may repeat) as input and prints every integer exactly once. You can assume that there are no more than 20 integers in the input. But there may be less. Zero signifies the end of input. It should NOT be printed. I have the first half of the program written which accept the input into the array but I can't figure out how to ouput it. I have one of the examples that I tried below but the output is all messed up...Any help would be greatly appreciated...Thanks #include using namespace std; int main() { // Declare array and number of elements in it. int array; int i = 0, length; // Instructing user to enter in a series of numbers' cout << "Please enter in a series of numbers ending with a 0." << endl; do { cin >> array[i]; i++; } while(array[i-1] != 0 && i < 20); if (array != 0) do { cout << array[i]; i++; } while (array[i] != 0 && i < 20); } I can't figure out what is wrong with this???? heres the output: Please enter in a series of numbers ending with a 0. 3 4 5 6 7 8 9 0 134515120134519976-107582498413451415397860401-1075824952134515146966080858900361345197245890036-bash-3.00\$ Why don't try to write it this way first and see what you get cout << "Please enter in a series of numbers ending with a 0." << endl; int num = 0; int length = 0; int i = 0; cin>>num; while(num != 0) { array[i] = num; i++; length++; } i = 0; if (array != 0) do { cout << array[i]; i++; } while (i < length); } Nov 13 '06 #3

 10K+ P: 13,264 Hello, After the numbers are input, before printing the values, variable 'i' is not initailized to zero. Yes of course that's it. And so it prints the garbage values in the array from there onwards. I had missed that. Nov 13 '06 #4

 P: 43 Thank You So much...That was it!!! Nov 13 '06 #5

 P: 43 OK I got that figured out an compiled it and it works great but...The repeated numbers are still being output. Im trying to figure out how exactly to go about figuring out a way to ouput the numbers one time only.. Any ideas??? Im thinkin I'm going to have to out in another loop somewhere where the numbers are being input and have like the repeats stored in another array somehow? ex: usere enters : 3 5 6 7 8 3 5 9 output should be: 3 5 6 7 8 9 Nov 13 '06 #6

 Expert 2.5K+ P: 3,652 OK I got that figured out an compiled it and it works great but...The repeated numbers are still being output. Im trying to figure out how exactly to go about figuring out a way to ouput the numbers one time only.. Any ideas??? Im thinkin I'm going to have to out in another loop somewhere where the numbers are being input and have like the repeats stored in another array somehow? ex: usere enters : 3 5 6 7 8 3 5 9 output should be: 3 5 6 7 8 9 You could do this by having 2 arrays. Array1 will hold the user's input - 3 5 6 7 8 3 5 9, in your example. The second array will be the same size as array1. You will run a for loop to access each element of array1. If the value array1[i] is present in array2, don't add it - but if it isn't present, add that value to array2. This might be what your code will look like: Expand|Select|Wrap|Line Numbers int array1; int array2; int count = 0;   for (int i = 0; i < 10; i++) {    // Input into array1 here }   for (int i = 0; i < 10; i++) {    bool found = false;    for (int j = 0; j < count; j++) {       // If array2[j] is the same as array1[i], the value has been found.          // Set found to true.    }    // If the number has been found, continue    // If the number hasn't been found,       array2[count++] = array1[i]; // Increases count to reflect size of array2 AND next position }   // Output array2 here. Of course, you can combine the input and duplicate check in the same for loop, but I'll leave that up to you, Nov 13 '06 #7

 P: 43 You could do this by having 2 arrays. Array1 will hold the user's input - 3 5 6 7 8 3 5 9, in your example. The second array will be the same size as array1. You will run a for loop to access each element of array1. If the value array1[i] is present in array2, don't add it - but if it isn't present, add that value to array2. This might be what your code will look like: Expand|Select|Wrap|Line Numbers int array1; int array2; int count = 0;   for (int i = 0; i < 10; i++) {    // Input into array1 here }   for (int i = 0; i < 10; i++) {    bool found = false;    for (int j = 0; j < count; j++) {       // If array2[j] is the same as array1[i], the value has been found.          // Set found to true.    }    // If the number has been found, continue    // If the number hasn't been found,       array2[count++] = array1[i]; // Increases count to reflect size of array2 AND next position }   // Output array2 here. Of course, you can combine the input and duplicate check in the same for loop, but I'll leave that up to you, Ok I made all the proper changes and compiled it but Im getting a wierd output and I don't know why...I tried many different changes but nothing working..when i output array1 though it outputs correctly and outputs all the numbers entered by the user heres my output for array 2 tho Expand|Select|Wrap|Line Numbers Please enter in a series of numbers ending with a 0. 1 2 3 3 4 0  4714024 134519980  and heres the changes i made: Expand|Select|Wrap|Line Numbers int main() {     // Declare array and number of elements in it.                                       int array1;     int array2;     int j = 0;     int i = 0;     int count = 0;     // Instructing user to enter in a series of numbers'                                 cout << "Please enter in a series of numbers ending with a 0." << endl;     do     {         cin >> array1[i];         i++;     }     while (array1[i-1] != 0 && i < 20);     i = 0;     for (int i = 0; i < 20; i++)     {         bool found = false;         for (int j = 0; j < count; j++)         {             if (array2[j] = array1[i])             {                 bool found = true;             }             else             {                 array2[count++] = array1[i];             }         }     }     i = 0;     do     {         cout << array2[i] << " ";         i++;     }     while (array2[i] != 0 && i < 20);     cout << endl; }   Anyone's help would be greatly appreciated...I can't get the numbers to be output only one time! Nov 13 '06 #8

 P: 66 Ok I made all the proper changes and compiled it but Im getting a wierd output and I don't know why...I tried many different changes but nothing working..when i output array1 though it outputs correctly and outputs all the numbers entered by the user heres my output for array 2 tho Expand|Select|Wrap|Line Numbers Please enter in a series of numbers ending with a 0. 1 2 3 3 4 0  4714024 134519980  and heres the changes i made: Expand|Select|Wrap|Line Numbers int main() {     // Declare array and number of elements in it.                                       int array1;     int array2;     int j = 0;     int i = 0;     int count = 0;     // Instructing user to enter in a series of numbers'                                 cout << "Please enter in a series of numbers ending with a 0." << endl;     do     {         cin >> array1[i];         i++;     }     while (array1[i-1] != 0 && i < 20);     i = 0;     for (int i = 0; i < 20; i++)     {         bool found = false;         for (int j = 0; j < count; j++)         {             if (array2[j] = array1[i])             {                 bool found = true;             }             else             {                 array2[count++] = array1[i];             }         }     }     i = 0;     do     {         cout << array2[i] << " ";         i++;     }     while (array2[i] != 0 && i < 20);     cout << endl; }   Anyone's help would be greatly appreciated...I can't get the numbers to be output only one time! Well you can create another array of the same size as previous one(since it is possible that no number is repeated). Then, scan the first array element by element. If the element is not found in the second array, add to its end else simply go to next iteration. Expand|Select|Wrap|Line Numbers   int first_array; int second_array; int second_array_cntr=0; int found=0;   for(int i=0;i 