COSC 1012 (TUTORIALS)
COSC 1012 (TUTORIALS)
2. Explain the purpose of flow of control, control variable used in a program and list down
the types of flow of control used in C++ programming language (3 Pts).
4. Write down the phases that every C++ program development environment passes through
with its corresponding programs to write C++ program (3 Pts).
Phases of C++ Program Programs Used at each Phase
Development
1
5. Write the syntax for the control structure of, if-----else, if—else-- if ladder and switch
statements (3 pts).
No Types of Control Syntax for the control structure
Structure
1 if----else
if----else if ladder
2
3 switch
6. All programs in many programming languages such as C++ are written in three control
structures or statements. List down and explain these control structure or statements used
in C++ programming language (3 Pts).
Part Two: Read the C++ code written under each of the following questions carefully and
identify the syntax errors you observed. Write your answers inside the box provided
under each questions.
7. Read the following C++ code carefully and identify the syntax error you observed and
the statement that makes the program correct side by side inside the box below shortly
and precisely.
#include <iostream>
using namespace std;
main(){
2
program to calculate Area of a Circle
float area; Syntax Errors
3
k=5;
k=--k+10;
cout<<"k="<<k<<endl;
k=5;
k=k-- +10;
<<"K="<<k;
}
Part Three: Read the following C++ program under each of the following questions carefully
and determine the output of the program you expect and write your answers
inside the box provided under each questions
9. Read the following C++ code carefully line by line and determine the output of the
program you expect and write your answer inside the box below (5 Pts).
#include<iostream>
using namespace std;
int main(){
//C++ program to demonstrate Arithemetic Operators
cout<<" C++ *******************Program"<<endl;
cout<<" C++*******Arithemetic Operators"<<endl<<endl;
int x, y;
//Assign values to x and y variables
x=120;
y=40;
cout<<"Sum="<<(x+y)<<endl;
cout<<"Difference="<<(x-y)<<endl;
cout<<"Product="<<(x*y)<<endl;
cout<<"Division="<<(x/y)<<endl;
cout<<"Modulus="<<(x%y)<<endl;
cout<<"C+++*********************Program"<<endl;
cout<<"********Relational Operators"<<endl<<endl;
//C++ program to demonstrate Relational or Equality Operator
int num=15;
cout<<"num<10 is "<<" "<<(num<10)<<endl;
4
cout<<"num>10 is"<<" "<<(num>10)<<endl;
cout<<"num==10 is"<<" "<<(num==10)<<endl;
cout<<"num==20 is"<<" "<<(num==20)<<endl;
cout<<"num!=20 is"<<" "<<(num!=20)<<endl;
cout<<"num>=20 is"<<" "<<(num>=20)<<endl;
cout<<"num<=20 is"<<" "<<(num<=20)<<endl<<endl;
cout<<"******Implement Casting *******"<<endl;
int cost=200;
int volume=800;
double up;
up=cost/(double)volume;
cout<<"Unit Price="<<up<<endl;
cout<<"Characters are Valid ************* Operands"<<endl;
cout<<"(Y less than Z) in ASCII Code*******"<<endl;
char var1='Y';
char var2='Z'; Output
//Y is less than Z because in ASCII code
cout<<"Y less than Z is:"<<(var1<var2)<<endl;
int m, n;
m=111;
n=++m;//Pre-increment Operator is applied in m
cout<<"m="<<m<<" "<<"n="<<n<<endl;
n=m++;//Post-increment ia applied in m;
cout<<"m="<<m<<" "<<"n="<<n<<endl;
n=--m;//Pre-decrement operator is applied on m
cout<<"m="<<m<<" "<<"n="<<n<<endl;
n=m--;//Post decrement is applied on m
cout<<"m="<<m<<" "<<"n="<<n<<endl;
cout<<"*************************"<<endl;
int k=20;
k=++k +10;//Pre-increment Operator applied on k
5
cout<<"K="<<k<<endl;
k=5;
//int a;
k=k++ +10;//Post Increment is applied on k
cout<<"K="<<k<<endl;
k=5;
k=--k +10;//Pre-decrement is made on K
cout<<"K="<<k<<endl;
k=5;
k=k-- +10;//Post-decrement operator is applied on k
cout<<"K="<<k<<endl<<endl;
cout<<"********C++ Program to Demonstrate"<<endl;
cout<<"********Assignment Operator d/t Characters"<<endl;
int z;
z=25;
z+=25;//z=(z+25)
cout<<"Z="<<z<<endl;
z-=25;//z=(z-25) Output
cout<<"Z="<<z<<endl;
z*=25;//(z=z*25)
cout<<"Z="<<z<<endl;
z%=25;//z=z%25)
cout<<"Z="<<z<<endl;
z/=25;//z=z/25
cout<<"Z="<<z<<endl;
return 0;
}
10. Read the following C++ program carefully line by line and determine the output of the
program you expect and write your answer inside the space provided under the question.
#include<iostream>
6
using namespace std;
int main () { Output
//Declaration of Variables
bool b,n;
b=10;
n=20;
//Test the condition
if(b==n){
cout<<"This is executed."<<endl;
}
else {
cout<<"This is not executed."<<endl;
}
cout<<"b>n is "<<(b>n)<<endl;
cout<<"b<n is "<<(b<n)<<endl<<endl;
//Declaration of variables
int x, y,z;
x=30;
y=40;
z=(x+y);
cout<<"X="<<x<<" "<<"Y="<<y<<" "<<"Z="<<z<<endl;
cout<<"WELCOME TO UNITY UNIVERSITY"<<endl;
cout<<"Department of Computer Science"<<endl;
int w=40;
cout<<"W="<<w<<endl;
cout<<"***GOOD LUCK!****"<<endl;
cout<<"****END OF PROGRAM****"<<endl<<endl;
cout<<"The size of an int is:\t\t"<<sizeof(int)<<" "<<"bytes"<<endl;
cout<<"The size of a short int is:\t"<<sizeof(short)<<" "<<"bytes"<<endl;
cout<<"The size of char is:\t\t"<<sizeof(char)<<" "<<"bytes"<<endl;
return 0;
7
}
}
Part Four: Write C++ program based on the information given to you for the following
questions. Make your writing neat and provides comment if it is possible to make
the program more readable. Use the attached blank paper for your answers.
11. Write C++ program to accept variables of three inputs and displays the inputs of the
program as an output.
12. Write C++ program to display the following output using char data type and character
strings in one program.
C D E F
I Love ETHIOPIA!
13. Write algorithm expression or pseudo code program and C++ program for the following
mathematical expression.
x+ y x− y
A) – B) x=√ a2 +b 2
z z−x
p−q
C) + m*n D) F= (9/5 * C) +32
r−s
14. Write C++ program to display the following output after running.
O B A M A
Welcome To C++ Programming Language mid- Exam
You will be the best COMPUTER SCIENCE man
If you are good in Programming
Please try to work hard being you are the best and competent in IT market
15. Observe the following mathematical expression information and analyze, write its pseudo
code program and C++ code to perform the required operation. The perimeter P and Area
of a triangle whose sides have lengths a, b and c are given by.
Where s=(a+b+c)/2.
8
9