Stop Microsoft

Miscellaneous => Programming & Networking => Topic started by: Bazoukas on 28 September 2002, 22:00

Title: For the life of me, I cant make this counter work.
Post by: Bazoukas on 28 September 2002, 22:00
the program asks you to input 10 integers in the array and then it adds them up. So far so good.
 my problem is that i cant make it to show the numbers entered in a reverse order.


int main() {
    int size;
   size=9;
   int array[size];
   int total;
   total=0;

   cout<<"Enter 10 numbers"<<endl;
   
   for (int num1=0; num1<size; num1++)
{
      
      cin>>array[size];
       
      total=total+array[size];
      
      }
      cout<<total<<endl;
   
for (int num1=size; num1>=0; num1--)
//also did this: for (int size=9; size>=0; size--)
//but same problem.         
         cout<< array[size];
      
      
      return 0;
   
}

  Here is the output.
1
2
3
4
5
6
7
8
9
45
9-107374330410741437901073819680110854222010739665841345136181345192161073787046-1073743328


  It can read the last number entered but after that it hits the wall.
 My assumption is that it reads the last number because of the num1=size but after why it stops?
  are the values from the array are being wiped out?

[ September 28, 2002: Message edited by: bazoukas ]

Title: For the life of me, I cant make this counter work.
Post by: flap on 29 September 2002, 21:20
this

for (int num1=size; num1>=0; num1--)

should be

for (int num1=size - 1; num1>=0; num1--)

If the array has 9 elements then it's indexed from 0 - 8. Although I don't know why you're setting the size to 9 if you want them to input 10 integers.
Title: For the life of me, I cant make this counter work.
Post by: jtpenrod on 7 October 2002, 08:29
To bazoukas:

Try this instead:
Code: [Select]

See if that works, and good luck. And remember to watch those array indices; don't let a run-away pointer bite you on the ass       ;)     :D    
______________________________________
(http://www.otakupc.com/etsig/dolphin.gif)
Computers are like air conditioners: they can't do their jobs if you open windows.

[ October 06, 2002: Message edited by: jtpenrod ]