0% found this document useful (0 votes)
66 views

Solutions: (Logical Or), Function

This document contains the solutions to an introduction to algorithms midterm exam from November 2021. It includes the exam questions and blank spaces to fill in answers. The questions cover topics like C++ keywords, logical operators, selection structures, comments, variable declaration, input/output, and writing complete programs. One question asks students to write a program that determines if a 3-digit integer is an Armstrong number. The summary provides the essential information about the document's contents and purpose in 3 sentences or less.

Uploaded by

Teckmo Oyun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views

Solutions: (Logical Or), Function

This document contains the solutions to an introduction to algorithms midterm exam from November 2021. It includes the exam questions and blank spaces to fill in answers. The questions cover topics like C++ keywords, logical operators, selection structures, comments, variable declaration, input/output, and writing complete programs. One question asks students to write a program that determines if a 3-digit integer is an Armstrong number. The summary provides the essential information about the document's contents and purpose in 3 sentences or less.

Uploaded by

Teckmo Oyun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

SOLUTIONS

2021-2022 Fall Semester


Introduction to Algorithms Midterm Exam - November 3, 2021
Instructor: Assistant Prof. Dr. Şafak DURUKAN ODABAŞI

Name-Surname:………………………………………………………………… Student ID:…………………………………………


Duration: 75 min. Good Luck!

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++.

3. [8 pts] Write a single C++ statement to accomplish each of the following.


a. Print the message "This is a C++ program" with each word on a separate line.
cout<<”This”<<endl<<”is”<<endl<<”a”<<endl<<”C++”<<endl<<”program”;

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

Name-Surname:………………………………………………………………… Student ID:…………………………………………


Duration: 75 min. Good Luck!

b. int main() c. int main()


{ {
int a=10; int a=10;
b=8; int b=8;
cout<<”a+b=”<<a+b; cout<<”a+b=”<<a+b;
return 0; return 0;
} }

int Int main(){


c. Int main(){ int a;
Int a; a=2;
cout<<a; a=2; } cout<<a; a=2; }
d. int Main(){ int main(){
int a=9, int a=9;
int z=5.9; int z=5;
} }

e. int main(){
int x=8;
int y=10;
if (x<y)
cout<<”x<y”;
else if()
cout<<”x>y”;
}

5. [10 pts] Write the output of following piece of codes.


a. int main()
{
int colour = 2;
if(colour==0)
cout<<"Black"<<endl;
else if(colour==1)
cout<<"Red"<<endl;
if(colour==2 && colour<5 ) Aqua
cout<<"Aqua"<<endl; Green
if(colour>0||colour==3)
cout<<"Green"<<endl;
else
cout<<"Other";
2
SOLUTIONS
2021-2022 Fall Semester
Introduction to Algorithms Midterm Exam - November 3, 2021
Instructor: Assistant Prof. Dr. Şafak DURUKAN ODABAŞI

Name-Surname:………………………………………………………………… Student ID:…………………………………………


Duration: 75 min. Good Luck!

}
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() {

int number; //153 1->hundreds, 5->tens, 3->ones


int ones,tens,hundreds;
int sumOfCubes;
cout<<"Enter a positive integer with 3 digits:";
cin>>number;

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

Name-Surname:………………………………………………………………… Student ID:…………………………………………


Duration: 75 min. Good Luck!

if(sumOfCubes==number)
cout<<number<<" is an Armstrong number";
else
cout<<number<<" is not an Armstrong number";

You might also like