Reverse array and pointers?

Ok! This is the assignment I was given this week. I know hold on its a long one... lol. I don't think he was very descriptive and I've asked him to please be a little more descriptive and 3 days later no response, anyways here it is: (Does my code look right cause it works the way I believe it is supposed to.)

Write a C++ function ReverseArray using "pointer notation" that will write out the elements of an array of int in reverse order.

Here is my code:

#include <iostream>

using namespace std;

void reverseArray();

int main()
{
      cout << "Enter five integers: ";
      reverseArray();

      return 0;
}
void reverseArray()
{
      int numbers[5];
      int* p_numbers = &numbers[5];
      int counter;
            for (counter = 0; counter < 5; counter++)
      {
            cin >> numbers[counter];
            cout << "Enter five integers: ";
      }
      cout << endl;

      cout << "The numbers in reverse are: ";

      for (counter = 4; counter >= 0; counter--)
            cout << numbers[counter] << " ";
      cout << endl;
}
jschmuffAsked:
Who is Participating?
 
SeanDurkinCommented:
Take a look at this webpage, it demonstrates pointer notation:

http://co-5.college-online.com/joan_albright/CPT_235_files/Pointer_Notat_vs_Array_Notat.htm

What you want to do is set int *p_numbers equal to the beginning of your array, and then print them out using p_numbers, instead of accessing the numbers array directly.
0
 
jschmuffAuthor Commented:
Thank you very much I will give it a shot tomorrow, as for now it is bed time for me. I believe I understand what you are saying. I will post tomorrow my new code and see if I understood correctly. Thank you again.
0
 
SeanDurkinCommented:
No prob... and as a side note, your professor may want you to put the values in the array with pointer notation as well. That won't be too hard to implement, either.. you just dereference p_numbers again using counter, rather than accessing the numbers array directly.
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
abithCommented:
int *start = &numbers[0];
int *end = start + (length-1);

for ( ; start<=end; start++, end--)
{
interchange the values of *start and *end
}
0
 
Infinity08Commented:
>> void reverseArray()

I think the point of the exercise is to make the function work for any array, meaning that you have to pass the array as a parameter to the function which will then show it in reverse.

So, the input part (cin etc.) would move to the main, and only the reverse printing is done by the function.
0
 
yuy2002Commented:
#include <iostream>
using namespace std;

template<class T,int size>
class Array
{
public:
    T elem[size];
    Array();
    ~Array();
    void reverseArray();
    void print();
};
template<class T,int size>
Array<T,size>::Array()
{
}

template<class T,int size>
Array<T,size>::~Array()
{
}

template<class T,int size>
void Array<T,size>::reverseArray()
{
    if(elem == NULL)
        return;
    int i=0;
    T temp[size];
    while(i<size)
    {
        temp[size-1-i] = elem[i];
        i++;
    }
    memcpy(elem,temp,size*sizeof(T));
    return;
}

template<class T,int size>
void Array<T,size>::print()
{
     int i=0;
     for(i=0;i<size;i++)
     {
         cout<<elem[i]<<endl;
     }
}
int main()
{
    Array<int,10> array;
    int i =0 ;
    for(i=0;i<10;i++)
    {
        array.elem[i] = i;
    }
    cout<<"*******Origin Array*********"<<endl;
    array.print();
    array.reverseArray();
    cout<<"*******Reverse Array*********"<<endl;
    array.print();
    return 0;
}
0
 
Infinity08Commented:
@yuy2002 : two things :

1) the asker clearly stated that this is a homework assignment - full code should thus not be posted according to the rules of this site.

2) KIS (keep it simple) : there is no need to use templates here ... the assignment is for an array of ints only
0
 
evilrixSenior Software Engineer (Avast)Commented:
Some observations about the posted solution...

>> using namespace std;
This should be discouraged as it can lead to problems with 3rd party code that may include older, non-namespaced, versions of the STL headers!

>> Array();
>> ~Array();
The compiler will auto-generate these!

>> memcpy(elem,temp,size*sizeof(T));
If elem (array of type T) is not a POD type this copy is dangerous!

