0

how could i display the reverse the values of the array that are random numbers using pointers.
So far this is what I have. it displays the array but it does not display the reverse.

#include "stdafx.h"
#include <iostream> 
#include <ctime> 
#include <cstdlib> 
const size = 10;
typedef int* IntPtr;

void input_array(int a[], int size);

void reverse(int a[], int size);

int index;

using namespace std; 

int main() 
{ int a[size];

input_array(a, size);

cout << endl;

reverse (a, size);

 

return 0;
} 

void input_array(int a[], int size)
{

srand((unsigned)time(0)); 
int random_integer; 
int lowest, highest; 


cout << "enter the range of the values lowest first then the highest\n";
cin >> lowest >> highest;

cout << "The random array with a size " << size << endl;
cout << endl;

int range=(highest-lowest)+1;

 
for( index=0; index < size; index++)
{ 
random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0)); 
 a[index] = random_integer;

cout << a[index] << " ";
}
 

} 

void reverse(int a[], int size)
{
IntPtr p1;

p1 = a;

cout << "The reverse of the array is\n";
cout << endl; 

for (index = 9; index<0; index--)
{
	cout << p1[index] << endl;
}
}
2
Contributors
2
Replies
3
Views
11 Years
Discussion Span
Last Post by fidelljf
0

Shouldnt

for (index = 9; index<0; index--)

be

for (index = 9; index>=0; index--)

. And since you are passing the size of the array use that instead of the hard coding 9. Like this

for (index = size; index>=0; index--)
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.