GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 15-Sep-2006, 20:18
alex18 alex18 is offline
New Member
 
Join Date: Sep 2006
Posts: 14
alex18 has a little shameless behaviour in the past

C++ Help with math


Hi everyone, I am new to this site, and very new to C++.
I graduated high school in 1980 and have not had math since and am now faced with an equation I do not understand. When I was in school, the highest math we took was algebra.
I would like to write this C++ program, or at least understand it a little better.

Please see the attachment.

Here is what the problem states:
Write an interactive C++ program that computes and outputs the mean and the standard deviation of a set of four integer values that are input by the user. The mean is the sum of the four values divided by 4, and the formula for the standard deviation is in the attachment. Where n = 4, x refers to each of the four values, and the x with the line overit is the mean.

If anyone can tell me what the aquation in the attachment means, I'd appreciate it. I do not understand how it is written. I have not seen this style especially the i's on a lower plane.

Thank you
Alex
Attached Images
File Type: jpg Math.jpg (6.2 KB, 46 views)
  #2  
Old 15-Sep-2006, 23:48
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,720
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: C++ Help with math


Quote:
Originally Posted by alex18
Please see the attachment.

Your attachment shows a formula written with a summation symbol. Once you know the name of the operation, you can find explanations in lots of places. For example: http://en.wikipedia.org/wiki/Summation. But it's kind of tough looking up something when you don't know what it is called. See Footnote.

It's really easy to make a program loop out of the summation:

In words, for the part of the problem that is the summation, you might say something like, "Let i go from 1 up to and including n: form the sum of terms (x[i]- xbar) squared." Where it means that i is an integer and i = 1, 2, ..., n. I have used C-language array notation for the subscript, and I used xbar as a name for the symbol that is x with a bar over it.

Some people might read it aloud as, "The sum as i goes from 1 to n of the square of the quantity x sub i minus xbar."

Does that help?

Regards,

Dave

Footnote: Well, 1980 sounds like a pretty long time ago to some people, I guess (where does the time go?), but the use of the greek capital Sigma as a summation symbol dates back to 1755, I think, when Leonhard Euler introduced it in his work, Institutiones calculi differentialis. So it wasn't really that new, even when old-timers like you were kids.
Last edited by davekw7x : 16-Sep-2006 at 00:36.
  #3  
Old 16-Sep-2006, 18:52
alex18 alex18 is offline
New Member
 
Join Date: Sep 2006
Posts: 14
alex18 has a little shameless behaviour in the past

Re: C++ Help with math


Thank you

Yes, thank you, this does help me get started.
I may post my program once I get it written, maybe you can tell me if I am on the right track.

Thanks again.
Alex
  #4  
Old 20-Sep-2006, 11:11
alex18 alex18 is offline
New Member
 
Join Date: Sep 2006
Posts: 14
alex18 has a little shameless behaviour in the past

Re: C++ Help with math


OK, I have started to code this assignment.
I know that I am all over th eplace with this as I have received several errors.

So far I am attemting to compute the mean of 4 numbers. (4 numbers added together and then divided the results by 4).

I also need to code the deviation statement next. I have written the code for each number because I have to write the deviation code for each individual number as well.

I thought I would include this in the cout and cin statements for each number, for example "The mean of the 4 numbers are, and the deviation of numbers are: firstNumber = (), secondNumber = (), and so on.

Can someone please take a look at this and help me with the errors that I am getting and let me know if I am on track?

Thanks All
Alex

CPP / C++ / C Code:
// This program computes the mean and deviation of 4 values entered by a user

#include <iostream>		// Access iostream header file
#include <iomanip>		// Access iomanip header file
#include <cmath>		// Access cmath header file
#include <string>		// Access string header file
#include <cfloat>		// Access float header file

using namespace std;

int main()
(
	// Variables
	int firstNumber;
	int secondNumber;
	int thirdNumber;
	int forthNumber;
	float mean;
	float deviationSummation;

	cout << fixed						// Set up fixed value
		<< setprecision(2);				// Output format


	cout <<"Enter the first whole number: " << endl;		// Prompt the user for the first number
	cin >> firstNumber;

	cout << "Enter the second whole number: " << endl;	// Prompt the user for the second number
	cin >> secondNumber;

	cout << "Enter the third whole number: " << endl;		// Prompt the user for the thirdt number
	cin >> thirdNumber;

	cout << "Enter the final whole number: " << endl;		// Prompt the user for the final number
	cin >> forthNumber;

	double mean = abs(firstNumber, secondNumber, thirdNumber, forthNumber);

	double mean = (firstNumber + secondNumber + thirdNumber + forthNumber)/4		// Calculate the mean of the 4 numbers

	cout << "The mean of the 4 numbers entered are" << fixed << setprecision(2)<< mean << endl;

	cin.get() ;
	cin.get() ;
	
	return 0;

	)
Last edited by admin : 21-Sep-2006 at 09:36. Reason: Please insert your C code between [cpp] & [/cpp] tags
  #5  
Old 20-Sep-2006, 12:29
dabigmooish's Avatar
dabigmooish dabigmooish is offline
Member
 
Join Date: May 2004
Location: Baltimore (middle of Canton)
Posts: 165
dabigmooish will become famous soon enough

Re: C++ Help with math


Hi, the first thing you need to do is post your code inside of tags. Please put [ c++ ] and [ / c++ ] (without the spaces) around your code. This will help all of us understand it.

Just looking at it I can see a couple of mistakes. First you declare the variable mean in three different places. You shouldn't do that. You should only declare the variable once.

