A True Canadian
Newbie
- Joined
- Aug 29, 2003
- Messages
- 2,874
- Reaction score
- 2
Hi, it's me again.
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:
The "Reverse" function is the one causing my problems.
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
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.
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