Chapter 7 Grade Book

11. Grade Book
A teacher has five students who have taken four tests. The teacher uses the following grading scale to assign a letter grade to a student, based on the average of his or her four test scores. Write a program that uses an array of string objects to hold the  five student names, an array of  five characters to hold the  five students letter grades, and  five arrays of four doubl es to hold each students set of test scores.  The program should allow the user to enter each students name and his or her four test scores. It should then calculate and display each students average test score and a letter grade based on the average. Input Validation: Do not accept test scores less than 0 or greater than 100.
12. Grade Book Modi cation
Modify  the  grade  book  application  in  Programming  Challenge  13  so  it  drops  each students lowest score when determining the test score averages and letter grades.*/

#include “stdafx.h”
#include<iostream>
#include<string>

using namespace std;

int main()
{
const int size=5;
//array for students’ names
string sNames[size];
//get Names
for(int i=0;i<5;i++)
{
cout<<“Enter a student’s name: “;
cin>>sNames[i];
}

//get scores
double scoreForS[size][4], avg[size],sum[size]={0,0,0,0,0};

for(int x=0;x<5;x++)
{
for(int y=0;y<4;y++)
{
cout<<“Enter the score for student “<<(x+1)<<” “;
cin>>scoreForS[x][y];
//input validation
while(scoreForS[x][y]<0||scoreForS[x][y]>100)
{
cout<<“Error.The range of score is 0 to 100. Reenter : “;
cin>>scoreForS[x][y];
}
sum[x]+=scoreForS[x][y];
}
}

//drop lowest score

double lowest[size];

for(int a=0;a<5;a++)
{
lowest[a]= scoreForS[a][0];
for(int b=1;b<4;b++)
{
if (lowest[a]>scoreForS[a][b])
lowest[a]= scoreForS[a][b];

}
sum[a]-=lowest[a];
}

//get avg
for(int c=0;c<5;c++)
{
avg[c]=sum[c]/3.0;
}

//scale
char grade[size];
for(int d=0;d<5;d++)
{
if(avg[d]>=90)grade[d]=’A’;
else if(avg[d]>=80)grade[d]=’B’;
else if(avg[d]>=70)grade[d]=’C’;
else if(avg[d]>=60) grade[d]=’D’;
else grade[d]=’F’;
cout<<“\nStudent “<<sNames[d]<<” get “<<grade[d];
}

system(“pause”);
return 0;
}

Chapter 7 Exam Grader

/*10. Exam Grader

One  of  your  professors  has  asked  you  to  write  a  program  to  grade  her final  exams, which consist of only 20 multiple-choice questions. Each question has one of four possible answers: A, B, C, or D. The le CorrectAnswers.txt contains the correct answers for all of the questions, with each answer written on a separate line. The  first line contains the answer to the  rst question, the second line contains the answer to the second question, and so forth.  Write  a  program  that  reads  the  contents  of  the  CorrectAnswers.txt le  into  a char array, and then reads the contents of another  file, containing a students answers, into a  second char  array.   The  program  should  determine  the  number  of  questions  that  the  student missed, and then display the following:

A list of the questions missed by the student, showing the correct answer and the incorrect answer provided by the student for each missed question
The total number of questions missed
The percentage of questions answered correctly. This can be calculated as
Correctly Answered Questions  ÷ Total Number of Questions
If the percentage of correctly answered questions is 70% or greater, the program should  indicate  that  the  student  passed  the  exam.  Otherwise,  it  should  indicate that the student failed the exam.

#include “stdafx.h”
#include<iostream>
#include<fstream>
#include<string>

using namespace std;

int main()
{
const int size=20;
int count=0;
string correctAnswers[size];
string  studentAnswer[size];
//Read Correct Answer
ifstream inputfile;
inputfile.open(“CorrectAnswers.txt”);

if(inputfile)
{
for(int i=0;i<20;i++)
{
inputfile>>correctAnswers[i];

}

}
else
{
cout<<“\nOpen file error.”;

}
inputfile.close();

//read student answer

ifstream inputSfile;
inputSfile.open(“StudentAnswers.txt”);

if(inputSfile)
{
for(int x=0;x<20;x++)
{
inputSfile>>studentAnswer[x];

}

}
else
{
cout<<“\nOpen file error.”;

}
inputSfile.close();

//Compare answer
for(int y =0;y<20;y++)
{

if(correctAnswers[y]!=studentAnswer[y])
{
cout<<“\nCorrect Answer: “<<correctAnswers[y];
cout<<“\nStudent Answer: “<<studentAnswer[y];
count++;
}

}
double percentageOfC=(20-count)/20.0;

cout<<“\nTotal number of questions missed: “<<count;
cout<<“\nPercentage of questions answered correctly: “<<percentageOfC;
if(percentageOfC>=0.7)cout<<“\nPass”;
else cout<<“\nFail”<<endl;

system(“pause”);
return 0;
}

Chapter 7 Payroll

/*8. Payroll
Write a program that uses the following arrays:

empId: an array of seven long integers to hold employee identification numbers.

The array should be initialized with the following numbers:
5658845   4520125   7895122   8777541
8451277   1302850   7580489

hours: an array of seven integers to hold the number of hours worked by each
employee

payRate: an array of seven doubles to hold each employees hourly pay rate

wages: an array of seven doubles to hold each employees gross wages

The program should relate the data in each array through the subscripts. For example,  the  number  in  element  0  of  the hours  array  should  be  the  number  of  hours worked  by  the  employee  whose  identification  number  is  stored  in  element  0  of  the empId  array.  That  same  employees  pay  rate  should  be  stored  in  element  0  of  the payRate array.

The  program  should  display  each  employee  number  and  ask  the  user  to  enter  that employees  hours  and  pay  rate.  It  should  then  calculate  the  gross  wages  for  that employee (hours times pay rate) and store them in the wages array. After the data has been entered for all the employees, the program should display each employees identification number and gross wages.

Input Validation: Do not accept negative values for hours or numbers less than 6.00 for pay rate.*/

#include “stdafx.h”
#include<iostream>

using namespace std;

int main()
{
const int size=7;
int empId[size]={5658845,4520125,7895122,8777541,8451277,1302850,7580489};
int hours[size];
double payRate[size];
double wages[size];

//get data
for(int i=0;i<=size;i++)
{
cout<<“\nEnter the number of hours worked by the employee whose ID is “<<empId[i];
cin>>hours[i];

while(hours[i]<0)
{
cout<<“\nError.Do not accept negative number. Reenter: “;
cin>>hours[i];
}

cout<<“\nEnter the hourly pay rate of the employee whose ID is “<<empId[i]<<” “;
cin>>payRate[i];

while(payRate[i]<0)
{
cout<<“\nError.Do not accept negative number. Reenter: “;
cin>>hours[i];
}

wages[i]=hours[i]*payRate[i];
}

//display data
for(int x=0;x<=size;x++)
{
cout<<“\nEmployee: “<<empId[x]<<“\t\tHours: “<<hours[x]<<“\t\tGross Wages:”<<wages[x];
}
cout<<endl;

system(“pause”);

return 0;
}

Chapter 7 Monkey Business

/*4. Monkey Business
A local zoo wants to keep track of how many pounds of food each of its three monkeys eats each  day  during  a  typical  week.  Write  a  program  that  stores  this  information  in  a  two-dimensional 3 * 7 array, where each row represents a different monkey and each column represents a different day of the week. The program should  first have the user input the data for each monkey. Then it should create a report that includes the following information:

Average amount of food eaten per day by the whole family of monkeys.
The least amount of food eaten during the week by any one monkey.
The greatest amount of food eaten during the week by any one monkey.

Input Validation: Do not accept negative numbers for pounds of food eaten.*/

 

#include “stdafx.h”
#include<iostream>

using namespace std;

