cpp

You Are a Star Baby

[code type=cpp]
/* Samir M. Nassar
* samir.nassar@steamedpenguin.com
* License: GNU Public License version 3
* Title: You are a star baby
*/

#include
#include
#include

using namespace std;

struct MovieData
{
string title;
string director;
int releaseDate;
int length;
};

void showMovie (MovieData);
void giveMeStars ();

int main()
{
MovieData movie01;
MovieData movie02;

movie01.title = "The Bridge on the River Kwai";
movie01.director = "David Lean";
movie01.releaseDate = 1957;
movie01.length = 161;

cout << "Please enter a movie title: ";
getline(cin, movie02.title);
cout << endl;
cout << "Please enter the director's name: ";
getline(cin, movie02.director);
cout << endl;
cout << "Please enter the date the movie was released: ";
cin >> movie02.releaseDate;
cout << endl;
cout << "Please enter the length of the movie in minutes: ";
cin >> movie02.length;
cout << endl;

cout << "Here is the first movie: ";
showMovie(movie01);
cout << endl;
cout << "Here is the second movie: ";
showMovie(movie02);
cout << endl;

return 0;
}

void giveMeStars()
{
cout << endl;
for (int z = 1; z <= 70; z++)
{
cout << "*";
}
cout << endl;
}

void showMovie (MovieData movie)
{
cout << endl;
giveMeStars();
cout << movie.title;
giveMeStars();
cout << "Director: " << movie.director;
cout << endl;
cout << "Release Date: " << movie.releaseDate;
cout << endl;
cout << "Length: " << movie.length << " minutes";
giveMeStars();
cout << endl;
}
[/code]

To Give Ones Best

/* Samir M. Nassar
* samir.nassar@steamedpenguin.com
* License: GNU Public License version 3
* Title: To Give Ones Best
*/

#include
#include
#include
using namespace std;

int main()
{
char vaulterName[63];

cout << "Enter your name:" << endl;
cin.getline( vaulterName, 63);
cout << endl;
cout << "Enter three dates and their pole vault heights" << endl;
cout << endl;

// Entering data for first date
float vaultHeight1;
char vaultDate1[27];

cout << endl;
cout << "Enter the date for the first vault: " << endl;
cin.getline( vaultDate1, 27);
cout << endl;
cout << "Now enter the vault height for that date: " << endl;
cin >> vaultHeight1;
cin.ignore();

if ( vaultHeight1 < 2.0 || vaultHeight1 > 5.0 )
{
cout << endl;
cout << "You entered a an invalid value!" << endl;
cout << "Enter a value that is no smaller than 2.0 feet and no";
cout << " larger than 5.0 feet!" << endl;
cout << endl;
cout << "This program will now quit." << endl;

return 1;
}

//Entering data for second date
float vaultHeight2;
char vaultDate2[27];

cout << endl;
cout << "Enter the date for the second vault: " << endl;
cin.getline( vaultDate2, 27);
cout << endl;
cout << "Now enter the vault height for that date: " << endl;
cin >> vaultHeight2;
cin.ignore();

if ( vaultHeight2 < 2.0 || vaultHeight2 > 5.0 )
{
cout << endl;
cout << "You entered a an invalid value!" << endl;
cout << "Enter a value that is no smaller than 2.0 feet and no larger";
cout << " than 5.0 feet!" << endl;
cout << endl;
cout << "This program will now quit." << endl;

return 1;
}

//Entering data for third date
float vaultHeight3;
char vaultDate3[27];

cout << endl;
cout << "Enter the date for the third vault: " << endl;
cin.getline( vaultDate3, 27);
cout << endl;
cout << "Now enter the vault height for that date: " << endl;
cin >> vaultHeight3;
cin.ignore();

if ( vaultHeight3 < 2.0 || vaultHeight3 > 5.0 )
{
cout << endl;
cout << "You entered a an invalid value!" << endl;
cout << "Enter a value that is no smaller than 2.0 feet and no";
cout << " larger than 5.0 feet!" << endl;
cout << endl;
cout << "This program will now quit." << endl;

return 1;
}

//Now let's evaluate which height is the best
cout << endl;
cout << "The best vaults by " << vaulterName << " were:" << endl;

if ( vaultHeight1 >= vaultHeight2 && vaultHeight1 >= vaultHeight3 )
{
if ( vaultHeight2 >= vaultHeight3 )
{
// 1 > 2 > 3
cout << left << setw(10) << "First: " << vaultHeight1 << endl;
cout << " " << "On " << vaultDate1;
cout << endl;
cout << left << setw(10) << "Second: " << vaultHeight2 << endl;
cout << " " << "On " << vaultDate2;
cout << endl;
cout << left << setw(10) << "Third: " << vaultHeight3 << endl;
cout << " " << "On " << vaultDate3;
cout << endl;
}
else
{
// 1 > 3 > 2
cout << left << setw(10) << "First: " << vaultHeight1 << endl;
cout << " " << "On " << vaultDate1;
cout << endl;
cout << left << setw(10) << "Second: " << vaultHeight3 << endl;
cout << " " << "On " << vaultDate3;
cout << endl;
cout << left << setw(10) << "Third: " << vaultHeight2 << endl;
cout << " " << "On " << vaultDate2;
cout << endl;
}
}
else if ( vaultHeight2 >= vaultHeight1 && vaultHeight2 >= vaultHeight3 )
{
if ( vaultHeight1 >= vaultHeight3 )
{
// 2 > 1 > 3
cout << left << setw(10) << "First: " << vaultHeight2 << endl;
cout << " " << "On " << vaultDate2;
cout << endl;
cout << left << setw(10) << "Second: " << vaultHeight1 << endl;
cout << " " << "On " << vaultDate1;
cout << endl;
cout << left << setw(10) << "Third: " << vaultHeight3 << endl;
cout << " " << "On " << vaultDate3;
cout << endl;
}
else
{
// 2 > 3 > 1
cout << left << setw(10) << "First: " << vaultHeight2 << endl;
cout << " " << "On " << vaultDate2;
cout << endl;
cout << left << setw(10) << "Second: " << vaultHeight3 << endl;
cout << " " << "On " << vaultDate3;
cout << endl;
cout << left << setw(10) << "Third: " << vaultHeight1 << endl;
cout << " " << "On " << vaultDate1;
cout << endl;
}
}
else
{
if ( vaultHeight1 >= vaultHeight2 )
{
// 3 > 1 > 2
cout << left << setw(10) << "First: " << vaultHeight3 << endl;
cout << " " << "On " << vaultDate3;
cout << endl;
cout << left << setw(10) << "Second: " << vaultHeight1 << endl;
cout << " " << "On " << vaultDate1;
cout << endl;
cout << left << setw(10) << "Third: " << vaultHeight2 << endl;
cout << " " << "On " << vaultDate2;
cout << endl;
}
else
{
// 3 > 2 > 1
cout << left << setw(10) << "First: " << vaultHeight3 << endl;
cout << " " << "On " << vaultDate3;
cout << endl;
cout << left << setw(10) << "Second: " << vaultHeight2 << endl;
cout << " " << "On " << vaultDate2;
cout << endl;
cout << left << setw(10) << "Third: " << vaultHeight1 << endl;
cout << " " << "On " << vaultDate1;
cout << endl;
}
}

return 0;
}

