Overclock.net - Overclocking.net
     
 
Home Gallery Reviews Blogs Register Today's Posts Mark Forums Read Members List


Go Back   Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming > Application Programming

Reply
 
LinkBack Thread Tools
Old 10-06-09   #1 (permalink)
New to Overclock.net
 
Join Date: Sep 2009
Location: So Cal
Posts: 3

Rep: 0 hack2009 Unknown
Unique Rep: 0
Trader Rating: 0
Default Need to change to functions

I need help changing my code to functions. I have no clue where to start and I thought you guys could help me point me in the right direction. My code is below.

Code:
#include <iostream>
#include <iomanip>
#include <math.h>
#include <cmath>

using namespace std;

int main()
{
	double amount;
	double exact_sum = 0;
	double in_sum;
	double exact_squares = 0;
	double sumsquares_exact = 0; 
	int round_amount;
	int round_sum = 0;
	int round_squares;
	int count_amount = 0;
	int sumsquares_round = 0; 
	cout << "Enter an amount...Enter zero to calculate:";
	cin >> amount;
	while (amount > 0)
	{
		count_amount ++;
		exact_sum += amount;
		in_sum = (amount + 5) / 10;
		round_amount = in_sum * 10;
		round_sum += round_amount;
		exact_squares = amount * amount;
		sumsquares_exact += exact_squares;
		round_squares = round_amount * round_amount;
		sumsquares_round += round_squares;
		cout<< "Enter an amount...Enter zero to calculate:";
		cin>> amount;
	}
cout<<setiosflags(ios::fixed|ios::showpoint)<<setprecision(2);
double stddev_exact, stddev_round, prct_dif;

stddev_exact = sqrt((sumsquares_exact - (exact_sum * exact_sum / count_amount))/(count_amount - 1));
stddev_round = sqrt((sumsquares_round - (round_sum * round_sum / count_amount))/(count_amount - 1));

prct_dif = ((fabs(exact_sum - round_sum))/exact_sum) * 100;

if (count_amount != 1 && exact_sum != 0)
	{
	cout<<"\nCalculation     Exact       Round"
		<<"\n--------------------------------------"
		
		<<"\nSum:"<< setw(17)
		<<exact_sum
		<<setw(9)
		<<round_sum;  
	                      
	cout<<"\nAverage:"<<setw(13)
		<<prct_dif
		<<setw(12)
		<<prct_dif
		
		<<"\nStd Dev:"<<setw(13)
		<<stddev_exact
		<<setw(11)
		<<stddev_round;

	if (prct_dif <= 2)
		{
		cout<<"\nIt's cost effective!";
		}
	else
	cout<<"\nIt's not cost effective!";
	}
	else 
	cout<<"\nStd Dev cannot be calculated.";
	return 0;
	}
and heres the guidelines to this assignment.

Overview

For this assignment, you are to write a program that will test the effect of rounding dollar amounts to the nearest $10 rather than keeping track of exact amounts. It has been determined that if the difference between the results of the two methods is less than 2%, then it would be cost effective for a company to round rather than balance to the penny.

Your program should accept dollar amounts from the user until 0 is entered. Each exact dollar amount should be rounded to the nearest $10 value. The exact and rounded amounts should then be used to keep track of:

the sum of the exact dollar amounts
the sum of the rounded dollar amounts
the sum of the squares of the exact dollar amounts
the sum of the squares of the rounded dollar amounts
the number of dollar amounts entered
After the user has entered an exact amount of 0, display the sum, average, and standard deviation for both the exact and rounded amounts. Calculate the percent difference between the sums of the exact and rounded amounts and make a determination as to whether or not it's cost effective to round the dollar values.

When testing your program, keep in mind that $10 is a greater percent of a small value, than of a large value. If you test with only very small numbers (like $1.35, $2.99), common sense should tell you that the results will be radically different.

Functions

double getAmount()

The purpose of this function is to "get" an exact dollar amount from the user and verify that it is either 0 or positive. As long as the value entered is invalid, display an error message and prompt the user for a new value. This should be done until a valid value is entered. Once a valid value is entered, it should be returned to the calling function.

double roundAmount(double exactAmount)

The purpose of this function is to round the passed in amount to the nearest $10 value and return the rounded amount.

To round to the nearest $10: 1) add 5 to the exact amount, 2) divide by 10, 3) multiply the integer portion by 10.

double calcAverage(double sum, int count)

The purpose of this function is to calculate and return an average if the count is greater than 0. If the count is less than or equal to 0, return -1.

double calcStdDev(double sum, double sumSquared, int count)

The purpose of this function is to calculate and return the standard deviation if the count is greater than 1. If the count is less than 1, return -1. The formula for standard deviation is:




where

Σ( x2 ) is the sum of the squared values
(Σ x)2 is the square of the sum of the values
size is the number of values
double calcDiff(double exactSum, double roundedSum)

