PDA

View Full Version : C++ classwork


Zero Tolerance
05-27-2003, 02:15 AM
here is a lil proggy we had last semester in "Structured Programming".....i thought it was kinda neat and would share it with who might wanna use it or add to it and make something a lil more unique......

the comments say it all so i wont waste more space explaining it...


//************************************************** ****************
// Rainfall program
// This program inputs 12 monthly rainfall amounts from a
// recording site and computes the average monthly rainfall.
// This process is repeated for as many recording sites as
// the user wishes.
//************************************************** ****************
#include <iostream>
#include <iomanip> // For setprecision()

using namespace std;

void Get12Amounts( float& );
void GetOneAmount( float& );
void GetYesOrNo( char& );

int main()
{
float sum; // Sum of 12 rainfall amounts
char response; // User response ('y' or 'n')

cout << fixed << showpoint // Set up floating pt.
<< setprecision(2); // output format

do
{
Get12Amounts(sum);
cout << endl << "Average rainfall is " << sum / 12.0
<< " inches" << endl << endl;
cout << "Do you have another recording site? (y or n) ";
GetYesOrNo(response);
} while (response == 'y');
return 0;
}

//************************************************** ****************

void Get12Amounts( /* out */ float& sum ) // Sum of 12 rainfall
// amounts

// Inputs 12 monthly rainfall amounts, verifying that
// each is nonnegative, and returns their sum

// Postcondition:
// 12 rainfall amounts have been read and verified to be
// nonnegative
// && sum == sum of the 12 input values

{
int count; // Loop control variable
float amount; // Rainfall amount for one month

sum = 0;
for (count = 1; count <= 12; count++)
{
cout << "Enter rainfall amount " << count << ": ";
GetOneAmount(amount);
sum = sum + amount;
}
}

//************************************************** ****************

void GetYesOrNo( /* out */ char& response ) // User response char

// Inputs a character from the user and, if necessary,
// repeatedly prints an error message and inputs another
// character if the character isn't 'y' or 'n'

// Postcondition:
// response has been input (repeatedly, if necessary, along
// with output of an error message)
// && response == 'y' or 'n'

{
do
{
cin >> response;
if (response != 'y' && response != 'n')
cout << "Please type y or n: ";
} while (response != 'y' && response != 'n');
}

//************************************************** ****************

void GetOneAmount( /* out */ float& amount ) // Rainfall amount
// for one month

// Inputs one month's rainfall amount and, if necessary,
// repeatedly prints an error message and inputs another
// value if the value is negative

// Postcondition:
// amount has been input (repeatedly, if necessary, along
// with output of an error message)
// && amount >= 0.0

{
do
{
cin >> amount;
if (amount < 0.0)
cout << "Amount cannot be negative. Enter again: ";
} while (amount < 0.0);
}

Zero Tolerance
05-27-2003, 02:18 AM
here is a code sample you can use for raising to the power of.....


#include <iostream>

using namespace std;

int Power( int, int );

int main()
{
int number; // Number that is being raised to power
int exponent; // Power the number is being raised to

cin >> number >> exponent;
cout << Power(number, exponent); // Nonrecursive call
return 0;
}

//************************************************** ****************

int Power( /* in */ int x, // Number that is being raised to power
/* in */ int n ) // Power the number is being raised to

// Computes x to the n power by multiplying x times the result of
// computing x to the n - 1 power.

// Precondition:
// x is assigned && n > 0
// Postcondition:
// Function value == x raised to the power n
// Note:
// Large exponents may result in integer overflow

{
if (n == 1)
return x; // Base case
else
return x * Power(x, n - 1); // Recursive call
}

Zero Tolerance
05-27-2003, 02:22 AM
a sample of template code...nothing fancy but could be useful to some....


//************************************************** ****************
//Main program file that uses function templates
//defined in header file templs.h (Chapt. 17)
//************************************************** ****************
#include <iostream>
#include <string>

using namespace std;

enum StatusType {OK, OUT_OF_STOCK, BACK_ORDERED};

#include "templs.h" // Insert template definitions AFTER
// StatusType is defined
int main()
{
int n = 35;
float x = -56.98;
char ch = 'A';
string str = "Hello";
StatusType status = OUT_OF_STOCK;

Print("n", n);
Print("x", x);
Print("ch", ch);
Print("str", str);
Print("status", status);
return 0;
}


ill post some more code samples when i get the time

:D