1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

can someone review my code? c++

Discussion in 'Software Development' started by xMikex, Jun 29, 2007.

Thread Status:
Not open for further replies.
  1. xMikex

    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!
     
  2. xMikex

    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;
     
  3. Shadow2531

    Shadow2531

    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.
     
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 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/590013

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice