part 1 you used one main program to get the students grades
part #1 you used one main program to get the students\' grades and to compute and display the letter grade. Modify the program to include two functions a) ComputeAvg which will be called to compute and return the student\'s average and b) LetterGrade which will be called to compute and return the letter grade.
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int i,s1, s2, s3, avg, a;
for (avg=1 ; avg<=3; i++)
for(i = 1 ; i <= 5; i++) // loops for 5 times
{
cout << \"\ What are the three test scores for student #\" <<i <<\"? \";
cin >> s1 >> s2 >> s3; // read score1, score2, score3
avg = round((s1 + s2 + s3) / 3.0); // calculate average
if (avg >= 90 && avg <= 100) {
cout << \"The average is \"<<avg<<\" and the grade is A\ \";
}
else if (avg >= 80 && avg < 90) {
cout << \"The average is \"<<avg<<\" and the grade is B\ \";
}
else if (avg >= 70 && avg < 80) {
cout << \"The average is \"<<avg<<\" and the grade is C\ \";
}
else if (avg >= 65 && avg < 70) {
cout << \"The average is \"<<avg<<\" and the grade is D\ \";
}
else if (avg < 65) {
cout << \"The average is \"<<avg<<\" and the grade is F\ \";
int num;
cin >> num;
int *arr = new int[num];
for(int i = 0; i < num; ++i){
}
double avg = 0;
for(int i = 0; i < num; ++i){
avg += arr[i];
}
avg /= num;
cout << \"The class average is \" << avg << endl;
return 0;
}
}
return 0;
}
part# 2
Write a program to display the number of ways to make change for a dollar using nickels, dimes, quarters and fifty-cent-pieces and display the total number of ways. Include appropriate headings in the printout.
Sample RUN
Nickel Dimes Quarters Fifty-cents
1 2 1 1
There are ? ways to make change for a dollar using using nickels, dimes, quarters and fifty-cents.
Part#3
Write a program to display the following triangular pattern of stars using nested loops only. Ask the user for the number of rows. This example has 6 rows. (Hint- use the setw(i) where i is a variable )
Part 1. Display the tree.
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * * * * * *
Part 2. Display the tree as above together with a base. Use a separate pair of nested loops for the base.
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * * * * * *
****
****
****
Part#4
A) Write a program to display the following 6 by 6 checker board.
B)
Write a program to display the graph of the equation y=x squared, the x- axis, the y-axis and the corresponding y values as shown.
C)Modify extra credit #3 to display a multi-tree (click on the executable file below to see a sample run. Your program must use two functions 1) displayrunk - which displays the trunk section of the tree and 2) displayBase to display the base of the tree. Ask the user to select one of the 6 special characters for the trunk.
D) Write a program to display this pattern of stars using nested loops.
E)Write a program that asks the user for an integer and a base and call on a function to convert the value using that base. The function can be called using any base. 1) Call the function with a base of 2 to convert the value to binary and 2) call on the function to convert the value to hexa-decimal (base 16). See attached sample RUN.
Part#5a)
A prime number is an integer(except 1) whose factors are 1 and itself.
Write a program to examine all of the integers from 2 to 100 if they are prime or not prime. Display each of the numbers and the word Prime if it is prime or all its factors (except 1 and itself) if it is not prime. Here is what the RUN should look like:
2 Prime
3 Prime
4 - Not prime. Factors are: 2
5 Prime
6 - Not prime. Factors are: 2, 3
(etc)
Requirements: Your program must use two functions 1) IsPrime which receives the integer to be checked and returns a boolean value (true if prime or false if not prime) when called, and 2) TheFactorsAre which display the factors of the integer it receives if it not prime.
B)
A perfect number - is one whose sum of factors (except itself) equals the number.
Write a program to ask the user to enter an integer and to test if it is a perfect number and display the factors and their sum. Your program must include two functions 1) checkPerfect and 2) displayFactors. Use function checkPerfect to check if the number is perfect. It should return TRUE or FALSE if it is a perfect or not perfect number respectively. You will then use function displayFactors to display the factors of the number as shown below.
The first two perfect numbers are 6 and 28. (e.g) 1+2+3=6 and 1+2+4+7+14=28
Part 1: Do it for one number and ask if the user wishes to try again.
Ask the user to enter an integer.
Sample RUN.
Enter an integer and I will tell you if it is a perfect number 32
1 + 2 + 4 + 8 + 16 = 31
Nope! 32 is not perfect
Would you like to try another number? y
Enter an integer and I will tell you if it is a perfect number 28
1 + 2 + 4 + 7 + 14 = 28
Yes! 28 is a perfect number
Would you like to try another number? n
Part 2:
After you have gotten the above program to work, modify it to display all the perfect integers up to 500. There are only three. What are they? 6 , 28 and ?
Sample RUN:
Enter an integer and I will check and display all the perfect numbers up to that number. What is your number 500
The perfect numbers up to 500 are:
6 = 1 + 2 +3
28 = 1 + 2 + 4 + 7 + 14
(etc)
You are to submit only part 2.
Part 3: Not to be submitted - can you find the first 5 perfect numbers?
#include <iostream>
using namespace std;
int main(){
int start = 1, end = 500;
cout << \"Perfect numbers between 1 and 500 are: \";
for(int i = start; i <= end; ++i){
int sum = 0;
int num = i;
for(int j = 1; j < num; ++j){
if(num % j == 0){
sum += j;
}
}
if(sum == num)
cout << num << \" \";
}
cout << endl;
return 0;
}
Solution
Here is the modularized code for you for problem 1:
#include <iostream>
#include <math.h>
using namespace std;
//a) ComputeAvg which will be called to compute and return the student\'s average.
int computeAvg(int s1, int s2, int s3)
{
return round((s1 + s2 + s3) / 3.0);
}
//b) LetterGrade which will be called to compute and return the letter grade.
char letterGrade(int avg)
{
if (avg >= 90 && avg <= 100)
return \'A\';
else if (avg >= 80 && avg < 90)
return \'B\';
else if (avg >= 70 && avg < 80)
return \'C\';
else if (avg >= 65 && avg < 70)
return \'D\';
else
return \'F\';
}
int main()
{
int i,s1, s2, s3, avg, a;
for (avg=1 ; avg<=3; i++)
for(i = 1 ; i <= 5; i++) // loops for 5 times
{
cout << \"\ What are the three test scores for student #\" <<i <<\"? \";
cin >> s1 >> s2 >> s3; // read score1, score2, score3
avg = computeAvg(s1, s2, s3); // calculate average
char grade = letterGrade(avg);
cout << \"The average is \"<<avg<<\" and the grade is \"<<grade<<\".\ \";
int num;
cin >> num;
int *arr = new int[num];
for(int i = 0; i < num; ++i){
}
double avg = 0;
for(int i = 0; i < num; ++i){
avg += arr[i];
}
avg /= num;
cout << \"The class average is \" << avg << endl;
}
return 0;
}




