Prepared By:: Lab Sheets
Prepared By:: Lab Sheets
Lab sheets
Prepared by:
Dr. Ahmad Aloqaily
Mr. Muhsen Khaldi
Hend Rabaee
Ala'a shdaifat
Haneen Hijazi
Ala,a gharaibeh
Magdleen Hasan
Safaa alHaj Saleh
1
The Hashemite University
Prince Al-Hussein Bin Abdullah II Faculty for Information Technology
Department of Computer Sciences and Its application
Introduction to Programming Lab
Lab Sheet --- week 1
Basics C++ Program
Technical part:
1- How to access course material --- Moodle
2- How to create a C++ project in Visual Studio
#include<iostream>
usingnamespace std;
int main(){
cout<<"My first C++ program."<<endl;
return 0;
}
Sample Run:
*****************************************
Example 2: This program illustrates how input statements work.
#include<iostream>
usingnamespace std;
int main(){
int feet, inches;
cout<<"Enter two integers separated by spaces: ";
cin>> feet >> inches;
cout<< endl;
cout<<"Feet = "<< feet << endl;
cout<<"Inches = "<< inches << endl;
return 0;
}
Sample Run:
2
EXERCISE:
Write a C++ program that prints your name, id and specialization on different lines?
3
The Hashemite University
Prince Al-Hussein Bin Abdullah II Faculty for Information Technology
Department of Computer Sciences and Its application
Introduction to Programming Lab
Lab Sheet --- week 2
Arithmetic operation in C++
ProgrammingExample: Convert Length
Write a program that takes as input a given length expressed in feet and inches and convert and outputs
the length in centimeters.
Problem analysis:
Input: length in feet and inches
Lengths are given in feet and inches.
Convert the length in feet and inches to all inches:
o Multiply the number of feet by 12
o Add given inches
One inch is equal to 2.54 centimeters
Output: equivalent length in centimeters
Program computes the equivalent length in centimeters
Needed variables
int feet; //variable to hold given feet
int inches; //variable to hold given inches
inttotalInches; //variable to hold total inches
doublecentimeters; //variable to hold length in centimeters
Named Constant
const double CENTIMETERS_PER_INCH = 2.54;
const int INCHES_PER_FOOT = 12;
Programming Example: Body of the Function
The body of the function main has the following form:
int main (){
declare variables...
statements...
return 0;
}
4
Example 1:
//********************************************************
// Program Convert Measurements: This program converts
// measurements in feet and inches into centimeters using
// the formula that 1 inch is equal to 2.54 centimeters.
//********************************************************
#include<iostream>
usingnamespace std;
//named constants
constdouble CENTIMETERS_PER_INCH = 2.54;
constint INCHES_PER_FOOT = 12;
int main(){
//declare variables
int feet, inches;
inttotalInches;
doublecentimeter;
//Statements: Step 1 - Step 7
cout<<"Enter two integers, one for feet and one for inches: "; //Step 1
cin >> feet >> inches; //Step 2
cout<< endl;
cout<<"The numbers you entered are "<< feet <<" for feet and "<< inches
<<" for inches. "<< endl; //Step 3
totalInches = INCHES_PER_FOOT * feet + inches; //Step 4
cout<<"The total number of inches = "<<totalInches<< endl; //Step 5
centimeter = CENTIMETERS_PER_INCH * totalInches; //Step 6
cout<<"The number of centimeters = "<<centimeter<< endl; //Step 7
return 0;
}
Sample Run:
Enter two integers, one for feet and one for inches: 5 10
The numbers you entered are 5 for feet and 10 for inches.
The total number of inches = 70
The number of centimeters = 177.8
EXERCISES:
1- Write C++ program that reads two integers (numb1 ,numb2) and find their :
Summation (numb1+numb2)
Multiplication (numb1*numb2)
Subtraction (numb1-numb2)
Division (numb1/numb2)
Remainder (num1%num2)
2- Write a C++ program that reads the length of a square and calculate its:
Circumference. (Hint : sq_circum =4*length)
Area. (Hint : sq_srea =length^2)
5
3- Write a C++ program that reads the radius of a circle and calculate its area.
(Hint : cir _area =22/7*radius^2)
4- Write a C++ program that reads the temperature in Fahrenheit and convert it to Celsius.
(Hint : Celsius = ((5/9)*(fahrenhite-32))
5- Write a C++ program that reads a three-digit integer and calculate and prints the sum of
all its digits.
Hint: if the user enters 123, the result is 1+2+3=6.
(Use mod and div in your program)
6- Write a program to print the values of x and y, then swap the two values by using a third
variable, reprint the new values of x and y.
Consider the following two variables:
X=10; Y=5;
*(Swap without using third variable): Re-type the program without using third variable.
6
The Hashemite University
Prince Al-Hussein Bin Abdullah II Faculty for Information Technology
Department of Computer Sciences and Its application
Introduction to Programming Lab
Lab Sheet --- week 3
Selection Control Structures
Example 1:Create a program that states whether an input number is positive, negative, or zero. Clearly,
we have more than two options, so a single if statement will not work. Instead, I use a nested if
construction as follows:
#include<iostream>
usingnamespace std;
int main(){
double x; //the input number
cout<<"Enter the number:";
cin>> x;
if (x==0)
cout<<"iszero";
else
if (x>0)
cout <<"is positive";
else
cout<<"is negative";
return 0;
}
Example 2: Suppose we want to create a program that takes as input a grade in percent, and produces as
output a letter grade.The program clearly has multiple choices to make:
#include<iostream>
usingnamespace std;
int main(){
double x;
cout<<"Enter the grade in percent: ";
cin>> x;
if (x >= 90)
cout<<"A";
elseif (x >=80)
cout<<"B";
elseif (x >=70)
cout<<"C";
elseif (x >=60)
cout<<"D";
else
cout<<"F";
return 0;
}
7
Example 3: The effect of break statements in a switch structure
#include<iostream>
usingnamespace std;
int main(){
intnum;
cout<<"Enter an integer between 0 and 7: "; //Line 1
cin>>num; //Line 2
cout<< endl; //Line 3
cout<<"The number you entered is "<<num<< endl; //Line 4
switch(num) //Line 5
{
case 0: //Line 6
case 1: //Line 7
cout<<"Learning to use "; //Line 8
case 2: //Line 9
cout<<"C++'s "; //Line 10
case 3: //Line 11
cout<<"switch structure."<<endl; //Line 12
break; //Line 13
case 4: //Line 14
break; //Line 15
case 5: //Line 16
cout<<"This program shows the effect "; //Line 17
case 6: //Line 18
case 7: //Line 19
cout<<"of the break statement."<<endl; //Line 20
break; //Line 21
default: //Line 22
cout<<"The number is out of range."<<endl; //Line 23
}
cout<<"Out of the switch structure."<<endl; //Line 24
return 0; //Line 25
}
Sample Run:
8
EXERCISES:
1- A) Write a C++ program that asks the user to enter student mark, prints "PASS” if the mark
is greater than or equal to 50, otherwise it should print "FAIL! You must take the course
again".
B) if the student "PASS" ,update the program in (a) to print the grade as follows :
2- Write a C++ program accepts an angel, in degrees, and displays the type of angel
corresponding to the degrees entered.
3- Write a C++ program that asks the user to enter employee basic salary, and then it calculates
the net salary according to the formula: Net salary = basic salary –tax*basic salary
tax equals .16 ,if the basic salary is greater than 1000
tax equals .10 , if the basic salary is between 500 and 1000
otherwise , tax equals .08
4- Write a C++ program that prompts the user to enter an integer and determines whether it is
divisible by both 5 and 6, whether it is divisible 5 or 6 ,and whether it is not divisible by both 5
and 6.
5- Write a C++ program that asks the user to enter three integers and find:
6- Write C++ program that asks the user to enter two integers an operation (+,-,*, /), then find the
result of entered operation (Hint: use switch statement)
9
The Hashemite University
Prince Al-Hussein Bin Abdullah II Faculty for Information Technology
Department of Computer Sciences and Its application
Introduction to Programming Lab
Lab Sheet --- week 4
Repetition Control Structures-I
Example 1: a program to determine the count of odd and even numbers for N positive numbers.
#include<iostream>
usingnamespace std;
constint N = 20;
int main(){
//Declare variables
int counter; //loop control variable
int number; //variable to store the new number
int zeros = 0; //Step 1
int odds = 0; //Step 1
int evens = 0; //Step 1
cout <<"Please enter "<< N <<" integers, "
<<"positive, negative, or zeros."<<endl; //Step 2
cout<<"The numbers you entered are:"<< endl;
for (counter = 1; counter <= N; counter++) //Step 3
{
cin>> number; //Step 3a
cout<< number <<" "; //Step 3b
switch (number % 2){ //Step 3c
case 0:
evens++;
if (number == 0)
zeros++;
break;
case 1:
case -1:
odds++;
}//end switch
return 0;
}
10
Sample Run:
Please enter 20 integers, positive, negative, or zeros.
The numbers you entered are:
55 33 22 11 22 55 33 22 11 22 112 55 66 22 33 22 44 55 22 33
55 33 22 11 22 55 33 22 11 22 112 55 66 22 33 22 44 55 22 33
There are 10 evens, which includes 0 zeros.
The number of odd numbers is: 10
Example 2:a program to determine the sum of the first n positive numbers.
#include<iostream>
usingnamespace std;
int main(){
int counter; //loop control variable
int sum; //variable to store the sum of numbers
int N; //variable to store the number of
//first positive integers to be added
cout<<"Line 1: Enter the number of positive "
<<"integers to be added: "; //Line 1
cin>> N; //Line 2
sum = 0; //Line 3
cout<< endl; //Line 4
for (counter = 1; counter <= N; counter++) //Line 5
sum = sum + counter; //Line 6
cout<<"Line 7: The sum of the first "<< N
<<" positive integers is "<< sum<< endl; //Line 7
return 0;
}
Sample Run:
11
Example 3: a program: Counter-Controlled Loop
Sample Run:
Line 1: Enter the number of integers in the list: 5
Line 6: Enter 5 integers.
2
3
6
4
8
Line 11: The sum of the 5 numbers = 23
Line 13: The average = 4
12
Example 4: A Program: Sentinel-Controlled Loop
Suppose you want to read some positive integers and average them, but you do not havea preset number
of data items in mind. Suppose the number -999 marks the end of thedata. You can proceed as follows.
#include<iostream>
usingnamespace std;
constint SENTINEL = -999;
int main(){
int number; //variable to store the number
int sum = 0; //variable to store the sum
int count = 0; //variable to store total numbers read
cout<<"Line 1: Enter integers ending with"<< SENTINEL <<endl; //Line 1
cin >> number; //Line 2
while (number != SENTINEL) //Line 3
{
sum = sum + number; //Line 4
count++; //Line 5
cin>> number; //Line 6
}
cout <<"Line 7: The sum of the "<< count <<" numbers is "<<
sum<< endl; //Line 7
if(count != 0) //Line 8
cout <<"Line 9: The average is "<< sum / count <<endl; //Line 9
else //Line 10
cout <<"Line 11: No input."<<endl; //Line 11
return 0 ;
}
Sample Run:
Line 1: Enter integers ending with -999
5
6
3
1
-999
Line 7: The sum of the 4 numbers is 15
Line 9: The average is 3
13
Example 5 : What does the following program print?
#include<iostream>
#include<iomanip>
usingnamespace std;
int main(){
for ( int r = 1; r <= 12; r++ ){
for ( int c = 1; c <= 12; c++ )
cout<<setw(4) << r*c;
cout<< endl;
}
return 0;
}
Sample Run:
1 2
2 4 6
3 6 9 12
4 8 12 16 20
5 10 15 20 25 30
6 12 18 24 30 36 42
7 14 21 28 35 42 49 56
8 16 24 32 40 48 56 64 72
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100 110
11 22 33 44 55 66 77 88 99 110 121 132
12 24 36 48 60 72 84 96 108 120 132 144
#include<iostream>
#include<iomanip>
usingnamespace std;
int main(){
for ( int r = 1; r <= 12; r++ ){
for ( int c = 1; c <= 12; C++ ){
cout<<setw(4) << r*c;
if (c > r)
break;
}
cout<< endl;
}
return 0;
}
14
Sample Run:
1 2 3 4 5 6 7 8 9 10 11 12
2 4 6 8 10 12 14 16 18 20 22 24
3 6 9 12 15 18 21 24 27 30 33 36
4 8 12 16 20 24 28 32 36 40 44 48
5 10 15 20 25 30 35 40 45 50 55 60
6 12 18 24 30 36 42 48 54 60 66 72
7 14 21 28 35 42 49 56 63 70 77 84
8 16 24 32 40 48 56 64 72 80 88 96
9 18 27 36 45 54 63 72 81 90 99 108
10 20 30 40 50 60 70 80 90 100 110 120
11 22 33 44 55 66 77 88 99 110 121 132
12 24 36 48 60 72 84 96 108 120 132 144
#include<iostream>
#include<iomanip>
usingnamespace std;
int main(){
for ( int r = 1; r <= 12; r++ ){
for ( int c = 1; c <= 12; c++ ){
cout<<setw(4) <<'#';
if (c >= r)
break;
}
cout<< endl;
}
return 0;
}
Sample Run:
#
# #
# # #
# # # #
# # # # #
# # # # # #
# # # # # # #
# # # # # # # #
# # # # # # # # #
# # # # # # # # # #
# # # # # # # # # # #
# # # # # # # # # # # #
15
The Hashemite University
Prince Al-Hussein Bin Abdullah II Faculty for Information Technology
Department of Computer Sciences and Its application
Introduction to Programming Lab
Lab Sheet --- week 5 and 6
Repetition Control Structures-II
EXERCISES
1- Write a C++ program that prints all the numbers
from 1 to 10
from 10 to 1
then calculates their total
-Using while loop
- Using for loop
2- Write a C++ program that prints the following line of asterisks
**********
Using while loop
Using for loop
Using for loop
3- Write a C++ program that reads the marks of any number of students and finds their
average.
4- Write a C++ program that reads the marks of N students. The program should display:
Whether each student “PASS” or “FAIL”.
The number of passed students and the number of failed students.
The average
The minimum mark
The maximum mark
6- Write a C++ program that asks the user to enter a number and check whether it is prime
or not.
7- Write a C++ program that allows the user to enter two numbers and finds the prime
numbers in between.
16
8- Write a C++ program that prints the following triangles of stars:
* * ***** *****
** ** **** ****
*** *** *** ***
**** **** ** **
***** ***** * *
9- Write a C++ program that asks the user to enter N integers and finds the minimum among
them.
10- Write a C++ program that calculates the factorials of the integers from 1 to 5. Print the
results in tabular format.
17
The Hashemite University
Prince Al-Hussein Bin Abdullah II Faculty for Information Technology
Department of Computer Sciences and Its application
Introduction to Programming Lab
Lab Sheet --- week 7
Functions I
Example1: predefined functions.
#include<iostream>
#include<cmath>
usingnamespace std;
int main(){
int x;
double u, v;
u = 4.2;
v = 3.0;
cout<<"\t "<< u <<" to the power of "<< v <<" = "<<pow(u, v)<< endl;
cout<<" 5.0 to the power of 4 = "<< pow(5.0, 4) << endl;
u = u + pow(3.0, 3);
cout<<" u = "<< u << endl;
x = -15;
cout<<": Absolute value of "<< x<<" = "<< abs(x) << endl;
return 0;
}
#include<iostream>
usingnamespace std;
int secret(int x);
int main(){
intnum = 5;
int a;
a = 2 + secret(num);
cout<< a <<" "<< secret(a) << endl;
return 0;
}
int secret(int x){
if (x > 5)
return 2 * x;
return x;
}
18
Example 3: user defined functions.Find the largest number of a set of 10 numbers.
#include<iostream>
usingnamespace std;
double larger(double x, double y);
int main(){
doublenum; //variable to hold the current number
double max; //variable to hold the larger number
int count; //loop control variable
cout<<"Enter 10 numbers."<<endl;
cin>>num; //Step 1
max = num; //Step 1
for (count = 1; count < 10; count++){ //Step 2
cin>>num; //Step 2a
max = larger(max, num); //Step 2b
}
cout<<"The largest number is "<< max << endl; //Step 3
return 0;
}//end main
Example 4:This program illustrates that a value. It returns only one value, even if the return
statement contains more than one expression.
#include<iostream>
usingnamespace std;
int funcRet1();
int funcRet2(int z);
int main(){
intnum = 4;
cout<<"Line 1: The value returned by funcRet1: "<< funcRet1()<<endl;//Line1
cout<<"Line 2: The value returned by funcRet2: "<< funcRet2(num)<<endl;//Line2
return 0;
}
int funcRet1(){
int x = 45;
return 23, x; //only the value of x is returned
}
int funcRet2(int z){
int a = 2;
int b = 3;
return 2 * a + b, z + b;//only the value of z + b is returned
}
19
Example 5:The following program is designed to find the area of a rectangle, the area of a circle,
or the volume of a cylinder. However, (a) the statements are in the incorrect order; (b) the
function calls are incorrect; (c) the logical expressionin the while loop is incorrect; and (d)
function definitions are incorrect. Rewrite the program so that it works correctly. Your program
must be properly indented. (Note that the program is menu driven and allows the user to run the
program as long as the user wishes.)
#include<iostream>
usingnamespace std;
constdouble PI = 3.1419;
double rectangle(double l, double w);
double circle(double r);
double cylinder(doublebR, double h);
#include<iomanip>
int main(){
double radius;
double height;
double length;
double width;
int choice;
cout<< fixed <<showpoint<<setprecision(2) << endl;
cout<<"This program can calculate the area of a rectangle, "<<"the area
of a circle, or volume of a cylinder."<<endl;
cout<<"To run the program enter: "<< endl;
cout<<"1: To find the area of rectangle."<<endl;
cout<<"2: To find the area of a circle."<<endl;
cout<<"3: To find the volume of a cylinder."<<endl;
cout<<"-1: To terminate the program."<<endl;
cout<<"Enter Your Choice\t";
cin>> choice;
cout<< endl;
while (choice != -1){
switch (choice){
case 1:
cout<<"Enter the length and the width "<<"of the
rectangle: ";
cin>> length >> width;
cout<< endl;
cout<<"Area of Rectangle = "<<rectangle(length,width)<<endl;
break;
case 2:
cout<<"Enter the radius of the circle: ";
cin>> radius;
cout<< endl;
cout<<"Area = "<< circle(radius)<< endl;
break;
case 3:
cout<<"Enter the radius of the base and the "<<"height of
the cylinder: ";
cin>> radius >> height;
cout<< endl;
cout<<"Volume = "<< cylinder(radius, height)<< endl;
20
break;
default:
cout<<"Invalid choice!"<<endl;
}
cout<<"To run the program enter: "<< endl;
cout<<"2: To find the area of a circle."<<endl;
cout<<"1: To find the area of rectangle."<<endl;
cout<<"3: To find the colume of a cylinder."<<endl;
cout<<"-1: To terminate the program."<<endl;
cout<<"Enter Your Choice\t";
cin>> choice;
cout<< endl;
}
return 0;
}
double rectangle(double l, double w){
return l * w;
}
double circle(double r){
return PI *r* r ;
}
double cylinder(doublebR, double h){
return PI * bR * bR * h;
}
Program testing: (run the program and enter the required data for eachcase)
This program can calculate the area of a rectangle, the area of a circle, or vol
ume of a cylinder.
To run the program enter:
1: To find the area of rectangle.
2: To find the area of a circle.
3: To find the volume of a cylinder.
-1: To terminate the program.
Enter Your Choice
This program can calculate the area of a rectangle, the area of a circle, or vol
ume of a cylinder.
To run the program enter:
1: To find the area of rectangle.
2: To find the area of a circle.
3: To find the volume of a cylinder.
-1: To terminate the program.
Enter Your Choice 1
21
Area of Rectangle = 100.00
To run the program enter:
2: To find the area of a circle.
1: To find the area of rectangle.
3: To find the colume of a cylinder.
-1: To terminate the program.
Enter Your Choice
22
The Hashemite University
Prince Al-Hussein Bin Abdullah II Faculty for Information Technology
Department of Computer Sciences and Its application
Introduction to Programming Lab
Lab Sheet --- week 8
Example 1:The following program is designed to find the area of a rectangle, the area of a circle, or the
volume of a cylinder. However, (a) the statements are in the incorrect order; (b) the function calls are
incorrect; (c) the logical expression in the while loop is incorrect; and (d) function definitions are
incorrect. Rewrite the program so that it works correctly. Your program must be properly indented.
(Note that the program is menu driven and allows the user to run the program as long as the user
wishes.)
#include<iostream>
usingnamespace std;
constdouble PI = 3.1419;
double rectangle(double l, double w);
double circle(double r);
double cylinder(doublebR, double h);
voidPrintMenu (); //function prototype
#include<iomanip>
int choice;
int main(){
double radius;
double height;
double length;
double width;
PrintMenu (); //function calling
cout<< fixed <<showpoint<<setprecision(2) << endl;
while (choice != -1){
switch (choice){
case 1:
cout<<"Enter the length and the width "<<"of the rectangle: ";
cin>> length >> width;
cout<< endl;
cout<<"Area of Rectangle = "<< rectangle(length,width) << endl;
break;
case 2:
cout<<"Enter the radius of the circle: ";
cin>> radius;
cout<< endl;
cout<<"Area = "<< circle(radius)<< endl;
break;
case 3:
cout<<"Enter the radius of the base and the "<<"height of the
cylinder: ";
cin>> radius >> height;
cout<< endl;
cout<<"Volume = "<< cylinder(radius, height)<< endl;
break;
default:
cout<<"Invalid choice!"<<endl;
23
}
PrintMenu (); //function calling
}
return 0;
}
// function definition
voidPrintMenu (){
cout<<"This program can calculate the area of a rectangle, "<<"the area of a
circle, or volume of a cylinder."<<endl;
cout<<"To run the program enter: "<< endl;
cout<<"1: To find the area of rectangle."<<endl;
cout<<"2: To find the area of a circle."<<endl;
cout<<"3: To find the volume of a cylinder."<<endl;
cout<<"-1: To terminate the program."<<endl;
cout<<"Enter Your Choice\t";
cin>> choice;
cout<< endl;
}
double rectangle(double l, double w){
return l * w;
}
double circle(double r){
return PI *r* r ;
}
double cylinder(doublebR, double h){
return PI * bR * bR * h;
}
********************************
Example 2 : Program Print a triangle of stars
#include<iostream>
usingnamespace std;
voidprintStars(int blanks, intstarsInLine);
int main(){
intnoOfLines; //variable to store the number of lines
int counter; //for loop control variable
intnoOfBlanks; //variable to store the number of blanks
cout<<"Enter the number of star lines (1 to 20) "
<<"to be printed: ";//Line 1
cin>>noOfLines; //Line 2
while (noOfLines< 0 || noOfLines> 20){ //Line 3
cout<<"Number of star lines should be "<<"between 1 and
20"<<endl; //Line 4
cout<<"Enter the number of star lines "<<"(1 to 20) to be
printed: "; //Line 5
cin>>noOfLines; //Line 6
}
cout<< endl << endl; //Line 7
noOfBlanks = 30; //Line 8
for (counter = 1; counter <= noOfLines; counter++){ //Line 9
printStars(noOfBlanks, counter); //Line 10
noOfBlanks--; //Line 11
}
return 0; //Line 12
24
}
voidprintStars(int blanks, intstarsInLine){
int count;
for (count = 1; count <= blanks; count++) //Line 13
cout<<' '; //Line 14
for (count = 1; count <= starsInLine; count++) //Line 15
cout<<" *"; //Line 16
cout<< endl;
}
Example 3:Value and reference parameters illustrating how two variables are swapped.
#include<iostream>
usingnamespace std;
void Swap (int&x, int&y);
int main (){
int x1,y1;
x1 =10;
y1 =15;
cout<<"before calling the Swap function"<< x1 <<"\t"<< y1 << endl;
Swap(x1,y1);
cout<<"after calling the Swap function"<< x1 <<"\t"<< y1 << endl;
return 0;
}
void Swap (int&x, int&y){
int temp;
cout<<"in Swap function before changing"<< x <<"\t"<< y << endl;
temp = x;
x = y;
y = temp;
cout<<"in Swap function after changing"<< x <<"\t"<< y << endl;
}
#include<iostream>
usingnamespace std;
voidfuncValueParam(intnum);
int main(){
int number = 6; //Line 1
cout<<"Line 2: Before calling the function "<<"funcValueParam, number =
"<< number<< endl; //Line 2
funcValueParam(number); //Line 3
cout<<"Line 4: After calling the function "<<"funcValueParam, number =
"<< number<< endl; //Line 4
return 0;
}
voidfuncValueParam(intnum){
cout<<"Line 5: In the function funcValueParam, "<<"before changing, num
= "<<num<< endl; //Line 5
num = 15; //Line 6
25
cout<<"Line 7: In the function funcValueParam, "<<"after changing, num
= "<<num<< endl; //Line 7
}
Example 5: This program reads a course score and prints the associated course grade.
#include<iostream>
usingnamespace std;
voidgetScore(int& score);
voidprintGrade(int score);
int main (){
intcourseScore;
cout<<"Line 1: Based on the course score, \n"<<" this program computes
the "<<"course grade."<<endl; //Line 1
getScore(courseScore); //Line 2
printGrade(courseScore); //Line 3
return 0;
}
voidgetScore(int& score){
cout<<"Line 4: Enter course score: "; //Line 4
cin>> score; //Line 5
cout<< endl <<"Line 6: Course score is "<< score << endl; //Line 6
}
voidprintGrade(intcScore){
cout<<"Line 7: Your grade for the course is "; //Line 7
if (cScore>= 90) //Line 8
cout<<"A."<< endl;
elseif (cScore>= 80)
cout<<"B."<< endl;
elseif(cScore>= 70)
cout<<"C."<< endl;
elseif (cScore>= 60)
cout<<"D."<< endl;
else
cout<<"F."<< endl;
}
26
The Hashemite University
Prince Al-Hussein Bin Abdullah II Faculty for Information Technology
Department of Computer Sciences and Its application
Introduction to Programming Lab
Lab Sheet --- week 9
Program Results:
BBBBBBB AAAAAA
100 56.6
B a
15
100
usingnamespace std;
return 0;
}
}
Sample Run:
Line 1: a = 23, b = 48.78, ch = M
Line 2: Volume = 1
Line 3: Volume = 20
Line 4: Volume = 34
Line 5: Volume = 120
Line 12: x = 46, y = 12.34, z = B
Line 12: x = 92, y = 42.68, z = B
Line 12: x = 184, y = 34.65, z = Q
Line 9: a = 184, b = 48.78, ch = M
29
The Hashemite University
Prince Al-Hussein Bin Abdullah II Faculty for Information Technology
Department of Computer Sciences and Its application
Introduction to Programming Lab
Lab Sheet --- week 10
EXERCISES
1- Write a C++programcalled measure that contains the following two functions:
31
Example 1: a program to read five numbers, find their sum, and print the numbers in reverse order.
#include<iostream>
usingnamespace std;
int main(){
int item[5]; //Declare an array item of five components
int sum;
int counter;
sum = 0;
cout<< endl;
cout<< endl;
return 0;
}
32
The Hashemite University
Prince Al-Hussein Bin Abdullah II Faculty for Information Technology
Department of Computer Sciences and Its application
Introduction to Programming Lab
Lab Sheet --- week 12
Exercise 1:write a C++ program that declares an array of size 5,the program will ask the user to fill the
array with students' marks, and then find their average, number of passed students and the maximum
mark.
The program declares another array that will contain "P" if the corresponding mark is greater than or
equal 50 and "F" if the corresponding mark is less than 50. The program declares another array that will
contain "P" if the corresponding mark is greater than or equal 50 and "F" if the corresponding mark is
less than 50. The main function for the program is shown below, write the missing functions and other
pieces of code so that the program could run properly
Exercise 2: write a C++ program that reads the salaries for 10 worker then:
Read the gender (m or f) for the 10 workers
Define a function max that finds the maximum salary.
Define a function average that finds the average for all salary .
Define function the gender that finds the gender for minimum salary.
Define function called its gender that finds the gender of first salary>500.
33
Exercise 3:write a C++ program that containstwo functions:
Setdata( int A[ ], int size): asks the user to fill the array.
34