The purpose of this function is to calculate and return the percent difference between the sum of the exact values and the sum of the rounded values. To calculate percent difference:




Think about using the function fabs() to find the absolute value.

Output

The output from your program should consist of the prompts to the user to enter dollar amounts, a neatly formatted table that displays the results from the calculations, and a message indicating whether the percent difference is acceptable or unacceptable.

The basic format for the table is:

Calculation Exact Rounded
------------------------------------------
Sum:
Average:
Std Dev:
For average (and standard deviation), if the return code from calcAverage (and calcStdDev) is not -1, display both the exact and rounded average (and standard deviation). If the return code is -1, display a message that the average (and standard deviation) could not be calculated.
hack2009 is offline   Reply With Quote
Old 10-06-09   #2 (permalink)
_02
AMD Overclocker
 
_02's Avatar
 
amd nvidia

Join Date: Aug 2009
Location: Altanta, GA
Posts: 753

Rep: 78 _02 is acknowledged by some
Unique Rep: 65
Trader Rating: 0
Default

You should shorten your question to something that doesn't require reading your entire assignment.

I don't think anyone is willing to do your assignment for you.
__________________
System: Phenom II gaming
CPU
X3 720be @ 3.5 (1.39v)
Motherboard
GA-MA790XT-UD4P
Memory
2x2gb G.Skill DDR3 1333
Graphics Card
8800gt 512mb OC
Hard Drive
3xWD 250gb SATA II
Sound Card
Echo MiaMidi
Power Supply
Fortron BlueStorm II 500w
Case
Coolermaster Centurion
CPU cooling
Arctic Freezer Pro 64
GPU cooling
Zalman VF1050
OS
Win 7 32bit
Monitor
LG 22" LCD
_02 is offline   Reply With Quote
Old 10-06-09   #3 (permalink)
Case Modder
 
Spotswood's Avatar
 
Join Date: Jul 2008
Location: New Hampshire, USA
Posts: 236

Rep: 46 Spotswood is acknowledged by some
Unique Rep: 39
Trader Rating: 0
Default

As an example for how to go about completing this assignment, I refactored your "get amount" code into a separate getAmount() function:
Code:
#include <iostream>
#include <iomanip>
#include <math.h>
#include <cmath>

using namespace std;

double getAmount()
{    
    double amount;
    while (true)
    {
        cout << "Enter an amount...Enter zero to calculate:";
        cin >> amount;
        if (amount == 0 || amount > 0)
            break;
        cout << "Um, you need to enter 0 or a positive number";
    } 
    return amount;
}

int main()
{
    double amount;
    double exact_sum = 0;
    double in_sum;
    double exact_squares = 0;
    double sumsquares_exact = 0; 
    int round_amount;
    int round_sum = 0;
    int round_squares;
    int count_amount = 0;
    int sumsquares_round = 0; 
    while (amount = getAmount() > 0)
    {
        count_amount ++;
        exact_sum += amount;
        in_sum = (amount + 5) / 10;
        round_amount = in_sum * 10;
        round_sum += round_amount;
        exact_squares = amount * amount;
        sumsquares_exact += exact_squares;
        round_squares = round_amount * round_amount;
        sumsquares_round += round_squares;
    }
    cout<<setiosflags(ios::fixed|ios::showpoint)<<setprecision(2);
    double stddev_exact, stddev_round, prct_dif;

    stddev_exact = sqrt((double)(sumsquares_exact - (exact_sum * exact_sum / count_amount))/(count_amount - 1));
    stddev_round = sqrt((double)(sumsquares_round - (round_sum * round_sum / count_amount))/(count_amount - 1));

    prct_dif = ((fabs(exact_sum - round_sum))/exact_sum) * 100;

    if (count_amount != 1 && exact_sum != 0)
    {
        cout<<"\nCalculation     Exact       Round"
            <<"\n--------------------------------------"

            <<"\nSum:"<< setw(17)
            <<exact_sum
            <<setw(9)
            <<round_sum;  

        cout<<"\nAverage:"<<setw(13)
            <<prct_dif
            <<setw(12)
            <<prct_dif

            <<"\nStd Dev:"<<setw(13)
            <<stddev_exact
            <<setw(11)
            <<stddev_round;

        if (prct_dif <= 2)
        {
            cout<<"\nIt's cost effective!";
        }
        else
            cout<<"\nIt's not cost effective!";
    }
    else 
        cout<<"\nStd Dev cannot be calculated.";
    return 0;
}
Good luck!
__________________
Rich
Custom Wooden Case Builder
Overclock.net Mod of the Month
Spotswood is offline   Reply With Quote
Reply


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



All times are GMT -5. The time now is 09:36 PM.


Overclock.net is a Carbon Neutral Site Creative Commons License

Terms of Service / Forum Rules | Privacy Policy | DMCA Info | Advertising | Become an Official Vendor
Copyright © 2009 Shogun Interactive Development. Most rights reserved.
Page generated in 0.12914 seconds with 8 queries