counting a few numbers and storing the values

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2007
Posts: 22
Reputation: johnnyjohn20 is an unknown quantity at this point 
Solved Threads: 0
johnnyjohn20 johnnyjohn20 is offline Offline
Newbie Poster

counting a few numbers and storing the values

 
0
  #1
Nov 22nd, 2007
Hello, i am trying to count from 0 - 7 and storing the values in an array.

I have been trying to do this solidly for 2 weeks

my attempt:

**************************************
int a[20];
int x;

for ( x=0 ; x<=7 ; x++ ) {

a[1] = x;
cout << x << endl; }

***************************************

I know that the output from this program is correct, i.e. it displays:
0
1
2
.
.
7

my problem is a[1] will only display the value inside the for loop's {}. I want to use an array element, a[1] outside of the for { } with those values stored in there. Please help!

Thanks,

John
Last edited by johnnyjohn20; Nov 22nd, 2007 at 3:59 pm. Reason: inserted wrong code
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: counting a few numbers and storing the values

 
0
  #2
Nov 22nd, 2007
> a[1] = x;
Perhaps
a[x] = x;
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 8
Reputation: jansson is an unknown quantity at this point 
Solved Threads: 2
jansson jansson is offline Offline
Newbie Poster

Re: counting a few numbers and storing the values

 
0
  #3
Nov 22nd, 2007
Hello johnnyjohn20.

As salem told you, exchanging a[1] whit 'a[x]' should solve your problems.
In your code you constantly over-wrights position 1 in the array, and leaving all the others untouched. If you try to display a[1] after the loop have finished, it will display '7'.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 22
Reputation: johnnyjohn20 is an unknown quantity at this point 
Solved Threads: 0
johnnyjohn20 johnnyjohn20 is offline Offline
Newbie Poster

Re: counting a few numbers and storing the values

 
1
  #4
Nov 22nd, 2007
Thank you for your help, so far i have adjusted the code and can now display the values outside of the { }, using
cout << a[1] << endl;
cout << a[2] << endl; ...etc etc. Is there a way i can display all of the values in the array using one simple command?.

Thanks for your time reading my posts!
Last edited by johnnyjohn20; Nov 22nd, 2007 at 4:47 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: counting a few numbers and storing the values

 
0
  #5
Nov 22nd, 2007
Use the same kind of for loop you used to fill the array perhaps?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC