Author Topic: For the life of me, I cant make this counter work.  (Read 1240 times)

Bazoukas

  • Member
  • **
  • Posts: 866
  • Kudos: 140
    • http://whitehouse.com
For the life of me, I cant make this counter work.
« 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 ]

Yeah

flap

  • Member
  • **
  • Posts: 1,268
  • Kudos: 137
For the life of me, I cant make this counter work.
« Reply #1 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.
"While envisaging the destruction of imperialism, it is necessary to identify its head, which is none other than the United States of America." - Ernesto Che Guevara

http://counterpunch.org
http://globalresearch.ca


jtpenrod

  • VIP
  • Member
  • ***
  • Posts: 675
  • Kudos: 105
For the life of me, I cant make this counter work.
« Reply #2 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    
______________________________________

Computers are like air conditioners: they can't do their jobs if you open windows.

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

Live Free or Die: Linux
If software can be free, why can't dolphins?