Você está na página 1de 5

For research purposes and to better help students, the admission office of your local university wants to know

how well female and male students perform in certain courses. You will receive a file that contains female and male students GPA's for certian courses. Due to confidentiality, the letter code f is used for female students and m is used for male students. Every file entry consists of a letter code followed by a GPA. Each line has one entry. The number of entries in the file is unknown. Write a program that computes and outputs the average GPA for both female and male students. Format your results to two decimal places. Your program should use the following functions. a. Function openFiles: This function opens the input and output files, and sets the output of the floating-point numbers to two decimal places in a fixed decimal format with a decimal point and trailing zeros. b. Function initialize: This function initializes variables such as countFemale, countMale, sumFemaleGPA, and sumMaleGPA. c. Function sumGrades: This function finds the sum of the female and male students GPA's. d. Function averageGrade: This function finds the average GPA for female and male students. e. Function printResults: This function outputs the relevant results. f. There can be no global variables. Use the appropriate parameters to pass information in and out of functions,

#include <iostream> #include <fstream> #include <iomanip> #include <string> using namespace std; void void void void void openFiles(ifstream&, ofstream&); initialize(ifstream&, ofstream&); sumGrades(float&, float&, ifstream&, ofstream&, int&, int&); averageGrade(int&, int&, float&, float&, ifstream&, ofstream&); printResults(int&, int&, float&, float&, ifstream&, ofstream&, float&, float&);

int main() { ifstream in; ofstream out; openFiles(in, out); return 0; } void openFiles(ifstream& in, ofstream& out) { in.open("GPA.dat"); out.open("Out.dat"); out << fixed << showpoint; out << setprecision(2); initialize(in, out); } void initialize(ifstream& in, ofstream& out) { char gender; float GPA; string line; int countFemale = 0; int countMale = 0; float sumFemaleGPA = 0; float sumMaleGPA = 0;

while (!in.eof()) { in >> gender >> GPA; if (gender == 'f') { countFemale++; sumFemaleGPA = sumFemaleGPA + GPA; } if (gender == 'm') { countMale++; sumMaleGPA = sumMaleGPA + GPA; } } sumGrades(sumFemaleGPA, sumMaleGPA, in, out, countFemale, countMale); averageGrade(countFemale, countMale, sumFemaleGPA, sumMaleGPA, in, out); } void sumGrades(float& sumFemaleGPA, float& sumMaleGPA, ifstream& in, ofstream& out, int& countFemale, int& countMale) { out << "Total Sum of GPA Grades: " << sumFemaleGPA + sumMaleGPA << endl; } void averageGrade(int& countFemale, int& countMale, float& sumFemaleGPA, float& sumMaleGPA, ifstream& in, ofstream& out) { float averageFemaleGPA = sumFemaleGPA / countFemale; float averageMaleGPA = sumMaleGPA / countMale; printResults(countFemale, countMale, sumFemaleGPA, sumMaleGPA, in, out, averageFemaleGPA, averageMaleGPA); } void printResults(int& countFemale, int&countMale, float& sumFemaleGPA, float& sumMaleGPA, ifstream& in, ofstream& out, float& averageFemaleGPA, float& averageMaleGPA) { out << "Total Females: " << countFemale << endl; out << "Total Males: " << countMale << endl; out << "Female Sum GPA: " << sumFemaleGPA << endl; out << "Male Sum GPA: " << sumMaleGPA << endl; out << "Female Average GPA: " << averageFemaleGPA << endl; out << "Male Average GPA: " << averageMaleGPA; in.close(); out.close(); } The cost to become a member of a fitness center is as follows: (a)Senior citizens discount is 30% (b)If membership is bought and paid for 12 or more months, the discounts is 15% (c)If more than 5 personal training sessions are bought and paid for, the discount on each session is 20%. Write a menu driven program that determines the cost of a new membership. Your program must contain a function that displays the general information about the fitness center and its charges; a function to get all of the necessary information to determine the membership cost; and a function to determine the membership cost. (Do not use any global variables)

//Header files #include "stdafx.h" #include <iostream> using namespace std; //function prototypes void displayInfo(); int readData(); int memCost(int, int, int); //function main

void main() { int cost; displayInfo(); cost = readData(); cout<<"\nThe cost of the membership is : $"<<cost<<endl; system("pause"); }//end main //function to display information void displayInfo() { cout<<"\n\tFitness Center - General information\n"<< cout<<"Membership for month: $200\n"; cout<<"Membership for personal training: $100\n"; cout<<"Discount for Senior citizens: 30%\n"; cout<<"Discount on purchasing 12 or more months: 15%\n"; cout<<"Discount on purchasing 5 or more personal training: 20%\n\n"; }//end display //function to read data int readData() { int srCitizen, proChoice,months=0,sessions=0,cost; cout<<"Enter your purchase details...\n"; cout<<"Enter 1 if you are senior citizen or 0 if not : "; cin>>srCitizen; if(srCitizen != 1) srCitizen =0; cout<<"\nChoose your fitness program...\n"; cout<<"1. Monthly membership\n"; cout<<"2. Personal Training\n"; cout<<"\nEnter your choice : "; do { cin>>proChoice; if(proChoice<1||proChoice>2) cout<<"Enter a valid choice : "; }while(proChoice<1||proChoice>2); if(proChoice==1) { cout<<"Enter number of months you want : "; cin>>months; } if(proChoice==2) { cout<<"Enter number of sessions you want : "; cin>>sessions; } cost = memCost(srCitizen,months,sessions); return cost; }//end read data //function to calculate membership int memCost(int sr, int m, int s) { int cost; //calculation if personal training is purchased if(m==0)

{ if(s<5) cost = s*100; else cost = s*100*0.8; } //calculation if monthly is purchased if(s==0) { if(m<12) cost = m*200; else cost = s*100*0.85; } //giving discount to senior citizens if(sr==1) cost=cost*0.7; return cost; }//end function calculate

#8
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include<iostream> #include<cmath> using namespace std; double double double double distance (int p, int q, int r, int s); radius (int p, int q, int r, int s); circumference (double r1); area (double r1);

int main () { int x1, x2, y1, y2; double r, d, c, a, p, s, q; cout<<"Enter the center of the circle: "<<endl; cin>>x1>>y1; cout<<"Enter an outer point on the circle: "<<endl; cin>>x2>>y2; d= distance (x1, y1, x2, y2); r= radius (x1, y1, x2, y2); c= circumference (r); a= area (r); p=0; q=0; s=0; cout<< "Distance between points: "<<d<<endl; cout<< "Radius of circle: "<<r<<endl; cout<< "Circumference of the circle: "<<c<<endl;

33 cout<< "Area of circle: "<<a<<endl; 34 system ("pause");} 35 double distance (int p, int q, int r, int s) //this 36 should be double distance (int p, int q, int r, int s) 37 { 38 double d1; 39 d1 = sqrt(pow((double)r-p,2) + 40 pow((double)s-q, 2)); 41 return d1; 42 } 43 double radius (int p, int q, int r, int s) 44 { 45 double r1; 46 r1= distance (p,q,r,s); 47 return r1; 48 } 49 double circumference (double r1) 50 { 51 double c1; 52 c1= 2*3.1416*r1; 53 return r1; 54 } 55 double area (double r1) 56 { 57 double a1; 58 a1= 3.1416*r1*r1; 59 return a1; 60 }

Você também pode gostar