Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 06-02-2005, 01:10 PM   #1
Daemon Poster
 
Starr's Avatar
 
Join Date: Apr 2005
Posts: 926
Send a message via AIM to Starr Send a message via MSN to Starr
Default Dynamic Memory Allocation, Arrays, Pointers

Ok I have a question. I am using dynamic memory allocation to create an array thats size is determined by the user. Now, after the user designates the size of the array I want to add all of the values in the and divide by the size of the array. (the program is determinging the averages of grades).

Ok, what I tried is, I tried to create a for loop that said:

Code:
for (int i = 0; i < numTests; i++)
		sum += iPtr[i];
'i' is the array variable for the pointer and 'numTests' is the size of the array that the user determines.

The closest I got was I got one term of the array divided by the size of the array determined by the user.

I am new to programming so cut me some slack.

The language should be obvious, if it's not then you wouldn't know the answer.

If you want I can post the entire code.
__________________

__________________
AMD Athlon 64 3200+ Venice Core, MSI K8N Neo-4, 2x 512 Corsair Dual Channel, Nvidia Geforce fx 6600 256MB, 160GB HardDrive

Windows Vista Business Edition

"There are 10 types of people in the world. Those who know binary and those who don't."
Starr is offline   Reply With Quote
Old 06-02-2005, 01:14 PM   #2
Baseband Member
 
Knight's Avatar
 
Join Date: May 2005
Posts: 77
Default

Could you post your code? It'll make it easier to help you

**Is iPtr the name of the array you are using?**
__________________

Knight is offline   Reply With Quote
Old 06-02-2005, 01:29 PM   #3
Baseband Member
 
Join Date: Sep 2004
Posts: 95
Default Re: Dynamic Memory Allocation, Arrays, Pointers

Quote:
Originally Posted by Starr
Code:
for (int i = 0; i < numTests; i++)
		sum += iPtr[i];
'i' is the array variable for the pointer and 'numTests' is the size of the array that the user determines.
Don't use a pointer, just use the array name, whatever that is.
__________________
As the sun sets, the earth shall prepare
ksb007 is offline   Reply With Quote
Old 06-02-2005, 02:25 PM   #4
Daemon Poster
 
Starr's Avatar
 
Join Date: Apr 2005
Posts: 926
Send a message via AIM to Starr Send a message via MSN to Starr
Default Re: Dynamic Memory Allocation, Arrays, Pointers

Yes, iPtr is the name of the pointer.

Code:
#include <iostream>
using namespace std; 
int main()
{
	int numTests;
	float sum, ans;
	cout << "Enter the number of grades that you need to find the average of: ";
	cin >> numTests;
	int * iPtr = new int[numTests];
	for (int i = 0; i < numTests; i++)
	{
		cout << "Enter test score #" << i + 1 << " : "; 
		cin >> iPtr[i]; 
	}
	for (int i = 0; i < numTests; i++)
	{
		sum = sum + iPtr[i];  
	}
	cout << "The sum of the test scores is: " << sum << endl;
	for (int i = 0; i < numTests; i++)
		ans = sum / numTests;
	cout << "The average of all of the grades is: " << ans << endl;
	delete [] iPtr;
	return 0;
}
There it is with the natest stuff I tried.
__________________
AMD Athlon 64 3200+ Venice Core, MSI K8N Neo-4, 2x 512 Corsair Dual Channel, Nvidia Geforce fx 6600 256MB, 160GB HardDrive

Windows Vista Business Edition

"There are 10 types of people in the world. Those who know binary and those who don't."
Starr is offline   Reply With Quote
Old 06-02-2005, 03:09 PM   #5
Baseband Member
 
Knight's Avatar
 
Join Date: May 2005
Posts: 77
Default

From the initial code snippet you supplied, it looked like Java to me..oh well. Why are you making the following for loop?
Code:
for (int i = 0; i < numTests; i++)
           ans = sum / numTests;
If you want the average, try the following:
Code:
ans = sum / numTests;
* I also forget, do variables have to be given a value (i.e 0 or null) before they can be used in C++? *
Knight is offline   Reply With Quote
Old 06-02-2005, 06:28 PM   #6
Daemon Poster
 
Starr's Avatar
 
Join Date: Apr 2005
Posts: 926
Send a message via AIM to Starr Send a message via MSN to Starr
Default Re: Dynamic Memory Allocation, Arrays, Pointers

No, you don't have to assign them anything. You just have to declare them.
__________________
AMD Athlon 64 3200+ Venice Core, MSI K8N Neo-4, 2x 512 Corsair Dual Channel, Nvidia Geforce fx 6600 256MB, 160GB HardDrive

Windows Vista Business Edition

"There are 10 types of people in the world. Those who know binary and those who don't."
Starr is offline   Reply With Quote
Old 06-02-2005, 07:40 PM   #7
Baseband Member
 
Knight's Avatar
 
Join Date: May 2005
Posts: 77
Default

Ah ok, thank you Starr. Did my previous post help you with your issue?
__________________
while(mySalary<yourSalary){
mySalary += moreMoney;
}
mySalary += (moreMoney*moreMoney);
System.out.println("Now there's a salary: $" + mySalary);
Knight is offline   Reply With Quote
Old 06-02-2005, 08:35 PM   #8
Daemon Poster
 
Starr's Avatar
 
Join Date: Apr 2005
Posts: 926
Send a message via AIM to Starr Send a message via MSN to Starr
Default Re: Dynamic Memory Allocation, Arrays, Pointers

Kind of. But I figured it OUT!!!!!! You question helped me. About do you need to assign the variable to anything. Well in this case sum needed to be assigned to something because it is being used in a math problem. So it needed to be assigned to 0. So I thank you knight for asking your question.
__________________

__________________
AMD Athlon 64 3200+ Venice Core, MSI K8N Neo-4, 2x 512 Corsair Dual Channel, Nvidia Geforce fx 6600 256MB, 160GB HardDrive

Windows Vista Business Edition

"There are 10 types of people in the world. Those who know binary and those who don't."
Starr is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 04:25 PM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0