Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Getting the wrong answer (http://www.programmingforums.org/showthread.php?t=13462)

357mag Jul 2nd, 2007 8:25 PM

Getting the wrong answer
 
I've got this program that uses the new operator with an array. I'm studying pointers and I read that you can use the new operator with an array and a pointer if you don't know how big the array is going to be. So I wrote this program that asks the user how many elements he would like in his array. Then he inputs that. Next, it asks him to enter the values for the elements. Then it inputs that. Finally, I want the program to simply output the sum of the elements. But I'm getting the wrong answer for the sum. I'm just kind of doing this by myself, my books don't show examples similar to this so here is my program:

:

int main()
{
        int max = 0;
        int sum = 0;
        int count = 0;
       
        cout << "Enter the number of elements you would like: ";
        cin >> max;

        int *pn = new int[max];

        do
        {
                cout << "Enter a value: ";
                cin >> *pn;
                count++;
        }while (count < max);

        for (int i = 0; i < max; i++)
                sum += *(pn + i);

        cout << "The sum is " << sum << endl;

        return 0;
}


357mag Jul 2nd, 2007 8:57 PM

Well I re-wrote the program and now it looks like I'm getting the correct answer:

:

for (int i = 0; i < max; i++)
        {
                cout << "Enter a value: ";
                cin >> pn[i];
        }

        for (int i = 0; i < max; i++)
                sum += pn[i];

        cout << "\nThe sum is " << sum << endl;


But I do have a question. Why do I have to write sum += pn[i]? I would think
that I need to tell the compiler to get at the value that is in the element in order to add it to the sum. So I would think I would have to write
sum += *pn[i]. But of course the compiler complains if I do that. Seems to
me that pn[i] would simply produce the addresses of the integers.

DaWei Jul 2nd, 2007 9:12 PM

Declaring an array with a length that is determined at runtime is not supported by many compilers. You might want to refer to the applicable standards, as well as the compliance of various compilers to those standards.

357mag Jul 2nd, 2007 9:51 PM

Well it appears to work fine with my compiler. I'm using Microsoft Visual Studio 2005.

DaWei Jul 2nd, 2007 10:46 PM

You also need to understand the reactions of the compiler to your syntax. pn [i] is a reference to a specific element of pn. Dereferencing that as *pn[i] will tell the compiler that pn[i] is a pointer. That might be in line with what pn actually holds in its elements, and it might not.

The standard requires that the name of an array be convertible to a pointer to that array. They are still not the same thing. If you examine the emitted machine code, you will see that there is difference of a level of indirection.

The language does us many 'favors' in order to ease our task, but those favors often introduce some ambiguities. You might want to have a peep at the pointer tutorial referenced in my signature.

lectricpharaoh Jul 3rd, 2007 9:42 AM

Quote:

Originally Posted by 357mag
But I do have a question. Why do I have to write sum += pn[i]? I would think
that I need to tell the compiler to get at the value that is in the element in order to add it to the sum. So I would think I would have to write
sum += *pn[i]. But of course the compiler complains if I do that. Seems to
me that pn[i] would simply produce the addresses of the integers.

With C and C++, you can use subscript notation on both actual arrays, and pointers. When you have an array, the array name by itself (ie, with no subscript) evaluates to the address of the first element. For example:
:

int array[5];
int *ptr = array;

See how I assign the array to the pointer? Obviously, this won't copy the contents of the array; instead, it copies the array's address. Likewise, I can use array notation- that is, subscripting- with the pointer:
:

array[3] = 817;
std::cout << ptr[3];  // prints '817'

In a similar manner, you can use pointer notation with arrays:
:

*(array+3) = 123;
std::cout << *(ptr+3);  // prints '123'

In essence, a subscript is a shorthand for pointer notation:
:

std::cout << ptr[4];
std::cout << *(ptr+4);  // semantically the same as the above line
std::cout << *(4+ptr);  // same again, since addition is commutative
std::cout << 4[ptr];  // looks funny, but it's the same too

Now, if you stick in your asterisk along with the subscript, as you suggest, you are attempting to use a pointer to int as a pointer to pointer to int:
:

*ptr[x] = 5;
*(*(ptr + x)) = 5;  // same as above, but makes the error more obvious

Hopefully, this will clear up the pointer issues. I also recommend reading the pointer tutorial in DaWei's signature.

pegasus001 Jul 4th, 2007 8:18 AM

Quote:

Originally Posted by 357mag (Post 129926)
I'm studying pointers and ...

Take a look at DaWei`s article on pointers in his signature its really good and then you have some tutorials in the tutorials section of the forum. :)


All times are GMT -5. The time now is 2:38 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC