for loop help

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

Join Date: Oct 2004
Posts: 13
Reputation: bryan7890 is an unknown quantity at this point 
Solved Threads: 0
bryan7890 bryan7890 is offline Offline
Newbie Poster

for loop help

 
0
  #1
Oct 26th, 2004
i wrote in my prog

#include <iostream>
using namespace std;
int main ( )
{

int arraysize = 25;
int a [arraysize] = { 2, 3, 5, 19, 4, 20, 35, 123, -3, 45, 82, 93, 62, 35, 29, 21, 19, 57, 35, 64, 62, 324, 242, 0, -3 }


wut kinda for loop do i need to print out 2, 3, 5, 19, 4, 20, 35, 123, -3, 45, 82, 93, 62, 35, 29, 21, 19, 57, 35, 64, 62, 324, 242, 0, -3 these numbers in reverse order..i used array for this programme.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 185
Reputation: Stack Overflow is an unknown quantity at this point 
Solved Threads: 4
Stack Overflow's Avatar
Stack Overflow Stack Overflow is offline Offline
C Programmer

Re: for loop help

 
0
  #2
Oct 26th, 2004
Greetings bryan7890,

Do you want to print those numbers in reverse order as in starting at the last array and read until the first, or are you trying to sort the array to ascend or descend accordingly?


- Stack Overflow
Following the rules will ensure you get a prompt answer to your question. If posting code, please include BB [code][/code] tags. Your question may have been asked before, try the search facility.

IRC
Channel: irc.daniweb.com
Room: #c, #shell
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 13
Reputation: bryan7890 is an unknown quantity at this point 
Solved Threads: 0
bryan7890 bryan7890 is offline Offline
Newbie Poster

Re: for loop help

 
0
  #3
Oct 27th, 2004
this is C++ question

Write a complete program that staticaly declares an array with the following values:

2, 3, 5, 19, 4, 20, 35, 123, -3, 45, 82, 93, 62, 35, 29, 21, 19, 57, 35, 64, 62, 324, 242, 0, -3

and manipulates the resulting array so that the values in the array and reversed.
Print out the new array, which should look as follows:

-3, 0, 242, 324, 62, 64, 35, 57, 19, 21, 29, 35, 62, 93, 82, 45, -3, 123, 35, 20, 4, 19, 5, 3, 2

Note:
- you must use for-loop structure in order to complete your manipulations
- you may use only one (1) array - cannot create second array
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 13
Reputation: bryan7890 is an unknown quantity at this point 
Solved Threads: 0
bryan7890 bryan7890 is offline Offline
Newbie Poster

Re: for loop help

 
0
  #4
Oct 27th, 2004
if i had to declare numbers, we lets say 1 through 25 (not inclusing 1 and including 25) this how would i do it.
int[25];
for (int k= 1; k<=25;++k){
a[k] = kj
}

but this thing is really confusing, because the numbers are not in pattern.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 22
Reputation: kakilang is an unknown quantity at this point 
Solved Threads: 0
kakilang's Avatar
kakilang kakilang is offline Offline
Newbie Poster

Re: for loop help

 
0
  #5
Oct 27th, 2004
how about

for(int i=24;i>=0;i--)
{
cout<<a[i]<<" "<<endl;
}
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 13
Reputation: bryan7890 is an unknown quantity at this point 
Solved Threads: 0
bryan7890 bryan7890 is offline Offline
Newbie Poster

Re: for loop help

 
0
  #6
Oct 27th, 2004
#include <iostream>

using namespace std;

int main()
{

int a [] = { 2, 3, 5, 19, 4, 20, 35, 123, -3, 45, 82, 93, 62, 35, 29, 21, 19, 57, 35, 64, 62, 324\
, 242, 0, -3 };

int size = sizeof(a);

cout << size << endl << endl;

for(int i = 0; i <= size; i++)
{
cout << a[i] << " ";
}

}

y i m geeting this kinda output..

2 3 5 19 4 20 35 123 -3 45 82 93 62 35 29 21 19 57 35 64 62 324 242 0 -3 -4260908 5 -4260800 0 0 1 -4260916 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 -4260680 0 -4260674 -4260650 -4260637 -4260623 -4260604 -4260597 -4260428 -4260392 -4260353 -4260340 -4260326 -4260302 -4260289 -4260255 -4260238 -4260230 -4260177 -4260161 -4260152 -4260124 -4260099 -4260088 -4260079 -4259905 -4259898 -4259878 0 2008 -4259868 2014 -4259850 3 65588 4 32 5 5 9 67004 7 -12910592 8 2816 6

and numbers are not in reverse order..
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 185
Reputation: Stack Overflow is an unknown quantity at this point 
Solved Threads: 4
Stack Overflow's Avatar
Stack Overflow Stack Overflow is offline Offline
C Programmer

Re: for loop help

 
0
  #7
Oct 27th, 2004
Well,

kakilang's for loop looked like:
for(int i = 24; i >= 0; i--)
While yours looks like
for(int i = 0; i <= size; i++)
The difference is that you start at 0 [the beginning] and looping until the end. kakilang's example starts at the end, and loop until the beginning decrementing instead of incrementing.

Also, C provides a compile-time unary operator called sizeof that can be used to compute the size of any object. Here, we are not looking to find the size of the object, our goal is to know the size of our array. By simply creating the array with a fixed number of 25 may help. For example:
#define SIZE 25

int main() {
	int i, a[SIZE] = { 2, 3, 5, 19, 4, 20, 35, 123, -3, 45, 82, 93, 62, 35, 29, 21, 19, 57, 35, 64, 62, 324, 242, 0, -3 };

	// Start at end; decrement to beginning
	for (i = SIZE-1; i >= 0; i--)
		...
Where the ... means to keep going. This was just an example, and hopefully it helps.

If you have further questions, please feel free to ask.


- Stack Overflow
Following the rules will ensure you get a prompt answer to your question. If posting code, please include BB [code][/code] tags. Your question may have been asked before, try the search facility.

IRC
Channel: irc.daniweb.com
Room: #c, #shell
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 13
Reputation: bryan7890 is an unknown quantity at this point 
Solved Threads: 0
bryan7890 bryan7890 is offline Offline
Newbie Poster

Re: for loop help

 
0
  #8
Oct 27th, 2004
thanx everyone who helped me apriciated.. i finally got it to work..thanxx
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC