Midterm Revsion C++
Midterm Revsion C++
Lecture 2
Example 1
#include <iostream>
using namespace std;
int main() {
return 0;
}
Example 2
Find the average of 3 numbers.
#include <iostream>
using namespace std;
int main() {
cout<<"The average of "<< num1 << " , " << num2 << "
and "<< num3 << " is: " <<avg<< endl;
return 0;
}
Lecture 3
Example 1
• Ask the user to enter two
numbers.
• The program needs to
calculate the sum of both
numbers.
• Both numbers and the sum
need to be saved in one array
#include <iostream>
#include <array>
using namespace std;
int main() {
cout << "The sum of " << numbers[0] << " and " <<
numbers[1] << " is " << numbers[2] << endl;
return 0;
}
Example 2
Example 3
Example 4
Finding the Highest Value in an Array Finding the Lowest Value in anArray
Lecture 4
Example 1
• Write a code that will ask the user to enter a number.
1. Check if the number is less than 5, greater than 5 or equal to 5.
2. If the number is less than 5, print “the number is less than 5”.
3. If the number is greater than 5, print “the number is greater than 5”.
4. If the number is equal to 5, print “the number is equal to 5”.
#include <iostream>
using namespace std;
int main() {
int number;
cout<<"Enter a number: ";
cin >> number;
if (number < 5 ){
cout<<"The number is less than 5 " << endl;
} else {
cout<<"The number is equal to five " << endl;
}
Example 2
• Write a code that will ask the user to enter two numbers.
• Check if the first number is less than the second number, or greater than or equal to it.
• If the first number is less than the second number, print “the first number is less than the second
number”.
• If the first number is greater than the second number, print “the first number is greater than the
second number”.
• If the first number is equal to the second number, print “the two numbers are equal”.
#include <iostream>
using namespace std;
int main() {
int num1,num2;
if (num1< num2){
cout<<"The first number is less than second number " <<
endl;
}
else if (num1 > num2){
cout<<" The first number is greater than second number " <<
endl;
}
else {
cout<<"The two numbers are equal " <<endl;
}
return 0;
}
Example 3
Write a code that will ask the user to enter a letter grade in capital letters.
The job of the code is to print the “performance” equivalent to that letter grade.
Let A is equivalent to Excellent.
B is equivalent to V.good.
C is equivalent to Good.
D is equivalent to Pass.
#include <iostream>
int main() {
char grade;
switch (grade) {
case 'A':
std::cout << "Excellent" << std::endl;
break;
case 'B':
std::cout << "Very Good" << std::endl;
break;
case 'C':
std::cout << "Good" << std::endl;
break;
case 'D':
std::cout << "Pass" << std::endl;
break;
default:
std::cout << "Invalid grade entered." <<
std::endl;
break;
}
return 0;
}