944,098 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3294
  • C++ RSS
Oct 26th, 2004
0

for loop help

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bryan7890 is offline Offline
13 posts
since Oct 2004
Oct 26th, 2004
0

Re: for loop help

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
Reputation Points: 26
Solved Threads: 4
Junior Poster
Stack Overflow is offline Offline
185 posts
since Sep 2004
Oct 27th, 2004
0

Re: for loop help

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bryan7890 is offline Offline
13 posts
since Oct 2004
Oct 27th, 2004
0

Re: for loop help

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bryan7890 is offline Offline
13 posts
since Oct 2004
Oct 27th, 2004
0

Re: for loop help

how about

for(int i=24;i>=0;i--)
{
cout<<a[i]<<" "<<endl;
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kakilang is offline Offline
22 posts
since Oct 2004
Oct 27th, 2004
0

Re: for loop help

#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..
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bryan7890 is offline Offline
13 posts
since Oct 2004
Oct 27th, 2004
0

Re: for loop help

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
Reputation Points: 26
Solved Threads: 4
Junior Poster
Stack Overflow is offline Offline
185 posts
since Sep 2004
Oct 27th, 2004
0

Re: for loop help

thanx everyone who helped me apriciated.. i finally got it to work..thanxx
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bryan7890 is offline Offline
13 posts
since Oct 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: problem creating a for loop for an array
Next Thread in C++ Forum Timeline: Im new





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC