1. Write a C++ program to implement this task. (50 pts) Percentage marks attaine
ID: 3722588 • Letter: 1
Question
1. Write a C++ program to implement this task. (50 pts) Percentage marks attained by a student in three exams are to be entered into a computer, using the keyboard. An indication of Pass or Fail is given out after the three marks are entered. The criteria for passing are as follows: A student passes if all three examinations are passed. Additionally a student may pass if only one subject is failed and the overall average is greater than or equal to 50. The pass mark for an individual subject is 40. Make use of a named constant for defining the pass mark of 40.
SAMPLE RUN 1 (user entered values are in blue) Enter the 3 marks, separated by a space: 75 85 90 You entered: Grade_1 = 75 Grade_2 = 85 Grade_3 = 90 Average Grade: 83.3333 You Passed :-)
SAMPLE RUN 2 (user entered values are in blue) Enter the 3 marks, separated by a space: 35 40 87 You entered: Grade_1 = 35 Grade_2 = 40 Grade_3 = 87 Average Grade: 54 You Passed :-)
SAMPLE RUN 3 (user entered values are in blue) Enter the 3 marks, separated by a space: 35 40 50 You entered: Grade_1 = 35 Grade_2 = 40 Grade_3 = 50 Average Grade: 41.6667 You Failed :-(
SAMPLE RUN 4 (user entered values are in blue) Enter the 3 marks, separated by a space: 35 38 39 You entered: Grade_1 = 35 Grade_2 = 38 Grade_3 = 39 Average Grade: 37.3333 You Failed :-(
2. You have to use a switch statement in this program. Write a C++ program which will compute the area of a square (area = side2) or a triangle (area = (base * height)/2) after prompting the user to type the first character of the figure name (t or s). If the user choses triangle, prompt them to enter the base and height and then compute and output the area of the triangle. If the user selects a square prompt them to enter the length of side of the square, and compute the area and out the area of the square. If the user enters a letter other than t or s, your program should print "Invalid entry" and end.
SAMPLE RUN 1 (user entered values are in blue) Enter t to compute area of triangle or s for square: a Invalid entry Done
SAMPLE RUN 2 (user entered values are in blue) Enter t to compute area of triangle or s for square: t You entered t for triangle Enter base: 4.5 Enter height: 6.5 Area of triangle of base 4.5 and height 6.5 is 14.625 Done
SAMPLE RUN 3 (user entered values are in blue) Enter t to compute area of triangle or s for square: s You entered s for square Enter length of side: 5.25 Area of sqaure of side length 5.25 is 27.5625 Done
Explanation / Answer
1)
#include <iostream>
using namespace std;
const int PASSING_MARKS=40;
int main() {
int sub1;
int sub2;
int sub3;
printf("Enter a marks of sub1 : ");
scanf("%d",&sub1);
printf("Enter a marks of sub2 : ");
scanf("%d", &sub2);
printf("Enter a marks of sub3 : ");
scanf("%d", &sub3);
if(sub1 >= PASSING_MARKS && sub2>= PASSING_MARKS && sub3 >=PASSING_MARKS){
printf("you Pass");
}else{
if((sub1 >= PASSING_MARKS && sub2 >= PASSING_MARKS && sub3 < PASSING_MARKS) || (sub1 < PASSING_MARKS && sub2 >= PASSING_MARKS && sub3 >= PASSING_MARKS) || (sub1 >= PASSING_MARKS && sub2 < PASSING_MARKS && sub3 >= PASSING_MARKS)){
int avg =(sub1+sub2+sub3)/3;
if(avg>=50){
printf("you Passed");
}else{
printf("you Failed");
}
}else{
printf("you failed");
}
}
}
2)
#include <iostream>
using namespace std;
const int PASSING_MARKS=40;
int main() {
int sub1;
int sub2;
int sub3;
char ch;
printf("Enter a choice: ");
scanf("%c",&ch);
switch(ch)
{
case 's': {
int length;
cout<< "Please enter length of side of square";
cin >> length;
cout<< "Area of square " << length*length;
break;
}
case 't': {
float base,height;
cout<< "Please enter base of side of triangle";
cin >> base;
cout<< "Please enter height of side of triangle";
cin >> height;
cout<< "Area of triangle " <<((base)*height)/2;
break;
}
default: cout<<" Invalid Choice Try Again...!!!";
break;
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.