Solutions: (Logical Or), Function
Solutions: (Logical Or), Function
1. [12 pts] Fill in the blanks in each of the following using given keywords.
Keywords: if, else-if, variable, switch-case, void, array, main, constant, int, and, && (logical and), ||
(logical or), function
a. The && operator can be used to ensure that two conditions are both true before choosing a
certain path of execution.
b. main is the only function all C++ programs must contain.
c. void is used to indicate that a function doesn’t return any value.
d. A constant value in C++ is an explicit number or character that doesn't change.
2. [15 pts] State whether each of the following is true or false.
a. [ F ] An if selection structure contains two sets of instructions: one set is processed when the
condition is true and the other set is processed when the condition is false.
b. [ F ] C++ considers the variables number and NuMbEr to be identical. Cas-sensitive!!!!
c. [ T ] C++ supports both single line and multi-line comments. // /**/
d. [ F ] = is used for comparison, whereas, == is used for assignment of two quantities.
e. [ F ] 3MyName is a valid variable name in C++.
This
is
a
C++
program
b. Read an integer from the user at the keyboard and store it in integer variable number.
cin>>number;
4. [20 pts] Identify and correct the errors in each of the following statements.
a. #include<iostream>
using namespace std; int main(){
void main(){ cout<<”Hello world”;
cout<<'Hello world!'; }
}
1
SOLUTIONS
2021-2022 Fall Semester
Introduction to Algorithms Midterm Exam - November 3, 2021
Instructor: Assistant Prof. Dr. Şafak DURUKAN ODABAŞI
e. int main(){
int x=8;
int y=10;
if (x<y)
cout<<”x<y”;
else if()
cout<<”x>y”;
}
}
return 0;
}
b. x = 10; y = -40; -40
if (x >= 10)
if (y < 40) {
cout<<”y=”<<+y;
}
else {
cout<<”y=”<<-y;
}
6. [35 pts] Write a complete C++ program to prompt the user to enter an integer with three digits. Your
program determines whether this integer is an Armstrong number or not.
A positive integer is named an Armstrong number if the sum of cubes of the individual digit is equal to
that number itself. For example:
Ex: 153 = 1 * 1 * 1 + 5 * 5 * 5 + 3 * 3 * 3 // 153 is an Armstrong number.
120 != 1 * 1 * 1 + 2 * 2 * 2 + 0 * 0 * 0 // 120 is not an Armstrong number
Example output:
> Enter an integer with three digits: > Enter an integer with three digits:
> 153 > 120
> 153 is an Armstrong number. > 120 is not an Armstrong number.
#include <iostream>
using namespace std;
int main() {
hundreds=number/100;
tens=(number%100)/10;
ones=number%10;
sumOfCubes=(ones*ones*ones)+(tens*tens*tens)+(hundreds*hundreds*hundreds);
3
SOLUTIONS
2021-2022 Fall Semester
Introduction to Algorithms Midterm Exam - November 3, 2021
Instructor: Assistant Prof. Dr. Şafak DURUKAN ODABAŞI
if(sumOfCubes==number)
cout<<number<<" is an Armstrong number";
else
cout<<number<<" is not an Armstrong number";