REC02_Problems_Solutions

REC02_Problems_Solutions - 1 14:440:127– Introduction to...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 14:440:127– Introduction to Computers for Engineers Recitation 02 Problems Solutions Rutgers University, Spring 2011 Instructor - Brenda V. Cortez 1. Euler’s number, e, is a mathematical constant with the value 2.71828. Write a program that defines Euler’s number as a local variable and print it to the screen. #include <iostream> using namespace std; // don’t forget this if you are using cout or cin // always put this in your programs int main(){ float e = 2.71828; cout << "Euler’s number e has the value of " << e << endl; return 0; } 2. Now, write a program containing two functions, main and printE and a static variable containing Euler’s number. The main function should call printE, which is a function that will print the variable containing Euler’s number. #include <iostream> // don’t forget this if you are using cout or cin using namespace std; // always put this in your programs static float e = 2.71828; // MUST include function declaration void printE(); int main(){ // CALL to function printE printE(); return 0; } void printE(){ cout << "Euler’s number is " << e << endl; } 3. Develop a program that has a function that prints each letter in your name. Your main function should make the calls to each function, such that your name is printed in a single like, i.e. Brenda. Hint: If your name is Steve, you will have 5 functions, each printing a single character of your name. Also remember that strings are enclosed in double quotes, but characters are enclosed in single quotes. 2 #include <iostream> using namespace std; void printB(); void printr(); void printe(); void printn(); void printd(); void printa(); // don’t forget this if you are using cout or cin // always put this in your programs int main(){ printB(); printr(); printe(); printn(); printd(); printa(); return 0; } void printB(){ cout << } void printr(){ cout << } void printe(){ cout << } void printn(){ cout << } void printd(){ cout << } void printa(){ cout << } ‘B’; ‘r’; ‘e’; ‘n’; ‘d’; "a\n"; 4. Write a program containing a function that asks the user to enter a radius, calculates the area of a circle, and prints the answer to the user. #include <iostream> using namespace std; void circArea(); // don’t forget this if you are using cout or cin // always put this in your programs 3 int main(){ circArea(); return 0; } void circArea(){ float pi = 3.1415, r; cout <<"Enter radius: "; cin >> r; cout <<"Circle Area with radius = " << r << " is " << pi*(r*r) << endl; } 5. Write a program using an if/else statement that prompts the user for a number of type int. If the number entered is greater than zero, send a message to the user saying: “You entered a number greater than zero.”. Otherwise, if the number is not greater than zero, then send a message to the user saying: “You entered a number that is not greater than zero.” #include <iostream> using namespace std; int main(){ int num; cout << "Enter a number: "; cin >> num; if(num > 0) cout << "You entered a number greater than zero.\n"; else cout << "You entered a number that is not greater than zero.\n"; return 0; } 6. Create a program that prompts the user to enter a letter. Use an if/else statement to check if the letter being entered matches one of the letters in your name. If it does, send a message to the user saying their character matches whatever letter in your name, but if it doesn’t then send a message to the user saying the opposite. Hint: My name is Brenda, so if a user enters a or A ,either letter matches a letter in my name. #include <iostream> using namespace std; int main(){ char letter; cout << "Enter a letter: "; cin >> letter; 4 if(letter == ‘B’ || letter == ‘b’) cout << "Your letter matches B/b\n"; else if (letter == ‘R’ || letter == ‘r’) cout << "Your letter matches R/r \n"; else if (letter == ‘E’ || letter == ‘e’) cout << "Your letter matches E/e \n"; else if (letter == ‘N’ || letter == ‘n’) cout << "Your letter matches N/n \n"; else if (letter == ‘D’ || letter == ‘d’) cout << "Your letter matches D/d \n"; else if (letter == ‘A’ || letter == ‘a’) cout << "Your letter matches A/a \n"; else cout << "Your letter doesn’t match any letter in my name.\n"; } 7. Write a program that prompts a user for a decimal number between 0 and 5. Use an if/else statement to determine is the number is greater than 2.0 and less than 4.0. If the number is within the specified range, send to the user the message “Success!”. If the number is between 0 and 5, send to the user the message “Almost!”, and if the number entered in not between 0 and 5, send to the user the message “Fail!”. #include <iostream> using namespace std; int main(){ float num; cout << "Enter a decimal number between 0 and 5: "; cin >> num; if (num > 2.0 && num < 4.0) cout << "Success!" << endl; else if (num <= 0 || num >= 5.0) cout << "Fail!\n"; else cout << "Almost!" << endl; return 0; } 8. Write a program that allows the user to enter a number (integer/decimal) and if the number 5 is greater than zero, returns to the screen the square of that number. If the number is not greater than zero, send an error message to the screen. Hint: If I enter 5, I should see on my screen the result of 5*5, so I should see 25 as the result. #include <iostream> using namespace std; int main(){ float num; cout << "Enter a number (decimal/integer): "; cin >> num; if (num > 0) cout << num*num << endl; else cout << "Error: You entry is not greater than zero.\n"; return 0; } 9. Write a program that asks the user to enter their gender (F or M) without case sensitivity, and their age. Use an if/esle statement to write the program that would output the following message based on the conditions stated: • F, age less than or equal to 18 but greater than 13, “Your are a female High School student.” • F, age greater than 18 but less than or equal to \verb23!, “Your are a female College student.” • F, age greater than 23, “Your are a female College graduate.” • M, age less than or equal to 18, “Your are a male High School student.” • M, age greater than 18 but less than or equal to 23, “Your are a male College student.” • M, age greater than 23, “Your are a male College graduate.” #include <iostream> using namespace std; int main(){ char gender; int age; cout << "Enter your gender (F/M): "; cin >> gender; cout << "Enter your age: "; cin >> age; if ((gender == ‘F’ || gender == ‘f’) && (age > 13 && age <= 18)) cout << "You are a female High School student." << endl; 6 else if ((gender == ‘F’ || gender == ‘f’) && ((age > 18) && (age <= 23))) cout << "You are a female College student.\n"; else if ((gender == ‘F’ || gender == ‘f’) && (age > 23)) cout << "You are a female College graduate.\n"; else if ((gender == ‘M’ || gender == ‘m’) && (age > 13 && age <= 18)) cout << "You are a male High School student." << endl; else if ((gender == ‘M’ || gender == ‘m’) && ((age > 18) && (age <= 23))) cout << "You are a male College student.\n"; else if ((gender == ‘M’ || gender == ‘m’) && (age > 23)) cout << "You are a male College graduate.\n"; return 0; } 10. Simplify the solution to Problem 9 by using nested if/else statements. Hint: Check if the user if Female or Male first. If the user is a female, then you can check the age. This will eliminate checking if the user is a Female every single time. #include <iostream> using namespace std; int main(){ char gender; int age; cout << "Enter your gender (F/M): "; cin >> gender; cout << "Enter your age: "; cin >> age; // Is user a female? if (gender == ’F’ || gender == ’f’){ if (age > 13 && age <= 18) cout << "You are a female High School student." << endl; else if (age > 18 && age <= 23) cout << "You are a female College student.\n"; else if (age > 23) cout << "You are a female College graduate.\n"; } // Or is user a male? else 7 if (gender == ’M’ || gender == ’m’){ if (age > 13 && age <= 18) cout << "You are a male High School student." << endl; else if (age > 18 && age <= 23) cout << "You are a male College student.\n"; else if (age > 23) cout << "You are a male College graduate.\n"; } return 0; } 11. Rewrite your solution to Problem 10 using a switch statement. Hint: You can use the gender values as the case values and then implement the age conditions using if/else statements. #include <iostream> using namespace std; int main(){ char gender; int age; cout << "Enter your gender (F/M): "; cin >> gender; cout << "Enter your age: "; cin >> age; switch (gender){ case ‘F’: case ‘f’: if (age > 13 && age <= 18) cout << "You are a female High School student." << endl; else if (age > 18 && age <= 23) cout << "You are a female College student.\n"; else if (age > 23) cout << "You are a female College graduate.\n"; break; case ‘M’: case ‘m’: if (age > 13 cout << "You else if (age > 18 cout << "You && age <= 18) are a male High School student." << endl; && age <= 23) are a male College student.\n"; 8 else if (age > 23) cout << "You are a male College graduate.\n"; break; } return 0; } 12. Write a program using a switch statement that prompts the user to enter his/her recitation section number, i.e. 1, 2, ... 8, to see when EXAM01 will be. If the user enters section numbers 2 or 4, send a message to the user telling them their EXAM01 has been changed to DSV on Thursday, February 17. Those students who enter section numbers 5, 6, 7, or 8, should get a message about their EXAM01 being changed to DSV on Friday, February 18. Sections numbered 3 or 1, will have their EXAM01 in DSV on Monday, February 21. Any other section number is invalid. #include <iostream> using namespace std; int main(){ int section; cout << "Enter your section number (1-8): "; cin >> section; switch (section){ case 2: case 4: cout << "Your EXAM01 will be on Thursday, February 17th in DSV.\n"; break; case 5: case 6: case 7: case 8: cout << "Your EXAM01 will be on Friday, February 18th in DSV.\n"; break; case 3: case 1: cout << "Your EXAM01 will be on Monday, February 21st in DSV.\n"; break; defaut: cout << "You entered an invalid section number.\n"; break; } return 0; } 9 13. Create a program to prompt the user to enter the number of snickers chocolate bars he/she wants. The input will be a number. Use a switch statement to determine the bill amount, where: • 1 bar is $0.75 • 2 bars are $1.25 • 3 bars are $1.65 • more than 3 bars is $1.65 + $0.30(numberOrdered-3) #include <iostream> using namespace std; int main(){ int bars; cout << "How many snickers chocolate bar do you want to buy: "; cin >> bars; switch (bars){ case 1: cout << "Bill Amount: $0.75\n"; break; case 2: cout << "Bill Amount: $1.25\n"; break; case 3: cout << "Bill Amount: $1.65\n"; break; default: cout << "Bill Amount: $" << (1.65 + (0.30 * (bars - 3)))<< endl; break; } return 0; } 14. In Problem 12, default was used in the switch statement to handle sections that were not 1-8. Remove this part, and instead implement a while loop that will validate the input from the user. You will see that validating the input makes the default part unnecessary. #include <iostream> using namespace std; int main(){ int section; cout << "Enter your section number (1-8): "; 10 cin >> section; while(section < 1 || section > 8){ cout << "Incorrect section number. Please try again.\n"; cout << "Enter your section number (1-8): "; cin >> section; } switch (section){ case 2: case 4: cout << "Your EXAM01 will be on Thursday, February 17th in DSV.\n"; break; case 5: case 6: case 7: case 8: cout << "Your EXAM01 will be on Friday, February 18th in DSV.\n"; break; case 3: case 1: cout << "Your EXAM01 will be on Monday, February 21st in DSV.\n"; break; } return 0; } 15. Now modify your code for your solution to Problem 13, so that you validate the input from the user as well. At the moment, you probably created a program that allows a negative number or zero number of snickers chocolate bars, which is invalid. Instead of using default, implement a while statement to validate the input. #include <iostream> using namespace std; int main(){ int bars; cout << "How many snickers chocolate bar do you want to buy: "; cin >> bars; while(bars <= 0){ cout << "You entered in invalid number of bars to buy.\n"; cout << "How many snickers chocolate bar do you want to buy: "; cin >> bars; } 11 switch (bars){ case 1: cout << "Bill Amount: $0.75\n"; break; case 2: cout << "Bill Amount: $1.25\n"; break; case 3: cout << "Bill Amount: $1.65\n"; break; default: cout << "Bill Amount: $" << (1.65 + (0.30 * (bars - 3)))<< endl; break; } return 0; } NOTE: If you were to run your executable for Problem 13 or Problem 15, and you entered the letter s instead of a number, your program would go into an infinite loop. Try it and when your program executed the infinite loop, press Ctrl+C to “kill it”. In order to handled this problem, you need to use stream manipulator, which will not be covered in this class. However, you should know that you can use the clear and ignore functions that are part of iostream. More specifically, ignore can be used to read in and discard some specified number of characters or the default of one character. This means that if you type in s, which is incorrect for number of bars, you could use ignore to discard this character. However, before you can continue, you have to reset input/output so that you can extract some new entry from the user. You simply have to type in clear(); and this will reset the stream. Following is a modified while statement, that is used to validate the input from the user using clear and ignore. int bars; cout << "How many snickers chocolate bar do you want to buy: "; cin >> bars; while (!(cin >> bars) || (bars <= 0)) { cin.clear(); cin.ignore(); cout << "You entered in invalid number of bars to buy.\n"; cout << "How many snickers chocolate bar do you want to buy: "; } 16. Write a for-loop that prints numbers from 1 to 7 in increments of 3. #include <iostream> using namespace std; // don’t forget this if you are using cout or cin // always put this in your programs 12 int main(){ for(int k = 1; k <= 7; k+=3) cout << k << endl; return 0; } 17. Now modify your code above, so that the number are printed in the reverse order. #include <iostream> using namespace std; // don’t forget this if you are using cout or cin // always put this in your programs int main(){ for(int k = 7; k >= 1; k-=3) cout << k << endl; return 0; } 18. Write a program using a for-loop that prompts the user for a number (int) and prints 5*number. For instance, if the user enters 3, you should print 5, 10, and 15; each on a separate line. #include <iostream> using namespace std; // don’t forget this if you are using cout or cin // always put this in your programs int main(){ int num; cout << "Enter a number: "; cin >> num; for(int k = 1; k <= num; k++) cout << 5*k << endl; return 0; } 19. Convert your solution to Problem 18 to a while-loop solution. #include <iostream> using namespace std; // don’t forget this if you are using cout or cin // always put this in your programs int main(){ int num; int k = 1; cout << "Enter a number: "; cin >> num; while(k <= num){ cout << 5*k << endl; k++; 13 } return 0; } 20. Change your for-loop solution to Problem 17 to a while-loop solution. #include <iostream> using namespace std; // don’t forget this if you are using cout or cin // always put this in your programs int main(){ int k = 7; while(k >= 1){ cout << k << endl; k-=3; } return 0; } 21. Write a program, using a for-loop that prompts a user (student) to enter his/her exam scores for three exams and returns the average of the three scores. #include <iostream> using namespace std; // don’t forget this if you are using cout or cin // always put this in your programs int main(){ float sum = 0, exam; for(int k=1; k <= 3; k++){ cout << "Enter your exam score:"; cin >> exam; sum += exam; } cout << "The average of your three scores is: " << sum/3.0 << endl; return 0; } ...
View Full Document

Ask a homework question - tutors are online