can someone review my code? c++

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

xMikex

Thread Starter
Joined
Nov 7, 2005
Messages
159
Here it is , I need to make sure im using pointers correctly because this is a school project and Im supposed to make the reverseArray function have "pointer notation" so can anyone see if I did that correctly?

#include <iostream>

using namespace std;

int* reverseArray(int *newN);

int counter;
int num[5];

int main()
{

cout << " Enter 5 Numbers: " << endl;

for (counter = 0; counter < 5;counter++)
{
cin >> num[counter];
}

cout << " The Numbers in reverse: " ;
reverseArray(num);

return 0;

}
int* reverseArray(int *newN)
{
int counter2 = 0;


for (counter2 = 4; counter2 >= 0;counter2--)
{
cout << newN[counter2] << " ";
}
cout << endl;
return 0;

}

Thanks!
 

xMikex

Thread Starter
Joined
Nov 7, 2005
Messages
159
also one thing to chang at the end of the fucntion its return *newN; not return 0;
 
Joined
Apr 30, 2001
Messages
2,636
You're not really using pointer notation.

I would picture it like this:

Code:
#include <iostream>
using namespace std;

int main() {
    int num[5];
    int* first = &num[0];
    int* second = &num[1];
    int* third = &num[2];
    int* fourth = &num[3];
    int* fifth = &num[4];
    
    *first = 1;
    *second = 2;
    *third = 3;
    *fourth = 4;
    *fifth = 5;
    
    cout << *fifth << "\n";
    cout << *fourth << "\n";
    cout << *third << "\n";
    cout << *second << "\n";
    cout << *first << "\n";
}
Or even

Code:
#include <iostream>
using namespace std;

int main() {
    int num[5];
    *num = 1;
    *(num + 1) = 2;
    *(num + 2) = 3;
    *(num + 3) = 4;
    *(num + 4) = 5;
    cout << *(num + 4) << "\n";
    cout << *(num + 3) << "\n";
    cout << *(num + 2) << "\n";
    cout << *(num + 1) << "\n";
    cout << *num << "\n";
}
To do what you want, set a pointer to the beginning of the array and in a loop, cin into it and increment the pointer. Just break before incrementing past the last element.

Then, use another pointer and a loop in your reverse function to print them out in reverse. Just set a pointer to the last element, print it out and decrement it till you print the first element. Just break before decrementing to before the first element.
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Members online

Top