>> void Array<T,size>::reverseArray()
There are smarter ways to do this. For example, you could start from each end and swap the elements until you get to the middle. You only need parse 50% off the array and you only need temporary storage that is the size of 1 element and the algorithm is simple!

>> public:    T elem[size];
Public data is a encapsulation sin!

>> if(elem == NULL)
>>        return;
Since elem is a fixed size stack array how could this predicate ever be true?

>> int i =0 ; ... array.elem[i] = i;
When indexing memory you should use size_t otherwise your code will not be portable.

IMHO this is not a good example and, as Infinity08 said, shouldn't have been posted!

-Rx.



0
 
itsmeandnobodyelseCommented:
>>>> IMHO this is not a good example and, as Infinity08 said, shouldn't have been posted!
The question is whether it should be 'upvalued' then by commenting each statement?

>>>> I think the point of the exercise is to make the
>>>> function work for any array, meaning that you
>>>> have to pass the array as a parameter to the
>>>> function which will then show it in reverse.

In case Infinity's suggestion 'got lost' because of the previous 'off topic' comments, I post it here again.

The consequence of the suggestion (enhanced by me) is that you should

- create the array in the main function (not in reverseArray)
- have a separate function for user input, e. g.
      bool getUserInput(int * numbers, int arrsiz);
- the reverseArray should take an int * (pointer notation) and
  should reverse the values of the array (not print them).
- finally have a 'display' function which outputs the reversed array.

You should pass the size of the array (== 5) to all functions, so that you easily can change it.

Regards, Alex
 
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> should be 'upvalued' then by commenting each statement?
Qualification of assertion!
0
 
jschmuffAuthor Commented:
So basically this assignment can be interpreted to code in several different ways? If so im going for simplest cause if I go ahead to stuff we haven't learned then that doesn't look good plus he will grade badly for that as well.
0
 
jschmuffAuthor Commented:
Here is my new code and the output is way off what am I doing wrong??

#include <iostream>

using namespace std;

void arrayRev();

const int SIZE = 5;

int main()
{
      int numbers[SIZE];
      int* p_numbers;
      int counter;
      p_numbers = numbers;

      cout << "Enter five integers: ";

      for (counter = 0; counter < SIZE; counter++)
      {
            cin >> numbers[counter];
            cout << "Enter five integers: ";
      }
      
      cout << endl;

      arrayRev();

      return 0;
}
void arrayRev()
{
      int numbers[SIZE];
      int* p_numbers;
      int counter;
      p_numbers = numbers;

      cout << "The numbers in reverse are: ";

      for (counter = 4; counter >= 0; counter--)
            cout << *(p_numbers+counter) << " ";
      cout << endl;
}
0
 
SeanDurkinCommented:
You're creating a local numbers array within the arrayRev() function, but it is completely different from the one in main(). All you have to do is pass the original numbers array into arrayRev() like this:

void arrayRev(int numbers[]);

void arrayRev(int numbers[])
{
      int* p_numbers;
      int counter;
      p_numbers = numbers;

      cout << "The numbers in reverse are: ";

      for (counter = 4; counter >= 0; counter--)
            cout << *(p_numbers+counter) << " ";
      cout << endl;
}

And then inside main() you just pass it with arrayRev(numbers); But keep in mind that the numbers array inside arrayRev is just a copy of main()'s arrayRev. Even though they have the same name (which you shouldn't do anyway because it makes it harder to read), they have different scopes, so they don't interfere with each other.
0
 
yuy2002Commented:
Hi,evilrix

3x for your good suggestion.
>>There are smarter ways to do this. For example, you could start from each end and swap the >>elements until you get to the middle. You only need parse 50% off the array and you only need >>temporary storage that is the size of 1 element and the algorithm is simple!
0
 
Infinity08Commented:
I don't think you need to actually reverse the array ... just print it in reverse :

>> Write a C++ function ReverseArray using "pointer notation" that will write out the elements of an array of int in reverse order.


Your latest code looks fine except that, as SeanDurkin already pointed out, you're still not passing the array as a parameter to the function (see also my first comment in this thread).
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.