Stadium Seats

[code type=cpp]
/* Samir M. Nassar
* samir.nassar@steamedpenguin.com
* License: GNU Public License version 3
* Title: Stadium Seats
*/

#include
#include
using namespace std;

int main()
{
double SeatCostClassA;
double SeatCostClassB;
double SeatCostClassC;
int SeatNumberClassA;
int SeatNumberClassB;
int SeatNumberClassC;
double IncomeClassA;
double IncomeClassB;
double IncomeClassC;

SeatCostClassA = 15.00;
SeatCostClassB = 12.00;
SeatCostClassC = 9.00;

cout << "How many seats were sold in Class A? ";
cin >> SeatNumberClassA;
cout << endl;
cout << endl;
cout << "How many seats were sold in Class B? ";
cin >> SeatNumberClassB;
cout << endl;
cout << endl;
cout << "How many seats were sold in Class C? ";
cin >> SeatNumberClassC;
cout << endl;
cout << endl;
cout << fixed << showpoint << setprecision(2);
IncomeClassA = SeatCostClassA * SeatNumberClassA;
IncomeClassB = SeatCostClassB * SeatNumberClassB;
IncomeClassC = SeatCostClassC * SeatNumberClassC;
cout << "Income generated from Class A tickets is: ";
cout << setw(10) << IncomeClassA <<"$";
cout << endl;
cout << "Income generated from Class B tickets is: ";
cout <

Seeing Stars

/* Samir M. Nassar
* samir.nassar@steamedpenguin.com
* License: GNU Public License version 3
* Title: Seeing Stars
*/

#include
#include
#include
using namespace std;

int main()
{
int rowNumber;
int maxStars;
int starsPerRow;
int padding;
int count1;
int count2;
int count3;
int count4;

cout << "Enter a number between 3 and 20: ";
cin >> rowNumber;

while ( rowNumber < 3 || rowNumber > 20 )
{
cout << endl;
cout << "You entered a bad number.";
cout << endl;
cout << "Enter a number between 3 and 20: ";
cin >> rowNumber;
}

cout << endl;
cout << endl;

maxStars = rowNumber + (rowNumber-1);

for ( count1 = 1 ; count1 <= rowNumber ; count1++ )
{
starsPerRow = count1 + (count1 - 1);
padding = ( maxStars - starsPerRow ) / 2;

for ( count2 = 1 ; count2 <= padding ; count2++ )
{
cout << " ";
}

for ( count3 = 1 ; count3 <= starsPerRow ; count3++ )
{
cout << "*";
}

for ( count4 = 1 ; count4 <= padding ; count4++ )
{
cout << " ";
}
cout << endl;
}

return 0;
}

Searching for Godot

[code type=cpp]
/* Samir M. Nassar
* samir.nassar@steamedpenguin.com
* License: GNU Public License version 3
* Title: Searching for Godot
*/

#include
#include
#include
#include
using namespace std;

int main()
{
int intArray[20];
bool foundNeg = false;
int counter = 0;

unsigned seed = time(0);
srand(seed);

for (int z = 0; z < 20; z++)
{
intArray[z] = (rand() % 70) - 20;
}

cout << endl;
cout << "Here are the numbers we generated: ";
cout << endl;
cout << endl;

for (int z = 0; z < 20; z++)
{
cout << intArray[z] << endl;
}

while (counter < 20 && !foundNeg)
{
if (intArray[counter] < 0 )
{
foundNeg = true;
}
else
{
counter++;
}
}

if (foundNeg)
{
cout << "The subscript of the first negative number";
cout << " in the array is: ";
cout << counter;
}
else
{
cout << "there are no negative numbers in the array" << endl;
}

cout << endl;
cout << endl;

return 0;
}
[/code]

Primal Nature

[code type=cpp]
/* Samir M. Nassar
* samir.nassar@steamedpenguin.com
* License: GNU Public License version 3
* Title: Primal Nature
*/

#include
#include
#include

using namespace std;

int number;
bool isPrime(int);

int main()
{
cout << "Enter an integer to check whether it is a prime: ";
cin >> number;
cout << endl;

if (isPrime(number))
{
cout << endl;
cout << number << " is prime!";
cout << endl;
cout << endl;
}
else
{
cout << endl;
cout << number << " is not prime!";
cout << endl;
cout << endl;
}

return 0;
}

/************************************************************************
* isPrime() tests whether an integer is a prime *
* *
* The function assumes all integers are prime then checks whether *
* that integer has a divisor larger than 1 and smaller than itself *
* *
* The integer 1 is not prime because it only has one divisor *
************************************************************************/
bool isPrime (int i)
{
bool prime;
prime = true;
if ( i == 1) // one is not prime
{
prime = false;
}
else
{
for (int j = 2; j < i; j++) // check for divisors
{
if (i%j == 0)
{
prime = false;
break;
}
}
}
return prime;
}
[/code]

Price Markup

[code type=cpp]
/* Samir M. Nassar
* samir.nassar@steamedpenguin.com
* License: GNU Public License version 3
* Title: Price markup
*/

#include
#include
#include
using namespace std;

double calculateRetail(double, double);

int main()
{
double wholesalePrice;
double markupPercentage;

cout << "This application will calculate what your retail prices need";
cout << endl;
cout << "to be based on the wholesale price and how much you need to";
cout << endl;
cout << "mark the price up.";
cout << endl;
cout << endl;

wholesalePrice = -1;

while (wholesalePrice < 0)
{
cout << "What is the wholesale price? ";
cin >> wholesalePrice;
cout << endl;
cout << endl;
}

markupPercentage = -1;

while (markupPercentage < 0)
{
cout << "By what percentage do you need to mark up the price? ";
cin >> markupPercentage;
cout << endl;
cout << endl;
}

cout << "The retail price after markup is: ";
cout << calculateRetail(wholesalePrice, markupPercentage);
cout << endl;
cout << endl;
}

double calculateRetail(double w, double m)
{
double retailPrice;

retailPrice = ( w + ( w * ( m/100 ) ) );
cout << fixed << showpoint << setprecision(2);
return retailPrice;
}
[/code]

The one hundredth digit

[code type=cpp]
/* Samir M. Nassar
* samir.nassar@steamedpenguin.com
* License: GNU Public License version 3
* Title: The one hundredth digit
*/

#include
using namespace std;

int main()
{
int integer;

cout << " Enter an integer of three or more digits: ";
cin >> integer;
cout << endl;
cout << endl;
cout << "The hundred's digit is: ";
cout << ((integer - (integer % 100))/100)%10;

return 0;
}
[/code]

Numbers Game

/* Samir M. Nassar
* samir.nassar@steamedpenguin.com
* License: GNU Public License version 3
* Title: Numbers Game
*/

#include
#include
#include
using namespace std;

int main()
{
int numInput;
int largest;
int evenCounter = 0;
int tally = 0;
int numOld = -99;

cout << "Enter an integer when asked. The program will give you" << endl;
cout << "the average of the even numbers as well as the largst" << endl;
cout << "number entered. The program will stop tabulating numbers" << endl;
cout << "when you enter -99.";
cout << endl;
cout << endl;

while (numInput != -99)
{
cout << "Enter an integer: ";
cin >> numInput;

if ( (numInput % 2) == 0) // Weed out even numbers
{
tally += numInput; // Total sum of even integers
evenCounter++; // How many even integers there are
}

if (numInput > numOld)
{
largest = numInput;
}
else
{
largest = numOld;
}

numOld = numInput;
}

cout << endl;
cout << endl;

if (tally != 0) // This prevents crashes if we enter no even integers.
{
cout << "Average of even numbers: " << tally / evenCounter;
cout << endl;
cout << endl;
}

cout << "The largest number entered is: " << largest;
cout << endl;
cout << endl;

return 0;
}

Monthly Sales Tax File Output

[code type=cpp]
/* Samir M. Nassar
* samir.nassar@steamedpenguin.com
* License: GNU Public License version 3
* Title: Monthly Sales Tax File Output
*/

#include
#include
#include
using namespace std;

int main()
{
char monthTax[10];
int yearTax;
float totalAmountPostTax;
float sales;
float totalSalesTax;
float countySalesTax;
float stateSalesTax;
ofstream taxOutputFile;

// Ask for the month
cout << "What month do you want to calculate taxes for? ";
cin >> monthTax;
cout << endl;
// Ask for the year
cout << "What year is it? ";
cin >> yearTax;
cout << endl;
// Ask for the total receipts
cout << "What are your total receipts? ";
cin >> totalAmountPostTax;
cout << endl;

sales = totalAmountPostTax / 1.06;
totalSalesTax = totalAmountPostTax - sales;
countySalesTax = totalAmountPostTax - ( totalAmountPostTax / 1.02 );
stateSalesTax = totalAmountPostTax - ( totalAmountPostTax / 1.04 );

cout << fixed << showpoint << setprecision(2);

cout << "Month: " << monthTax << ", " << yearTax;
cout << endl;
cout << "----------------------------------------";
cout << endl;
cout << setw(25) << left;
cout << "Total Collected: " << "$ ";
cout << setw(9) << right;
cout << totalAmountPostTax;
cout << endl;
cout << setw(25) << left;
cout << "Sales: " << "$ ";
cout << setw(9) << right;
cout << sales;
cout << endl;
cout << setw(25) << left;
cout << "County Sales Tax: " << "$ ";
cout << setw(9) << right;
cout << countySalesTax;
cout << endl;
cout << setw(25) << left;
cout << "State Sales Tax: " << "$ ";
cout << setw(9) << right;
cout << stateSalesTax;
cout << endl;
cout << setw(25) << left;
cout << "Total Sales Tax: " << "$ ";
cout << setw(9) << right;
cout << totalSalesTax;
cout << endl;
cout << "----------------------------------------";
cout << endl;
cout << endl;

system("pause");

cout << endl;
cout << "Writing to file...";
cout << endl;

taxOutputFile.open("tax-tables.txt");

taxOutputFile << fixed << showpoint << setprecision(2);

taxOutputFile << "Month: " << monthTax << ", " << yearTax;
taxOutputFile << endl;
taxOutputFile << "----------------------------------------";
taxOutputFile << endl;
taxOutputFile << setw(25) << left;
taxOutputFile << "Total Collected: " << "$ ";
taxOutputFile << setw(9) << right;
taxOutputFile << totalAmountPostTax;
taxOutputFile << endl;
taxOutputFile << setw(25) << left;
taxOutputFile << "Sales: " << "$ ";
taxOutputFile << setw(9) << right;
taxOutputFile << sales;
taxOutputFile << endl;
taxOutputFile << setw(25) << left;
taxOutputFile << "County Sales Tax: " << "$ ";
taxOutputFile << setw(9) << right;
taxOutputFile << countySalesTax;
taxOutputFile << endl;
taxOutputFile << setw(25) << left;
taxOutputFile << "State Sales Tax: " << "$ ";
taxOutputFile << setw(9) << right;
taxOutputFile << stateSalesTax;
taxOutputFile << endl;
taxOutputFile << setw(25) << left;
taxOutputFile << "Total Sales Tax: " << "$ ";
taxOutputFile << setw(9) << right;
taxOutputFile << totalSalesTax;
taxOutputFile << endl;
taxOutputFile << "----------------------------------------";

taxOutputFile.close();
return 0;
}
[/code]

Syndicate content