int main()
{
int monkeyEat[3][7], sum=0,sumOf[3]={0,0,0};
double avg,avgOf1,avgOf2,avgOf3;

//get data
for(int i=0;i<=2;i++)
{

for(int x=0;x<=6;x++)
{
cout<<“\nEnter the amount of food eaten by monkey “<<i+1<<” in day “<<x+1<<” : “;
cin>>monkeyEat[i][x];

while (monkeyEat[i][x]<0)
{
cout<<“\nNot accept negative values. Reenter: “;
cin>>monkeyEat[i][x];
}
sum+=monkeyEat[i][x];
sumOf[i]+=monkeyEat[i][x];
}
}
avg=sum/7;
avgOf1=sumOf[0]/7.0;
avgOf2=sumOf[1]/7.0;
avgOf3=sumOf[2]/7.0;

cout<<“\nThe average amount of food eaten per day by the whole family of monkeys is “<<avg;

if(avgOf1<=avgOf2&&avgOf1<=avgOf3)
cout<<“\nThe least amount of food eaten during the week by any one monkey is “<<sumOf[0];
else if(avgOf2<=avgOf1&&avgOf2<=avgOf3)
cout<<“\nThe least amount of food eaten during the week by any one monkey is “<<sumOf[1];
else
cout<<“\nThe least amount of food eaten during the week by any one monkey is “<<sumOf[2];

if(avgOf1>=avgOf2&&avgOf1>=avgOf3)
cout<<“\vThe largest amount of food eaten during the week by any one monkey is “<<sumOf[0];
else if(avgOf2>=avgOf1&&avgOf2>=avgOf3)
cout<<“\nThe largest amount of food eaten during the week by any one monkey is “<<sumOf[1];
else
cout<<“\nThe largest amount of food eaten during the week by any one monkey is “<<sumOf[2]<<endl;

system(“pause”);

return 0;
}

Chapter 7 Chips and Salsa

/*3. Chips and Salsa
Write a program that lets a maker of chips and salsa keep track of sales for five different types of salsa: mild, medium, sweet, hot, and zesty. The program should use two parallel 5-element arrays: an array of strings that holds the  five salsa names and an array of integers that holds the number of jars sold during the past month for each salsa type. The salsa names should be stored using an initialization list at the time the name array is created. The program should prompt the user to enter the number of jars sold for each type. Once this sales data has been entered, the program should produce a report that displays sales for each salsa type, total sales, and the names of the highest selling and lowest selling products.

Input Validation: Do not accept negative values for number of jars sold.*/

#include “stdafx.h”
#include<iostream>
#include<string>

using namespace std;

int main()
{
string chipAndSalsa[5]={“mild”,”medium”,”sweet”,”hot”,”zesty”};
int numberOfSold[5],sum=0,hSold,lSold;
string highest, lowest;

//get data
for(int i=0;i<=4;i++)
{
cout<<“\nEnter the number of jars sold during the past month for “<<chipAndSalsa[i]<<” “;
cin>>numberOfSold[i];

while(numberOfSold[i]<0)
{
cout<<“\nNot accept negative number. Please reenter: “;
cin>>numberOfSold[i];
}

sum+=numberOfSold[i];
}

//get heighest
hSold=numberOfSold[0];
highest=chipAndSalsa[0];
for(int x=1;x<4;x++)
{
if(hSold<numberOfSold[x])
{
hSold=numberOfSold[x];
highest=chipAndSalsa[x];
}
}

//get lowest
lSold=numberOfSold[0];
lowest=chipAndSalsa[0];
for(int y=1;y<4;y++)
{
if(lSold>numberOfSold[y])
{
lSold=numberOfSold[y];
lowest=chipAndSalsa[y];
}
}

//display data
for(int z=0;z<5;z++)
{
cout<<“\nThe sales for “<<chipAndSalsa[z]<<” is “<<numberOfSold[z];

}

cout<<“\nThe total sales is “<<sum;
cout<<“\nThe best product is “<<highest;
cout<<“\nThe worst product is “<<lowest<<endl;

system(“pause”);

return 0;
}

Chapter 7 Rainfall Statistics

/*2. Rainfall Statistics
Write a program that lets the user enter the total rainfall for each of 12 months into an array of doubles. The program should calculate and display the total rainfall for the year, the average monthly rainfall, and the months with the highest and lowest amounts.
Input Validation: Do not accept negative numbers for monthly rainfall  figures.*/

