Reverse Arrays?

Joined
Aug 29, 2003
Messages
2,874
Reaction score
2
Hi, it's me again. :p

Once again I have gotten stuck in my "C\C++ Whatever" stuff. This time, I can't quite figure out how to get a reverse array to work. The program reads the inputs from the user and then prints out the values on the screen. That part I've gotten, it's just printing them in reverse order is where the difficulty lies.

The user is asked to put in at most 10 values, and then I want to print the values in the reverse order that they typed them in. (i.e. 12, 45, -3, 45 would be 45, -3, 45, 12).

Here is what I have so far:

Code:
#include <stdio.h>
void Fill(int B[], int &num);
 void Dump(int C[], int num);
 void Reverse(int D[], int num);
 main()
	{
	int Array[10], n=0;
	Fill(Array, n);
	Dump(Array, n);
	Reverse(Array, n);
	}
 void Fill(int B[], int &num)
	{
	int i;
	printf("Enter the number of elements => "); 
	scanf("%d", &num);
	if (10 < num || num < 0)
		{	printf("Invalid\n");
			Fill(B, num);	}
	else
		{	printf("Enter the elements\n");
			for(i = 0; i < num; ++i)
				scanf("%d", &B[i]);	}
	}
 void Dump(int C[], int num)
	{
	int i;
	printf("The Elements in the array are:\n");
	for(i = 0; i < num; ++i)
		printf("\t%d\n", C[i]);
	}
 void Reverse(int D[], int num)
	{
	int i;
	printf("The Elements in the Array (in reverse order) are:\n");
	for(i = num-1; i >= num; ++i);
		printf("\t%d\n", D[i]);
	}

The "Reverse" function is the one causing my problems. :p
I left the "Dump" function in there for a reference as well. So far, the program runs fine, except it prints out only one of the many numbers. (i.e. if 23 was the last number of a series of numbers entered by the user, only 23 would appear in the "reverse" function). I think a normal for loop can get the job done, but you never know.

As usual it's probably something simple. Thanks in advance for any help. I think I'm finally getting the hang of this programming stuff. :E
 
The problem is is that you are incorrectly using the for loop. There should not be a ; after the for loop statement.. such as you have it:

Code:
for(i = num-1; i >= num; ++i);

The correct syntax is:

Code:
for( /* Setup */)
	{
		// Code to do
	}

And you also have a problem with the way your code inside the for loop is..

I'll give you a hint if you want to do it on your own... You must reverse your for loop setup (int i should decreae, not increase)

Or if you really just want to know.. here is the code in a spoiler wrapping....

for(i = num-1; i >= 0; --i)
{
printf("\t%d\n", D);
}


Also another tip.. You can declare the int i inside the for loop if you wanted to (I prefer it becuase I think it makes my code look neater, but you can do what you want)
Code:
for(int i = 0; i < 10; i++)

And it is also a good habbit to get into to setting all declarations equal to 0 when you declare them. (int i = 0) (Again, not forcing you, just trying to help)

Sorry if you knew those 2 things already.

Hope your having fun!
-Limb
 
Limb said:
The problem is is that you are incorrectly using the for loop. There should not be a ; after the for loop statement.. such as you have it:

Code:
for(i = num-1; i >= num; ++i);

The correct syntax is:

Code:
for( /* Setup */)
	{
		// Code to do
	}

He did it the correct way. If you leave the ; out it will only loop with the next line of code. It goes the same for while / do / if statments too.. example..

Code:
  if (1 != 2)
    printf("Yeh !!");

is shorthand for...

Code:
  if (1 != 2) {
    printf("Yeh !!");
  }

and

Code:
for( i = num-1; i >= num; ++i)
  printf("Yeh !!");

is shorthand for
Code:
for( i = num-1; i >= num; ++i) {
  printf("Yeh !!");
}
 
Thanks for the replies.

The semi-colon was a typo, and thanks for pointing that out. The curly braces ( { & } ) are optional in this case, but yes, it does make the code neater and easier to read.

As for the reverse array. I tried everything to get it to work, except setting the second condition >= 0 while the counter was --i; or something along those lines. :)

Thanks again for the in-depth responses, and yes I am having fun with this.
 
Sorry about that Looks like I learned something new from this :LOL:

-Limb
 
Hmmm. How would I go about printing the sum and the average?

I'm sure the function would be similar to the others:

void Compute(int E[], int num);

How would I go about this? Would another loop suffice? I know I'll need more variables other than just i, but how would you take the existing information and be able to do something with it? Parameters maybe?

EDIT:

Code:
 void Compute(int E[], int num)
	{
	int i, sum;
	sum = 0;
	printf("The Sum of the Elements are:\n");
	for(i = 0; i < num; ++i)
		sum = sum + E[i];
		printf("\t%d\n", E[i]);
	}

How's that? The problem is it's printing jibberish.
 
You need to print out the sum after the loop is finished. Same for the avg but you must do one step first(which I'm sure you know).

It seems like you need to review what exactly a 'for loop' does. It looks like you have the code, but not the understanding.

Not trying to be rude.
 
In a for loop, I believe the first section lists the statements, or conditions to start, to initialize the loop. The second tells the loop when to exit, and the third is simply a counter.

i = 0 sets i at 0 to start the loop.
i < num signifies that when i is less than the number of elements (at most 10), the loops stops.
++i tallies all the numbers.

correct?
 
pretty close, except for a few technicals.

The second part is a test, if it fails, the loop doesn't execute. The third part is just an action taken on the variable after every loop execution, but before the test. You can increment, decrement, multiply, a lot of things.

The reason I said it doesn't seem like you quite understand, is you calculated the sum, but you didn't print it or anything, but you printed out your values in the array.


here's some FYI:

You don't need all the section either.
ie for(; i < 10;) is a while loop.

You can have multiple part to each section.
ie for(i=0, j=10; i != j; i++, j--)
 
Yeah...oops.

You have no idea how silly I feel once I realized that it should have been the "sum" and not the "E" in the printf statement. :eek:
 
Back
Top