By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,605 Members | 1,473 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,605 IT Pros & Developers. It's quick & easy.

Question about arrays

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 <iostream>
using namespace std;
int main()
{
// Declare array and number of elements in it.
int array[20];
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] != 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
Share this Question
Share on Google+
8 Replies


vpawizard
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 <iostream>
using namespace std;
int main()
{
// Declare array and number of elements in it.
int array[20];
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] != 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 <iostream>
using namespace std;
int main()
{
// Declare array and number of elements in it.
int array[20];
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] != 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] != 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

Ganon11
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
  1. int array1[10];
  2. int array2[10];
  3. int count = 0;
  4.  
  5. for (int i = 0; i < 10; i++) {
  6.    // Input into array1 here
  7. }
  8.  
  9. for (int i = 0; i < 10; i++) {
  10.    bool found = false;
  11.    for (int j = 0; j < count; j++) {
  12.       // If array2[j] is the same as array1[i], the value has been found.
  13.          // Set found to true.
  14.    }
  15.    // If the number has been found, continue
  16.    // If the number hasn't been found,
  17.       array2[count++] = array1[i]; // Increases count to reflect size of array2 AND next position
  18. }
  19.  
  20. // 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
  1. int array1[10];
  2. int array2[10];
  3. int count = 0;
  4.  
  5. for (int i = 0; i < 10; i++) {
  6.    // Input into array1 here
  7. }
  8.  
  9. for (int i = 0; i < 10; i++) {
  10.    bool found = false;
  11.    for (int j = 0; j < count; j++) {
  12.       // If array2[j] is the same as array1[i], the value has been found.
  13.          // Set found to true.
  14.    }
  15.    // If the number has been found, continue
  16.    // If the number hasn't been found,
  17.       array2[count++] = array1[i]; // Increases count to reflect size of array2 AND next position
  18. }
  19.  
  20. // 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
  1. Please enter in a series of numbers ending with a 0.
  2. 1 2 3 3 4 0 
  3. 4714024 134519980 
and heres the changes i made:
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.     // Declare array and number of elements in it.                                  
  4.     int array1[20];
  5.     int array2[20];
  6.     int j = 0;
  7.     int i = 0;
  8.     int count = 0;
  9.     // Instructing user to enter in a series of numbers'                            
  10.     cout << "Please enter in a series of numbers ending with a 0." << endl;
  11.     do
  12.     {
  13.         cin >> array1[i];
  14.         i++;
  15.     }
  16.     while (array1[i-1] != 0 && i < 20);
  17.     i = 0;
  18.     for (int i = 0; i < 20; i++)
  19.     {
  20.         bool found = false;
  21.         for (int j = 0; j < count; j++)
  22.         {
  23.             if (array2[j] = array1[i])
  24.             {
  25.                 bool found = true;
  26.             }
  27.             else
  28.             {
  29.                 array2[count++] = array1[i];
  30.             }
  31.         }
  32.     }
  33.     i = 0;
  34.     do
  35.     {
  36.         cout << array2[i] << " ";
  37.         i++;
  38.     }
  39.     while (array2[i] != 0 && i < 20);
  40.     cout << endl;
  41. }
  42.  
Anyone's help would be greatly appreciated...I can't get the numbers to be output only one time!
Nov 13 '06 #8

vpawizard
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
  1. Please enter in a series of numbers ending with a 0.
  2. 1 2 3 3 4 0 
  3. 4714024 134519980 
and heres the changes i made:
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.     // Declare array and number of elements in it.                                  
  4.     int array1[20];
  5.     int array2[20];
  6.     int j = 0;
  7.     int i = 0;
  8.     int count = 0;
  9.     // Instructing user to enter in a series of numbers'                            
  10.     cout << "Please enter in a series of numbers ending with a 0." << endl;
  11.     do
  12.     {
  13.         cin >> array1[i];
  14.         i++;
  15.     }
  16.     while (array1[i-1] != 0 && i < 20);
  17.     i = 0;
  18.     for (int i = 0; i < 20; i++)
  19.     {
  20.         bool found = false;
  21.         for (int j = 0; j < count; j++)
  22.         {
  23.             if (array2[j] = array1[i])
  24.             {
  25.                 bool found = true;
  26.             }
  27.             else
  28.             {
  29.                 array2[count++] = array1[i];
  30.             }
  31.         }
  32.     }
  33.     i = 0;
  34.     do
  35.     {
  36.         cout << array2[i] << " ";
  37.         i++;
  38.     }
  39.     while (array2[i] != 0 && i < 20);
  40.     cout << endl;
  41. }
  42.  
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
  1.  
  2. int first_array[100];
  3. int second_array[100];
  4. int second_array_cntr=0;
  5. int found=0;
  6.  
  7. for(int i=0;i<no_of_elements;i++)
  8. {
  9.      found=0;
  10.      for(int j=0;j<second_array_cntr;j++)
  11.            if(second_array[j]==first_array[i]){found=1;break;}
  12.  
  13.      if(found==0)second_array[j]=first_array[i];
  14. }
  15.  
  16.  
Nov 14 '06 #9

Post your reply

Sign in to post your reply or Sign up for a free account.