Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 2nd, 2007, 7:25 PM   #1
357mag
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 148
Rep Power: 4 357mag is on a distinguished road
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 is offline   Reply With Quote
Old Jul 2nd, 2007, 7:57 PM   #2
357mag
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 148
Rep Power: 4 357mag is on a distinguished road
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.
357mag is offline   Reply With Quote
Old Jul 2nd, 2007, 8:12 PM   #3
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Jul 2nd, 2007, 8:51 PM   #4
357mag
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 148
Rep Power: 4 357mag is on a distinguished road
Well it appears to work fine with my compiler. I'm using Microsoft Visual Studio 2005.
357mag is offline   Reply With Quote
Old Jul 2nd, 2007, 9:46 PM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Jul 3rd, 2007, 8:42 AM   #6
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,010
Rep Power: 5 lectricpharaoh will become famous soon enough
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.
__________________
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
lectricpharaoh is offline   Reply With Quote
Old Jul 4th, 2007, 7:18 AM   #7
pegasus001
Hobbyist Programmer
 
pegasus001's Avatar
 
Join Date: Nov 2006
Location: 163H
Posts: 213
Rep Power: 2 pegasus001 is on a distinguished road
Quote:
Originally Posted by 357mag View Post
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.
__________________
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.
pegasus001 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:44 PM.

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