Using a For loop and pointer to reverse output

I'm having trouble understand how to reverse my output using the For loop. At first I thought I could just replace the ++ with --, but that obviously didn't work. I'm thinking I need to change something with my x integer equation but can't figure it out. Any help is greatly appreciated

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include "stdafx.h"
#include <iostream>
using namespace std;

void dispArr(int * val, int size);

void main()
{
	int arrA[5] = {2, 4, 6, 8, 10};	//array defined
	dispArr(arrA, 5),			

	system("PAUSE");
	return;

}
 void dispArr(int *val, int size)
 {
	 int *ptrA= new int[5];
	 for (int x = 0;x < size;x++)
	 {
		 *(ptrA + x) = *(val + x);
	 }
	for (int x = 0;x < size;x++)
	{
		cout << x << " = " << +(ptrA + x) << endl;
	}
	
	delete [] ptrA;
	system("PAUSE");
	return;
}
Last edited on
First, a syntactic note:
1
2
3
4
for (int x = 0;x < size;x++)
  {
    *(ptrA + x) = *(val + x);
  }

is here same as:
1
2
3
4
for (int x = 0;x < size;x++)
  {
    ptrA[x] = val[x];
  }

The problem with this is that you copy first element to first position.
For reversal, it should go to last position.

When x==0, you want to write to prtA[size-1].
When x==size-1, you want to write to ptrA[0].
1
2
3
4
5
for ( int x = 0; x < size; ++x )
  {
    const int y = func(x);
    ptrA[y] = val[x];
  }

Now, invent such expression func(x) that you get what you need.
There are also functions built into c++ to get the size of the array, but you could write a loop to find the size for you as well.

The sizeof operator works pretty well but you have to be careful with it.

Unless each element is of the same length in characters you could run into problems.

Here would be a couple of examples to maybe give you an idea.

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

int main(){

    char letters[] = {'a', 'b', 'c', 'd', 'e'};

    for (int i = sizeof(letters); i > 0; i--){
        std::cout << letters[i-1] << " ";
    }
    return 0;
}


1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <string>

int main(){

    std::string letters[] = {"ab", "cd", "ef", "gh", "ij"};

    for (int i = (sizeof(letters))/(sizeof(letters[0])); i > 0; i--){
        std::cout << letters[i-1] << " ";
    }
    return 0;
}


(sizeof(letters))/(sizeof(letters[0]) is basically the size of the entire array divided by the size of an individual array element.

This tells you how many elements are in the array because sizeof doesn't distinguish different array elements for you, it just returns the size of the entire array even if individual elements contain different amounts of characters.
Last edited on
Well, somehow, I came up with this and it works, however it goes through it 5 times instead of just displaying it the first way then the reverse way.

#include "stdafx.h"
#include <iostream>
using namespace std;

void dispArr(int * val, int size);

void main()
{
int arrA[5] = { 2, 4, 6, 8, 10 }; //array defined
dispArr(arrA, 5),

system("PAUSE");
return;

}
void dispArr(int *val, int size)
{
int *ptrA = new int[5];
for (int x = 0; x < size; x++)
{
*(ptrA + x) = *(val + x);
}
for (int x = 0; x < size; x++)
{
cout << x << " = " << +(ptrA + x) << endl;
}

for (int x = 0; x < size; x++)
{
*(ptrA + x) = *(val + x);
for (int x = 0; x < size; x++)
cout << x << " = " << +(ptrA + x - 4) << endl;
}

delete[] ptrA;
system("PAUSE");
return;
Last edited on
Topic archived. No new replies allowed.