Sie sind auf Seite 1von 4

Programming Fundamentals Spring 2012

(BS-CS-F11 Morning-B & Afternoon-B)

Assignment # 4
Submission Deadline: Wednesday, 2nd May, 2012 (till 6:00 PM) Submission Folders: \\printsrv\Teacher Data\Ahmad Ghazali\PF - Spring 2012\A4 Morning \\printsrv\Teacher Data\Ahmad Ghazali\PF - Spring 2012\A4 Afternoon

Instructions This is an individual assignment. You are NOT allowed to work/submit in form of group. Absolutely NO collaboration is allowed. Any traces of plagiarism/cheating would result in an Fgradeinthiscourse. DoNOTcopyevenasinglelineofcodefromanyotherpersonorbookorInternetoranyother source. Thisassignmentneedstobesubmittedinsoftform.SeeSubmissionProcedureattheend. LatesubmissionswillNOTbeaccepted,inanycase. ClearlymentionyourName,RollNumberandSectionincommentsatthetopofeachCPPfile.

This assignment consists of two parts. You are required to solve and submit the questionsgiveninPartAtilltheabovementioneddeadline.QuestionsinPartBare foryourownpracticeandyouareNOTrequiredtosubmitthem.

PartA
1. Write a C++ program for the Programming Challenge # 11 (Star Search) at Page 368 of your textbook. Also write a driver main function to test the working of functions that you implement.

2. Write a C++ program for the Programming Challenge # 14 (Overloaded Hospital) at Page 369 of your textbook. Also write a driver main function to test the working of functions that you implement.

3. Write a C++ program for the Programming Challenge # 22 at Page 371 of your textbook. You will also need to solve Programming Challenge # 21 in order to solve this problem. (The name of the output file should be primes.txt)

Page 1 of 4

4. Write a function named convertTime() that has an integer parameter named seconds and three integer reference parameters named hours, mins, and secs. The function is to convert the passed number of seconds into an equivalent number of hours, minutes, and seconds. Using the reference parameters, the function should alter the arguments in the calling function. Also write a driver main function to test the convertTime() function. 5. For research purposes and to better help students, the admissions office of PUCIT wants to know how well female and male students have performed in their degree programs. You receive a file that contains female and male student CGPAs. The letter code f is used for female students and m for male students. Every file entry consists of a letter code followed by a CGPA. Each line has one entry (see sample input file shown below). The number of entries in the file is unknown. The name of the input file should be cgpa.txt. Write a program that computes and outputs the average CGPA for both female and male students. Format your results rounded off to two decimal places (see sample output file shown below). The name of the output file produced by your program should be summary.txt.
Sample input file: cgpa.txt m 3.6 f 3.4 f 2.15 m 2.98 m 4 f 3.53 m 2.05 Sample output file: summary.txt Average CGPA of 4 male students: 3.16 Average CGPA of 3 female students: 3.03

Your program should have the following functions (apart from the main function): boolopenFiles(ifstream&,ofstream&); 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. This function will return true if both files are successfully opened, otherwise it should return false. If both files are not opened successfully, the program should be terminated using the exit function. voidinitializeVariables(int&,int&,double&,double&); This function initializes the four variables which are passed into it by reference. These variables will be representing: count of male students, count of female students, sum of male CGPAs and sum of female CGPAs. voidsumGrades(ifstream&,double&,double&,int&,int&); This function reads the data from the input file (whose handle it receives as the first parameter), and determines and stores the sum of male CGPAs, sum of female CGPAs, count of male students, and count of female students in the next four reference parameters.

Page 2 of 4

voidaverageGrade(double&,double&,double,double,int,int); This function finds the average CGPA for female and male students and returns them in the first two reference parameters. Next four parameters are used to receive the sum of male CGPAs, sum of female CGPAs, count of male students, and count of female students. voidprintResults(ofstream&,int,double,int,double); This function outputs the relevant results (see the sample output file) to the output file (whose handle it receives as the first parameter). Next four parameters are used to receive the count of male students, average CGPA of male students, count of female students, and average CGPA of female students, respectively.

PartB
6. A clever and simple method of preparing to sort dates into ascending (increasing) or descending (decreasing) order is to convert a date in the form day/month/year into an integer number with the formula:

date=year10000+month100+day
For example, using this formula, the date 14/8/1947 converts to the integer 19470814, and the date 26/04/2012 converts to the integer 20120426. Sorting the resulting integer numbers puts dates into the correct order automatically. Using this formula, write a function named convertDays() that accepts a day, month, and year; converts the passed data into a single date integer; and returns the integer to the calling function. Write a driver main function which tests the function convertDays() by passing various data to it and displaying the returned values.

7. Write C++ programs for the following Programming Challenges at the end of Chapter 6: Programming Challenge # 4,5,8,9,10,12,15,16,23

8. Write C++ programs for the following Programming Challenges at the end of Chapter 6: Programming Challenge # 1,2,3,6,7,13,17,18,19,20

9. Write C++ programs for the following Programming Challenge at the end of Chapter 6: Programming Challenge # 24

Page 3 of 4

10. A palindrome is a number or a text phrase that reads the same backwards as forwards. For example, each of the following integers is a palindrome: 12321, 55, 45554 and 1216121. Write a C++ function isPalindrome() which receives an integer and determines whether it is a palindrome or not. This function should return true if the parameter is a palindrome, otherwise it should return false. Also write a driver main function to test the working of this function.

SubmissionProcedure
Youarerequiredtosubmitthisassignmentinsoftcopyformat(inthespecifiedfolder).Put thefive.CPPfiles(q1.cpp,q2.cpp,,q5.cpp)inafolder(doNOTincludeanyotherfilesin your submission). The name of the folder should be your complete roll number (like BCSF11M234),thencompressthatfolder,andcopythe.RARfileinthespecifiedsubmission folderforyoursection. Pleasenote: DoNOTuseanyglobalvariablesinyourprograms. These good programming practices will also have their (significant) weightage in the markingofthisassignment: o Commentyourcodeintelligently.Uncommentedcodewillnotbegivenanycredit. o Indentyourcodeproperly. o Usemeaningfulvariableandfunctionnames. o Usemeaningfulpromptlines/labelsforinput/output. Although, you need to submit five questions in this assignment, but only two or three questionswillbemarked.Since,youdonotknowwhichthosetwo/threequestionsare, therefore,youareadvisedtosolveandsubmitallfivequestions.

GOODLUCK!
Remember:Honestyalwaysgivesfruit(nomatterhowfrighteningistheconsequence);and Dishonestyisalwaysharmful(nomatterhowhelpingitmayseeminacertainsituation)!

Page 4 of 4