|
![]() |
Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming > Application Programming | |
C++, help with intro to functions
|
||
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) | |||||||||||||
|
Folding Fanatic
![]() |
I've been working at this assignment for a few days now, and I've finally got a fairly solid shell, but I'm so tired right now I'm struggling with some of my debugging especially with respect to my prototypes. Any help would be appreciated.
![]() Code:
/***************************************************************
CSCI 240 Program 5 Fall 2009
Programmer: Chris Ebener
Section: 1
TA: ViKram Kumra
Date Due: 10/09/09
Purpose: The purpose of this program is to calculate whether it
is cost effective for a company to round all monetary figures
to the closest figure of 10 given the exact numbers from the user.
***************************************************************/
#include <iostream>
#include <iomanip>
#include <math.h>
#include <cmath>
double getAmount(double);
double roundAmount(double);
double calcAverage_exact(double, int);
double calcAverage_round(int, int);
double calcStdDev_exact(double, double, int);
double calcStdDev_round(int, int, int);
double calcDiff(double, double);
using namespace std;
int main()
{
double money;
int round_money;
double exact_sum = 0;
double in_sum;
int round_sum = 0;
double exact_squares = 0;
int round_squares;
int count_money = 0;
double sumsquares_exact = 0;
int sumsquares_round = 0;
cout<< "\nEnter an amount (0 to quit):";
cin>> money;
while(money > 0)
{
count_money ++;
exact_sum += money;
money = getAmount(money);
round_money = roundAmount(money);
round_sum += round_money;
exact_squares = money * money;
sumsquares_exact += exact_squares;
round_squares = round_money * round_money;
sumsquares_round += round_squares;
cout<< "\nEnter an amount (0 to quit) :";
cin>> money;
}
cout<<setiosflags(ios::fixed|ios::showpoint)<<setprecision(2);
double average_exact;
double stddev_exact;
double prct_dif;
int average_round;
int stddev_round;
average_exact = calcAverage_exact(sumsquares_exact, count_money);
average_round = calcAverage_round(sumsquares_round, count_money);
stddev_exact = calcStdDev_exact(exact_sum, sumsquares_exact, count_money);
stddev_round = calcStdDev_round(sumsquares_round, count_money);
prct_dif = calcDiff(exact_sum, round_sum);
if(average_exact == -1 or average_round == -1)
cout<<"not enough values to calculate the average";
if(stddev_exact == -1 || stddev_round == -1)
cout<<"not enough values to calculate the standard deviation";
else
cout<<"\nCalculation Exact Round";
cout<<"\n--------------------------------------";
cout<<"\nSum:"
<<setw(17)
<<average_exact
<<setw(12)
<<average_round;
cout<<"\nAverage:"
<<setw(13)
<<prct_dif
<<setw(12)
<<prct_dif;
cout<<"\nStd Dev:"
<<setw(13)
<<stddev_exact
<<setw(12)
<<stddev_round;
if (prct_dif <= 2)
{
cout<<"\nIts cost effective!\n";
system ("pause");
return 0;
}
else
cout<<"\nIts not cost effective!\n";
system ("pause");
return 0;
}
// Function 1
double getAmount(double money)
{
if (money > 0)
{
return money;
}
else
if (money < 0)
{
cout<<"\nPlease enter a valid number";
cin>>money;
return money;
}
else
return 0;
}
// Function 2
double roundAmount(double money)
{
double in_sum;
int round_money;
in_sum = (money + 5) / 10;
round_money = in_sum * 10;
return round_money;
}
// Function 3.1
double calcAverage_exact(double exact_sum, int count_money)
{
double average_exact;
if (count_money > 1)
{
average_exact = exact_sum / count_money ;
return average_exact;;
}
else
return -1;
}
// Function 3.2
double calcAverage_round(int round_sum, int count_money)
{
double average_round;
if (count_money > 1)
{
average_round = round_sum / count_money;
return average_round;
}
else
return -1;
}
// Function 4.1
double calcStdDev_exact(double exact_sum, double sumsquares_exact, int count_money)
{
double stddev_exact;
if (count_money > 1)
{
stddev_exact = sqrt((sumsquares_exact - (exact_sum * exact_sum / count_money))/(count_money - 1));
return stddev_exact;
}
else
return -1;
}
//Function 4.2
double calcStdDev_round(int round_sum, int sumsquares_round, int count_money)
{
int stddev_round;
if (count_money > 1)
{
stddev_round = sqrt((sumsquares_round - (round_sum * round_sum / count_money))/(count_money - 1));
return stddev_round;
}
else
return -1;
}
//Function 5
double calcDiff(double exact_sum, double round_sum)
{
double prct_dif;
prct_dif = ((fabs(exact_sum - round_sum))/exact_sum) * 100;
return prct_dif;
}
Last edited by tofunater : 10-11-09 at 07:50 PM |
|||||||||||||
|
|
|
|
|
#2 (permalink) | |||||||||||||
|
Linux Lobbyist
![]()
Join Date: Oct 2008
Location: Melbourne, Teh land of Oz
Posts: 1,675
Rep: 98
![]() Unique Rep: 85
Trader Rating: 0
|
Well, you need to actually tell us what's the problem... what kinda error messages are you getting, or what is the program actually doing wrong?
Also, strictly speaking, your formatting is far from terrible, though you could use more empty lines to "break the code up" at certain places, the formatting is quite good for someone who's just learning.
__________________
Statistix show that 92% of teenagers have moved to real music . If you are one of the sane 8% that still listen to funk, hip-hop or jazz, copy and past this into your sig, for thou art now offically awesome!![]() ![]() ![]() ![]() DISCLAIMER:By reading the above post, you accept that the poster ("bomfunk") is now the rightful owner of all your present possessions.
|
|||||||||||||
|
|
|
|
|
#3 (permalink) |
|
Case Modder
![]() |
In order to compile your code I had to change the declaration of calcAverage_round to:
Code:
double calcAverage_round(int, int); Code:
while(money != 0)
{
money = getAmount(money);
count_money ++;
exact_sum += money;
round_money = roundAmount(money);
round_sum += round_money;
exact_squares = money * money;
sumsquares_exact += exact_squares;
round_squares = round_money * round_money;
sumsquares_round += round_squares;
cout<< "\nEnter an amount (0 to quit) :";
}
![]() Hmmmm...this is obviously wrong: Code:
double getAmount(double money)
{
while (money < 0)
{
cout<<"\nPlease enter a valid number";
cin>>money;
return money;
}
if (money > 0)
return money;
else
return 0;
}
![]() Good luck!
__________________
Rich Custom Wooden Case Builder
|
|
|
|
|
|
#4 (permalink) | |||||||||||||
|
Folding Fanatic
![]() |
Thank you for the hints spotswood, I really just needed to take a step back from it for awhile and attack it again later
Now I've eliminated the infinite loop, and the program works, but the final calculation seems off, would someone mind running it and seeing if they can figure out if I'm just crazy?
|
|||||||||||||
|
|
|
|
|
#5 (permalink) | ||||||||||
|
PC Gamer
![]() |
Hey Chris,
I was trying to go through your code but I noticed a LOT of commands were missing the beginning and closing brackets like Code:
if (blah blah blah){...}
__________________
|
||||||||||
|
|
|
|
|
#6 (permalink) | |||||||||||||
|
Overclocker
![]()
Join Date: May 2007
Location: Houston, Texas
Posts: 5,873
Rep: 226
![]() ![]() ![]() Unique Rep: 166
Trader Rating: 14
|
Two words, GLOBAL VARIABLES. Gotta love C++
__________________
|
|||||||||||||
|
|
|
|
|
#7 (permalink) | ||||||||||
|
PC Gamer
![]() |
Here is what I was able to fix so far.
I also noticed that you forget to close a lot of functions (with the }) so if you could go through and make sure that everything is in brackets. Also, if you add some comments into the program so I can get a faint idea of what is going on that would really help. It is also somewhat helpful to add comments to the end of each of the brackets. For example Code:
} //end else }//end function1 }//end main() Lastly, indenting the code so one can see the hierarchy of the if else statements would be awesome. If the program you are copying and pasting from does not work out so well you could try just uploading the cpp file or using a site like pastebin(.us?) to show us the code. Once this stuff gets corrected I'll be more than happy to have another look at the code to see if I can get it working. Code:
/***************************************************************
CSCI 240 Program 5 Fall 2009
Programmer: Chris Ebener
Section: 1
TA: ViKram Kumra
Date Due: 10/09/09
Purpose: The purpose of this program is to calculate whether it
is cost effective for a company to round all monetary figures
to the closest figure of 10 given the exact numbers from the user.
***************************************************************/
#include <iostream>
#include <iomanip>
#include <math.h>
#include <cmath>
double getAmount(double);
double roundAmount(double);
double calcAverage_exact(double, int);
double calcAverage_round(int, int);
double calcStdDev_exact(double, double, int);
double calcStdDev_round(int, int, int);
double calcDiff(double, double);
using namespace std;
int main()
{
double money;
int round_money;
double exact_sum = 0;
double in_sum;
int round_sum = 0;
double exact_squares = 0;
int round_squares;
int count_money = 0;
double sumsquares_exact = 0;
int sumsquares_round = 0;
cout<< "\nEnter an amount (0 to quit):";
cin>> money;
while(money > 0)
{
count_money ++;
exact_sum += money;
money = getAmount(money);
round_money = roundAmount(money);
round_sum += round_money;
exact_squares = money * money;
sumsquares_exact += exact_squares;
round_squares = round_money * round_money;
sumsquares_round += round_squares;
cout<< "\nEnter an amount (0 to quit) :";
cin>> money;
}
cout<<setiosflags(ios::fixed|ios::showpoint)<<setprecision(2);
double average_exact;
double stddev_exact;
double prct_dif;
int average_round;
int stddev_round;
average_exact = calcAverage_exact(sumsquares_exact, count_money);
average_round = calcAverage_round(sumsquares_round, count_money);
stddev_exact = calcStdDev_exact(exact_sum, sumsquares_exact, count_money);
stddev_round = calcStdDev_round(sumsquares_round, count_money);
prct_dif = calcDiff(exact_sum, round_sum);
if(average_exact == -1 or average_round == -1)
{
cout<<"not enough values to calculate the average";
}
if(stddev_exact == -1 || stddev_round == -1)
{
cout<<"not enough values to calculate the standard deviation";
}
else
{
cout<<"\nCalculation Exact Round";
cout<<"\n--------------------------------------";
cout<<"\nSum:"
<<setw(17)
<<average_exact
<<setw(12)
<<average_round;
cout<<"\nAverage:"
<<setw(13)
<<prct_dif
<<setw(12)
<<prct_dif;
cout<<"\nStd Dev:"
<<setw(13)
<<stddev_exact
<<setw(12)
<<stddev_round;
}
if (prct_dif <= 2)
{
cout<<"\nIts cost effective!\n";
system ("pause");
return 0;
}
else
{
cout<<"\nIts not cost effective!\n";
system ("pause");
return 0;
}
// Function 1
double getAmount(double money)
{
if (money > 0)
{
return money;
}
else
if (money < 0)
{
cout<<"\nPlease enter a valid number";
cin>>money;
return money;
}
else
{
return 0;
}
}
// Function 2
double roundAmount(double money)
{
double in_sum;
int round_money;
in_sum = (money + 5) / 10;
round_money = in_sum * 10;
return round_money;
}
// Function 3.1
double calcAverage_exact(double exact_sum, int count_money)
{
double average_exact;
if (count_money > 1)
{
average_exact = exact_sum / count_money ;
return average_exact;;
}
else
{
return -1;
}
}
// Function 3.2
double calcAverage_round(int round_sum, int count_money)
{
double average_round;
if (count_money > 1)
{
average_round = round_sum / count_money;
return average_round;
}
else
{
return -1;
}
}
// Function 4.1
double calcStdDev_exact(double exact_sum, double sumsquares_exact, int count_money)
{
double stddev_exact;
if (count_money > 1)
{
stddev_exact = sqrt((sumsquares_exact - (exact_sum * exact_sum / count_money))/(count_money - 1));
return stddev_exact;
}
else
return -1;
}
//Function 4.2
double calcStdDev_round(int round_sum, int sumsquares_round, int count_money)
{
int stddev_round;
if (count_money > 1)
{
stddev_round = sqrt((sumsquares_round - (round_sum * round_sum / count_money))/(count_money - 1));
return stddev_round;
}
else
{
return -1;
}
}
//Function 5
double calcDiff(double exact_sum, double round_sum)
{
double prct_dif;
prct_dif = ((fabs(exact_sum - round_sum))/exact_sum) * 100;
return prct_dif;
}
return 0;
}
__________________
|
||||||||||
|
|
|
|
|
#8 (permalink) | ||||||||||
|
PC Gamer
![]() |
Easy now but I would not recommend using them. Look at my "We lost our programming virginity" thread (I started it) to see a lot of reasons why you shouldn't use them.
__________________
|
||||||||||
|
|
|
|
|
#9 (permalink) | |||||||||||||
|
Folding Fanatic
![]() |
I apologize for the formatting, I'll get to work on line documentation. As for end brackets, I'm not sure what you mean, the program compiles and runs just fine. Mostly I was hoping that you would look over the math portion to see if the results seemed normal.
edit: oh you mean after single line code statements. Technically I'm allowed to run single command lines without brackets, but it is probably a good idea to get into the habit of including them after every decision statement.
Last edited by tofunater : 10-11-09 at 10:31 PM |
|||||||||||||
|
|
|
|
|
#10 (permalink) | ||||||||||||||
|
The Dapper Swindler
![]() |
Quote:
@OP how did you even get that to compile? You certainly weren't using g++. Code:
average_exact = calcAverage_exact(sumsquares_exact, count_money); average_round = calcAverage_round(sumsquares_round, count_money); stddev_exact = calcStdDev_exact(exact_sum, sumsquares_exact, count_money); stddev_round = calcStdDev_round(exact_sum, sumsquares_round, count_money); prct_dif = calcDiff(exact_sum, round_sum); Add the term in red and as far as I can tell it runs. Also, you're using a lot of double to int conversions, which is throwing a bunch of warning flags. Every time you want to convert a double to an int you have to put (int) in front of the variable. Its not that big of a deal, but it might screw with your data, and you'll probably lose marks for the warning flags. Also either the code was copied wrong or your math is bad, because according to your program when you enter 1,2,3 the sum is 4.67, and the average is 250.00. The standard deviation seems right though.
Last edited by nathris : 10-11-09 at 10:37 PM |
||||||||||||||
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|