#include “stdafx.h”
#include<iostream>

using namespace std;

int main()
{
double rainfall[12],sum=0,avg,highest=0, lowest;
int highestMonth,lowestMonth;

for(int i=0; i<=11;i++)
{
cout<<“\nThe total rainfall for “<<(i+1)<<” month :”;
cin>>rainfall[i];
while(rainfall[i]<0)
{
cout<<“\nError.Enter a positive number.”;
cout<<“\nEnter the total rainfall for “<<(i+1)<<” month :”;
cin>>rainfall[i];
}

sum+=rainfall[i];
}
cout<<“\nThe total rainfall is “<<sum;
avg=sum/12.0;
cout<<“\nThe average monthly rainfall is “<<avg;

for(int x=0;x<11;x++)
{
if(highest<rainfall[x])
{
highest=rainfall[x];
highestMonth=x+1;
}

}
lowest=rainfall[0];
for(int y=0;y<11;y++)
{
if(lowest>rainfall[y])
{
lowest=rainfall[y];
lowestMonth=y+1;
}
}

cout<<“\nThe month that has highest rainfall is”<<highestMonth;
cout<<“\nThe month that has lowest rainfall is”<<lowestMonth<<endl;

system(“pause”);
return 0;
}

Chapter 6 Prime Number List

/*21.isPrime Function
A prime number is a prime number that is only evenly divisible by itself and 1. For example, the number 5 is prime because it can only be evenly divided by 1 and 5. The number 6, however, is not prime because it can be divided evenly by 1, 2, 3, and 6.  Write a function name i sP rim e, which takes an integer as an argument and returns true if the argument is a prime number, or false otherwise. Demonstrate the function in a complete program.

22. Prime Number List
Use the i sPrime function that you wrote in Programming Challenge 21 in a program that stores a list of all the prime numbers from 1 through 100 in a  file.*/

 

#include “stdafx.h”
#include<iostream>
#include<fstream>
#include<string>

using namespace std;

bool isPrime(int);

int main()
{
ofstream outputFile;
outputFile.open(“PrimeNumberList.txt”);

for(int i=1;i<=100;i++)
{
isPrime(i);
while(isPrime(i)==true)
{
outputFile<<i<<endl;
break;
}

}
outputFile.close();
system(“pause”);
return 0;

}

bool isPrime(int n)
{
bool isPrime=true;
for(int i=2;i<n;i++)
{
double x=n%i;
if( x ==0.0)
isPrime=false;
}
return isPrime;
}

Chapter 6 Using Files-Hosptial Report

/*18. Using Files Hospital Report
Modify Programming Challenge 14, Overloaded Hospital, to write the report it creates to a  file.*/

#include “stdafx.h”
#include<iostream>
#include<fstream>
#include<string>

using namespace std;

double calcCharge(int &,double &,double &,double&);
double calcCharge(double&,double&);

int main()
{
ofstream outputfile;
outputfile.open(“Hospital Report.txt”);
char iOrO;
int n=1;
double x=1.0,y=1.0,z=1.0,total=0.0;
cout<<“In-patient or Out-patient? Enter I or O: “<<endl;
cin>>iOrO;
while(iOrO!=’I’&&iOrO!=’O’)
{
cout<<“Error. Enter I or O:”;
cin>>iOrO;
}
if (iOrO==’I’)
total=calcCharge(n,x,y,z);
else
total=calcCharge(y,z);
outputfile<<“Total charge is “<<total<<endl;

system(“pause”);

return 0;
}

