0% found this document useful (0 votes)
10 views9 pages

COSC 1012 (TUTORIALS)

This document is a worksheet for an Introduction to Computer Science and Programming course at Unity University, covering key concepts in C++ programming. It includes questions on variables, flow control, syntax errors, program phases, and expected outputs from provided C++ code snippets. Additionally, it prompts students to write their own C++ programs based on given specifications.

Uploaded by

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

COSC 1012 (TUTORIALS)

This document is a worksheet for an Introduction to Computer Science and Programming course at Unity University, covering key concepts in C++ programming. It includes questions on variables, flow control, syntax errors, program phases, and expected outputs from provided C++ code snippets. Additionally, it prompts students to write their own C++ programs based on given specifications.

Uploaded by

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

UNITY UNIVERSITY, ADAMA CAMPUS

DEPARTMENT OF COMPUTER SCIENCE


INTRODUCTION TO COMPUTER SCIENCE AND PROGRAMMING I (COSC 1012)
WORKSHEET
Part One: Read the questions 1 to 7 carefully and give short and precise answer/s for each
question in the boxes provided under each question.
1. Define Variables and its syntax used in C++ programming Language by giving
examples (2 Pts).

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

3. Identify either the following C++ statement is valid or invalid (3 Pts)


using namespace std; p = (x*y)+20;
cout<<“Sum=“<<” “<<Sum; double x:20;
cout>>>a>b; int m; n; j; i,
main(); cout<<“k=“<<k<<” contains a value”,
Valid C++ Statement Invalid C++ Statement

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

cout<<"Enter the radius of a circle";


cin>>r;
area=(r*r)*3.14156;
cout<<"Area="<<<<endl;
float w, h, rectarea;
cout<<"Enter the width of the rectangle"
cin>>w
cout<<"Enter the height of the rectangle";
cin>>h;
rectarea=(w*h);
cout<<"Area="<<rectarea;
cout<<"Enter the width of the triangle";
cin>>width;
cout<<"Enter the height of the triangle";
cin>>height;
trigarea=(width*height)*0.5;
cout<<"area="<<trigarea;
return 0;
}
8. Observe the following simple C++ program and identify the syntax errors of the program
that violates the correct program writing.
include<iostream> { Syntax Errors
using namespace std;
k=5;
k=++k+10;
cout<<"K="<<k<<endl;
k=5
k=k++ +10;
cout<<"K="<<k<<endl;

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

You might also like