You can not use cmath::abs() in the form you are using it

CPP / C++ / C Code:
abs(firstNumber, secondNumber, thirdNumber, forthNumber);
is not valid. Here is page that explains the abs function. You are giving it too many parameters.

on this line:
CPP / C++ / C Code:
double mean = (firstNumber + secondNumber + thirdNumber + forthNumber)/4 // Calculate the mean of the 4 numbers


you do not have a semi colon and you are declaring mean again. You should remove this entire line. Also the numbers you read in are integers. Since you are doing an integer/interger your answer will also be an integer. In other words any remainder will be dropped after the divide. These numbers should be of type float to prevent this.

It seems your logic is correct but the notation is off.
__________________
"To argue with a person who has renounced the use of reason is like administering medicine to the dead."
-Thomas Paine
www.sullivan-county.com/deism.htm
  #6  
Old 20-Sep-2006, 13:37
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,720
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: C++ Help with math


Quote:
Originally Posted by alex18
Can someone please take a look at this and help me with the errors that I am getting and let me know if I am on track?

Normally I would respectfully suggest that you post the exact error messages, but the first ones are pretty clear. Well they are clear to someone who has done a lot of programming (and, therefore has seen lots and lots and lots of error messages). Believe it or not, I actually remember the greate confusion that consumed me the first time I tried some stuff like this. I really do.

In C and C++, code blocks are enclosed in braces (the '{', '}' thingies that some people call "curly brackets", not parentheses. The statements in the main() function are a block of code.

CPP / C++ / C Code:
int main()
{ /* a left brace, not a left parenthesis */
.
.
.
. 
} /* a right brace, not a right parenthesis */

Now try to compile and see if the error messages are maybe a little meaningful. If you can't understand what the heck they mean (or you can't see how to fix the code), then I suggest you post the exact messages here, along with whatever current code you are using (after whatever changes that you can see how to do). Paste the messages into your post; don't paraphrase.

You might also mention what compiler you are using. It may not be obvious from the messages, and sometimes it makes a difference to people who want to help.

Regards,

Dave
  #7  
Old 20-Sep-2006, 15:49
alex18 alex18 is offline
New Member
 
Join Date: Sep 2006
Posts: 14
alex18 has a little shameless behaviour in the past

Re: C++ Help with math


Wow great suggestions, thank you.

I will try this tonight and get back with you.

Thanks agian, I truly do appreciate the help!!!

Alex
  #8  
Old 20-Sep-2006, 20:26
alex18 alex18 is offline
New Member
 
Join Date: Sep 2006
Posts: 14
alex18 has a little shameless behaviour in the past

Re: C++ Help with math


Hi everyone, it's me again.
You will probably here a lot from me during this class.
I must say a have deep appreciation for everyone who understands and has the patience to code, my hats off to you all!

First I want to say thatnk you.

I have written the code for my program and I am not currently getting any errors.
Also, I am not getting any output.
I am able to enter the 4 values, but then nothing happens.

I am using cout statements.

Please let me know what I am missing.

Again thank you very very much.

Alex
  #9  
Old 20-Sep-2006, 20:28
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,720
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: C++ Help with math


Quote:
Originally Posted by alex18

Please let me know what I am missing.

You have to show us what you are doing before anyone can tell you what you are doing wrong (or what you are not doing).

In other words: show us the code.

Regards,

Dave
  #10  
Old 21-Sep-2006, 06:31
alex18 alex18 is offline
New Member
 
Join Date: Sep 2006
Posts: 14
alex18 has a little shameless behaviour in the past

Re: C++ Help with math


Thanks Dave:

Below is my code, but one more question:
Should I put the cout statements right below the cin statements rather than together on the bottom?

Again I really appreciate you!

Alex

CPP / C++ / C Code:
// This program computes the mean and deviation of 4 values entered by a user

#include <iostream> // Access iostream header file

#include <iomanip> // Access iomanip header file

#include <cmath> // Access cmath header file

#include <string> // Access string header file

#include <cfloat> // Access float header file

using namespace std;

int main()

{

// Variables

int i1, i2, i3, i4; // Input variables

int sum; // Computed sum

int mean; // computed mean

int std_dev; // computed deviation

// Prompt the user to input 4 integers

cout << "Enter four whole numbers, separated by spaces: ";

// Read the integers

cin >> i1 >> i2 >> i3 >> i4;

// Compute the sum

sum = i1 + i2 + i3 + i4;

// Compute the mean

mean = sum / 4;

// Compute the standard deviation

std_dev = sqrt((pow(i1 - mean, 2) +

pow(i2 - mean, 2) +

pow(i3 - mean, 2) +

pow(i4 - mean, 2)) / (4 - 1)); 


// Output the results

cout << "The sum of the four numbers is " << sum << endl; 

cout << "The mean of the four numbers are " << mean << endl; 

cout << "The standard deviation is " << std_dev << endl; 

return 0; 

cin.get() ;

cin.get() ;



}
Last edited by admin : 21-Sep-2006 at 09:38. Reason: Please insert your C code between [cpp] & [/cpp] tags
 
 

Recent GIDBlogDeveloping GUIs with wxPython (Part 1) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB 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
Help needed on visual C++ basic math program xoBeBExo C++ Forum 5 18-Nov-2005 01:52
Math function errors tnethaji C Programming Language 1 04-Jul-2005 13:22
C math functions tnethaji C Programming Language 1 04-Jul-2005 06:03
Beginning C++ math prog Dariklar C++ Forum 6 10-Sep-2003 11:51

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 12:38.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.