double calcCharge(int & day,double & rate,double & medicationCharge,double & chargeForService)
{
cout<<“Enter the number of days spent in the hospital: “<<endl;
cin>>day;

while(day<0)
{
cout<<“Error. Please enter a positive number.Enter the number of days spent in the hospital: “<<endl;
cin>>day;
}

cout<<“Enter the daily rate: “<<endl;
cin>>rate;

while(rate<0)
{
cout<<“Error. Please enter a positive number.Enter the daily rate: “<<endl;
cin>>rate;
}

cout<<“Enter hospital medication charges: “<<endl;
cin>>medicationCharge;

while(medicationCharge<0)
{
cout<<“Error. Please enter a positive number.Enter hospital medication charges: “<<endl;
cin>>medicationCharge;
}

cout<<“Enter charges for hospital services:”<<endl;
cin>>chargeForService;

while(chargeForService<0)
{
cout<<“Error. Please enter a positive number.Enter charges for hospital services:”<<endl;
cin>>chargeForService;
}

double totalCharge=day*rate+medicationCharge+chargeForService;
return totalCharge;

}
double calcCharge(double & medicationCharge,double & chargeForService)
{
cout<<“Enter hospital medication charges: “<<endl;
cin>>medicationCharge;

while(medicationCharge<0)
{
cout<<“Error. Please enter a positive number.Enter hospital medication charges: “<<endl;
cin>>medicationCharge;
}

cout<<“Enter charges for hospital services:”<<endl;
cin>>chargeForService;

while(chargeForService<0)
{
cout<<“Error. Please enter a positive number.Enter charges for hospital services:”<<endl;
cin>>chargeForService;
}

double totalCharge=medicationCharge+chargeForService;
return totalCharge;

}

Chapter 6 Overloaded Hospital

/*14. Overloaded Hospital
Write a program that computes and displays the charges for a patients hospital stay. First, the program should ask if the patient was admitted as an in-patient or an out-patient. If the patient was an in-patient, the following data should be entered:

The number of days spent in the hospital
The daily rate
Hospital medication charges
Charges for hospital services (lab tests, etc.)

The program should ask for the following data if the patient was an out-patient:

Charges for hospital services (lab tests, etc.)
Hospital medication charges

The program should use two overloaded functions to calculate the total charges. One of the functions should accept arguments for the in-patient data, while the other function accepts arguments for out-patient information. Both functions should return the total charges.
Input Validation: Do not accept negative numbers for any data.*/

#include “stdafx.h”
#include<iostream>

using namespace std;

double calcCharge(int &,double &,double &,double&);
double calcCharge(double&,double&);

int main()
{
char iOrO;
int n=1;
double x=1.0,y=1.0,z=1.0,total=0.0;
cout<<“In-patient or Out-patient? Enter I or O: “<<endl;
cin>>iOrO;
while(iOrO!=’I’&&iOrO!=’O’)
{
cout<<“Error. Enter I or O:”;
cin>>iOrO;
}
if (iOrO==’I’)
total=calcCharge(n,x,y,z);
else
total=calcCharge(y,z);
cout<<“Total charge is “<<total<<endl;

system(“pause”);

return 0;
}

double calcCharge(int & day,double & rate,double & medicationCharge,double & chargeForService)
{
cout<<“Enter the number of days spent in the hospital: “<<endl;
cin>>day;

while(day<0)
{
cout<<“Error. Please enter a positive number.Enter the number of days spent in the hospital: “<<endl;
cin>>day;
}

cout<<“Enter the daily rate: “<<endl;
cin>>rate;

while(rate<0)
{
cout<<“Error. Please enter a positive number.Enter the daily rate: “<<endl;
cin>>rate;
}

cout<<“Enter hospital medication charges: “<<endl;
cin>>medicationCharge;

while(medicationCharge<0)
{
cout<<“Error. Please enter a positive number.Enter hospital medication charges: “<<endl;
cin>>medicationCharge;
}

cout<<“Enter charges for hospital services:”<<endl;
cin>>chargeForService;

while(chargeForService<0)
{
cout<<“Error. Please enter a positive number.Enter charges for hospital services:”<<endl;
cin>>chargeForService;
}

double totalCharge=day*rate+medicationCharge+chargeForService;
return totalCharge;

}
double calcCharge(double & medicationCharge,double & chargeForService)
{
cout<<“Enter hospital medication charges: “<<endl;
cin>>medicationCharge;

while(medicationCharge<0)
{
cout<<“Error. Please enter a positive number.Enter hospital medication charges: “<<endl;
cin>>medicationCharge;
}

cout<<“Enter charges for hospital services:”<<endl;
cin>>chargeForService;

while(chargeForService<0)
{
cout<<“Error. Please enter a positive number.Enter charges for hospital services:”<<endl;
cin>>chargeForService;
}

double totalCharge=medicationCharge+chargeForService;
return totalCharge;

}

