![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Mar 2005
Posts: 148
Rep Power: 4
![]() |
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;
} |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Mar 2005
Posts: 148
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#3 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Mar 2005
Posts: 148
Rep Power: 4
![]() |
Well it appears to work fine with my compiler. I'm using Microsoft Visual Studio 2005.
|
|
|
|
|
|
#5 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#6 | |
|
Caffeinated Neural Net
![]() Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,010
Rep Power: 5
![]() |
Quote:
int array[5]; int *ptr = array; array[3] = 817; std::cout << ptr[3]; // prints '817' *(array+3) = 123; std::cout << *(ptr+3); // prints '123' 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 *ptr[x] = 5; *(*(ptr + x)) = 5; // same as above, but makes the error more obvious
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot. - Vaarsuvius, Order of the Stick |
|
|
|
|
|
|
#7 |
|
Hobbyist Programmer
Join Date: Nov 2006
Location: 163H
Posts: 213
Rep Power: 2
![]() |
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.
![]()
__________________
You never test the depth of a river with both feet. The believer is happy. The doubter is wise. Free speech carries with it some freedom to listen. The next generation will always surpass the previous one. It`s one of the never ending cycles of life. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Code compiled without error, but gets the wrong answer; Calculating Volume | Fall Back Son | C | 15 | Oct 21st, 2006 6:51 PM |
| Wrong answer | Edgar | Assembly | 6 | Jul 12th, 2006 7:17 PM |
| [Python] Simple Comment Stripper | UnKnown X | Show Off Your Open Source Projects | 0 | Feb 10th, 2006 7:57 AM |
| First Python Programme: Fibonacci Finder | UnKnown X | Python | 2 | Dec 15th, 2005 6:19 PM |
| What is wrong with this code? | c0ldshadow | Visual Basic .NET | 5 | Dec 5th, 2005 5:37 PM |