# Thread: array program

1. ## array program

i am trying to do a program where with an array you write 20 numbers for the array and then you print all the numbers, but not the duplicated ones here's my code

float numbers[20], final[21];
int i,j;
for(i=0; i<20; i++){
cout <<"Enter a number: "<<flush;
cin >>numbers[i];}

for(j=0; j<20; j++){
if(numbers[j]=numbers[i+1]){
cout<<numbers[i];}}

what is wrong with it?

2. > if(numbers[j]=numbers[i+1]){
3 things wrong with it
1. You're using = inside an if statement, where it is more usual to use a comparison like ==
2. i doesn't change inside this loop
3. i has the value 20, so it's off the end of the numbers array, and adding 1 to it just makes it worse

Were you trying to copy the array to final by any chance?

3. i had final in the beginnig and i made just in case i needed it
could you help with it because it has been bugging me for a while

4. syntax wise the coditional statement of your if statement is wrong. A single = sign is the assignment operator. Dual = signs is the logical equals operator.

concept wise what if numbers[0] and numbers[10] are the same. Your code won't pick it up. I suggest sorting the numbers before running a display algorhythm. In the second for loop i + 1 never changes, j + 1 probably a little better idea, unless you were trying to compare each number in numbers[] to everyother number in numbers. The code for that approach requires a nested loop algorhythm similar to sorting. Personally, I like the sorting idea better, but it's up to you. You can search the board for a bubble sort algorhythm. It's a pretty common question.

I would then likely display the last of a series of equal values by saying if numbers[i] != numbers[i + 1] display numbers[i], but that necessitates special handling of the last item in the array (sense there is no "next" one), and being sure you don't overread the end of the array. How you do it is up to you however.

The array final[] is never used. This would likely generate a warning, but not an error.

5. thanx that is a lot of help but i am still having a little problem i'm trying to get it to show the non repeating numbers, but it is not doing it
here's the code:
const int size=20;
int numbers[size];
int i,j;
for(i=0; i<size; i++){
cout <<"Enter a number: "<<flush;
cin >>numbers[i];}
cout<<"Here are all of the numbers in the array: ";
for(i=0; i<size; i++){
cout<<numbers[i]<<" ";}
for (int pass=0; pass<size; pass++){
for(i=0;i<size-1;i++){
if(numbers[i]>numbers[i+1]){
j=numbers[i];
numbers[i]=numbers[i+1];
numbers[i+1]=j;
}}}
cout <<"\nHere are the non repeating numbers: ";
for(i=0; i<size; i++){
numbers[i];
for(j=numbers[i]; j<size; j++){
if(numbers[i]!=numbers[i+1]){
cout<<numbers[i]<<" ";}
}}

return 0;

6. assuming numbers is an array of int and sorted lowest to highest
this loop will print the last in a series of equal numbers in the array and any unique numbers in the array except the last element of the array.

for(i = 0; i < size - 1; i++)
{
if(numbers[i] != numbers[i + 1])
{
cout << numbers[i]
}
}

note: so as not to overread the array the loop stops when i is the next to last index of the array, not the last index of the array. That means we haven't evaluated the last index. However since the last index MUST be the last in a series of equal numbers, even if it is a unique number, in a sorted array, then you just have to print it out without worrying about it.

cout << numbers[size - 1];

or since the loop stopped when i == size - 1 you could also use;

//cout << numbers[i];

walk through these sequences to be sure the algorhythm works:

0 0 1 1 1 1 3 5 6 7 7 8 8 8 8 8
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1

Popular pages Recent additions