am i going crazy?

This is a discussion on am i going crazy? within the C++ Programming forums, part of the General Programming Boards category; I'm trying to take an array of n user defined values and write 2 functions...one that shows the value of ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    153

    am i going crazy?

    I'm trying to take an array of n user defined values and write 2 functions...one that shows the value of this array and one that reverses the order of the values of the array. HEre are my two functions:
    Code:
    void show_array(double ar[], int n)
    {
        for (int i = 0; i < n; i++)
        {
            cout << "Value #" << (i + 1) << "= ";
            cout << ar[i] << "\n";
        }
    }
    
    void reverse_array(double ar[], int n)
    {
        double temp;
        for (int i = 0; i < n; i++)
        {
            temp = ar[i];
            ar[i] = ar[i + (n- 1)];
            ar[i + (n - 1)] = temp;
        }
    }
    My problem is that let's say a 3 element array is given the following values: ar[0] = 3, ar[1] = 2, and ar[2] = 1, the show_array function displays the following values: 1 (which is right), 8.59976e-298 (very wrong), and1.39068e-309 (even more wrong)...any idea why this isn't doing what I'd like it to...? Thanks -Chap

  2. #2
    Registered User
    Join Date
    Oct 2004
    Posts
    120
    The problem is in your reverse_array function (which you must be calling before show_array, because thats the only way I could duplicate what your seeing).

    The first iteration through the loop is good, because i = 0 and n-1 = 2, so you swap indexes 0 & 2. But the next two iterations are not going to work...let me know if you see the problem.

    PK

  3. #3
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Code:
    void reverse_array(double ar[], int n)
    {
        double temp;
    	int i;
        for ( i = 0; i < (n-1)/2; i++)
        {
            temp = ar[i];
          
    		ar[i]=ar[n-i-1];
    		ar[n-i-1]=temp;
        }
    }

  4. #4
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Code:
    void reverse_array(double ar[], int n)
    {
        double temp;
    	int i;
        for ( i = 0; i < (n-1)/2; i++)
        {
            temp = ar[i];
          
    		ar[i]=ar[n-i-1];
    		ar[n-i-1]=temp;
        }
    }

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > ar[i] = ar[i + (n- 1)];
    > ar[i + (n - 1)] = temp;

    Both of these should be n-1-i (or -i+n-1):
    Code:
            ar[i] = ar[n - 1 - i];
            ar[n - 1 - i] = temp;

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by Chaplin27
    I'm trying to take an array of n user defined values and write 2 functions...one that shows the value of this array and one that reverses the order of the values of the array. HEre are my two functions:

    ...

    My problem is that let's say a 3 element array is given the following values: ar[0] = 3, ar[1] = 2, and ar[2] = 1, the show_array function displays the following values: 1 (which is right), 8.59976e-298 (very wrong), and1.39068e-309 (even more wrong)...any idea why this isn't doing what I'd like it to...? Thanks -Chap

    Well, Micko has given you a solution, including a bug fix on the limits of the loop in reverse_array().

    For future problems, here's a way to find out for yourself what's going on.

    Go back to your original program. Now, show_array() seems pretty straightforward, and I'm guessing that if you called show_array() with your initial array values it gave you what you expected.

    If it seems that reverse_array() is not working right, then put some output statements to see what it is trying to do.

    Something like:

    Code:
    void reverse_array(double ar[], int n)
    {
        double temp;
        for (int i = 0; i < n; i++)
        {
            cout << "i = " << i << ", i+(n-1) = " << i+(n-1) << endl;
            temp = ar[i];
            ar[i] = ar[i + (n- 1)];
            ar[i + (n - 1)] = temp;
        }
    }
    Seeing that the value of the second expression gives an out-of-range index value, you can go back to pencil-and-paper to see if you can discover what it should be. Then fix the code accordingly.

    After you fix the array index expression, the output statement will also give you a hint as to why the loop limit should be changed.

    cout<< is a very powerful debugging tool, and (I claim) faster than posting a request for help and waiting for a helpful response.


    Regards,

    Dave
    Last edited by Dave Evans; 10-05-2004 at 09:53 AM.

  7. #7
    Registered User
    Join Date
    Oct 2004
    Posts
    120
    I was really hoping no one would post the solution, as it seemed that Chaplin simply needed a nudge in the right place to bug hunt.

  8. #8
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Well, I personaly prefer to post code when one who ask also post code. My experience is that it is much better to understand mistakes when examin right code

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is AOL music crazy?
    By joeprogrammer in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 03-24-2006, 06:24 PM
  2. restart drives me crazy
    By Jumper in forum A Brief History of Cprogramming.com
    Replies: 20
    Last Post: 03-17-2004, 11:52 AM
  3. crazy
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 08-31-2002, 08:54 PM
  4. crazy output
    By asirep in forum C Programming
    Replies: 22
    Last Post: 04-09-2002, 11:41 AM
  5. Replies: 1
    Last Post: 02-24-2002, 05:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21