Chapter 6 Order Status

/*13. Order Status
The Middletown Wholesale Copper Wire Company sells spools of copper wiring for $100 each. Write a program that displays the status of an order. The program should have a function that asks for the following data:
The number of spools ordered.
The number of spools in stock.
Whether there are special shipping and handling charges.
(Shipping  and  handling  is  normally  $10  per  spool.)  If  there  are  special  charges,  the program should ask for the special charges per spool.

The gathered data should be passed as arguments to another function that displays
The number of spools ready to ship from current stock.
The number of spools on backorder (if the number ordered is greater than what is in stock).
Subtotal of the portion ready to ship (the number of spools ready to ship times $100).
Total shipping and handling charges on the portion ready to ship.
Total of the order ready to ship.
The shipping and handling parameter in the second function should have the default argument 10.00.
Input  Validation:  Do  not  accept  numbers  less  than  1  for  spools  ordered.  Do  not accept a number less than 0 for spools in stock or shipping and handling charges.*/

#include “stdafx.h”
#include<iostream>

using namespace std;

void askForData(int &, int &, double & shippingAndHandlingCharge);

void countAndDisplay(int, int, double shippingAndHandlingCharge= 10.0);

int main()
{
int numOfOrder=0, numOfStock=0;
double shippingAndHandlingCharge=0.0;
askForData(numOfOrder, numOfStock, shippingAndHandlingCharge);
countAndDisplay(numOfOrder, numOfStock, shippingAndHandlingCharge);

system(“pause”);
return 0;
}

void askForData(int &order, int & stock, double & shippingAndHandlingCharge )
{
cout<<“\nEnter the number of spools ordered “;
cin>>order;
while(order<1)
{
cout<<“\nError. Please enter a number greater than 1.Enter the number of spools ordered “;
cin>>order;
}

cout<<“Enter the number of spools in stock “;
cin>>stock;
while(stock<0)
{
cout<<“\nError. Please enter a positive number.Enter the number of spools in stock “;
cin>>stock;
}

char sepcialCharge;

cout<<“\nIs there are special shipping and handling charges? Enter Y or N “;
cin>>sepcialCharge;

while(sepcialCharge!=’Y’&&sepcialCharge!=’N’)
{
cout<<“\nError. Please enter Y or N. Y for there are special shipping and handling charges.”
<<“\n N for there are not special shipping and handling charges”;
cin>>sepcialCharge;
}

if(sepcialCharge==’Y’)
{

cout<<“\nEnter the special charges per spool “;
cin>>shippingAndHandlingCharge;
while(shippingAndHandlingCharge<0)
{
cout<<“\nError. Please enter a positive number.Enter the special charges per spool”;
cin>>shippingAndHandlingCharge;
}
}else shippingAndHandlingCharge=10.0;
}
void countAndDisplay(int oder, int stock, double shippingAndHandlingCharge)
{
int readyToShip, backorder=0,subtotalForReady;
double totalSnHChargesForReady, totalOfOderReady;

if(oder<=stock)
{
readyToShip=oder;

}else
{
readyToShip=stock;
backorder=oder-stock;
}

subtotalForReady=readyToShip*100;
totalSnHChargesForReady=readyToShip*shippingAndHandlingCharge;
totalOfOderReady=subtotalForReady+totalSnHChargesForReady;

cout<<“\nThe number of spools ready to ship from current stock:”<<readyToShip;
cout<<“\nThe number of spools on backorder “<<backorder;
cout<<“\nSubtotal of the portion ready to ship “<<subtotalForReady;
cout<<“\nTotal shipping and handling charges on the portion ready to ship “<<totalSnHChargesForReady;
cout<<“\nTotal of the order ready to ship “<<totalOfOderReady<<endl;

}