All Tasks

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

BIOMEDICAL ENGINEERING H.T.

I
DEPARTMENT

Computer Programming
(CS 101 & MDE125)

Dr. Nour S. Bakr


Eng. Manar Fathy

Schaum's Programming with C++ 2nd Ed.


By: John Hubbard

C++ Language Tutorial


By: Juan Soulié

Dr. Nour S. Bakr 01000-98-1103 dr.nour@ieee.org 1


Computer Programming using C++ Task # 00: Introduction
Dr. Nour S. Bakr Eng. Manar Fathy

Dr. Nour S. Bakr 01000-98-1103 dr.nour@ieee.org 2


Computer Programming using C++ Task # 00: Introduction
Dr. Nour S. Bakr Eng. Manar Fathy

A keyword: is a word that is already defined and is reserved for a unique


purpose in programs written in that language.
Standard C++ now has 74 keywords

Dr. Nour S. Bakr 01000-98-1103 dr.nour@ieee.org 3


Computer Programming using C++ Task # 01: Basic Elements of C++
Dr. Nour S. Bakr Eng. Manar Fathy

Dr. Nour S. Bakr 01000-98-1103 dr.nour@ieee.org 4


Computer Programming using C++ Task # 01: Basic Elements of C++
Dr. Nour S. Bakr Eng. Manar Fathy

Dr. Nour S. Bakr 01000-98-1103 dr.nour@ieee.org 5


Computer Programming using C++ Task # 02: If - Control Statements
Dr. Nour S. Bakr Eng. Manar Fathy

1- Write a program that reads a number and indicates if it is


positive or negative.

# include <iostream>
int main ( )
{
double num;
cout << "Enter a number: ";
cin >> num;
if (num > 0)
cout << num << " is positive\n";
else if (num < 0)
cout << num << " is negative\n";
}

2- Write a program that reads a number and prints its


additive reverse according to the following output samples.

# include <iostream>
int main ( )
{
double num;
cout << "Enter a number: ";
cin >> num;
if (num != 0)
cout << "Reverse is: " << num * -1 << endl;
else
cout << "No reverse\n";
}

Dr. Nour S. Bakr 01000-98-1103 dr.nour@ieee.org 6


Computer Programming using C++ Task # 02: If - Control Statements
Dr. Nour S. Bakr Eng. Manar Fathy

3- Write a program that reads two integers and indicate if


one of them is multiple of the second.
# include <iostream>
int main ( )
{
int n1, n2;
cout << "Enter two integers: ";
cin >> n1 >> n2;
if (n1 % n2 == 0)
cout << n1 << " is multiple of " << n2 << endl;
else if (n2 % n1 == 0)
cout << n2 << " is multiple of " << n1 << endl;
else
cout << "no multiples\n";
}

4- Write a program that reads a positive number


representing hours worked by a labor and print his
equivalent payment if he gets as follows: 15 LE/hour for first
30 hours, 20 LE/hour for next 20 hours, and 30 LE/hour for
further hours.
# include <iostream>
int main ( )
{
int hours;
double pay;
cout << "Enter hours: ";
cin >> hours;
if (hours <= 30)
pay = hours * 2;
else if (hours <= 50)
pay = 30 * 2 + (hours - 30) * 2.5;
else
pay = 30 * 2 + 20 * 2.5 + (hours - 50) * 3;
cout << "Payment is: " << pay << " KD\n";
}

Dr. Nour S. Bakr 01000-98-1103 dr.nour@ieee.org 7


Computer Programming using C++
Task # 03: Switch - Control Statements
Dr. Nour S. Bakr Eng. Manar Fathy

Update this program using a switch Statement to print your


actual Grad in certain subject, also print your total Grad
depend on GPA.

# include <iostream>
int main ( )
{
int score;
cout << "Enter your test score: "; cin >> score;
switch (score/10)
{
case 10:
case 9: cout << "Your grade is an A." << endl; break;
case 8: cout << "Your grade is a B." << endl; break;
case 7: cout << "Your grade is a C." << endl; break;
case 6: cout << "Your grade is a D." << endl; break;
case 5:
case 4:
case 3:
case 2:
case 1:
case 0: cout << "Your grade is an F." << endl; break;
default: cout << "Error: score is out of range.\n";
}
cout << "Goodbye." << endl;
}

Dr. Nour S. Bakr 01000-98-1103 dr.nour@ieee.org 8


Computer Programming using C++ Task # 04: while - Iteration
Dr. Nour S. Bakr Eng. Manar Fathy

Write a program to countdown (from n to 1) using a while-loop.


Update this program using do-while loop.

#include <iostream>
using namespace std;
int main ()
{
int n;
cout << "Enter the starting number > ";
cin >> n;
while (n>0)
{
cout << n << ", ";
--n;
}
cout << "FIRE!\n";
return 0;
}

Dr. Nour S. Bakr 01000-98-1103 dr.nour@ieee.org 9


Computer Programming using C++ Task # 04: while - Iteration
Dr. Nour S. Bakr Eng. Manar Fathy

Write a program that reads an integer and prints the sum of


its digits.

# include <iostream>
using namespace std;
int main ( )
{
int x, d, sum = 0;
cout << "Enter an integer: ";
cin >> x ;
while ( x != 0 )
{
d = x % 10;
sum += d ;
x /= 10;
}
cout << "Sum: " << sum << endl;
return 0;
}

Write a program to get the following output?

Dr. Nour S. Bakr 01000-98-1103 dr.nour@ieee.org 11


Computer Programming using C++ Task # 05: For- loop
Dr. Nour S. Bakr Eng. Manar Fathy

Write a program that reads 10 integers and prints the count of even
numbers using for loop.

#include <iostream>
using namespace std;
int main ()
{
int x, i, count = 0;
cout << "Enter 10 integers:\n";
for (i = 1; i <= 10; i++)
{
cin >> x;
if (x % 2 == 0)
count = count + 1;
}
if (count != 0)
cout << "Number of even numbers is: " << count << endl;
else
cout << "No even numbers!\n";
return 0;
}

Dr. Nour S. Bakr 01000-98-1103 dr.nour@ieee.org 11


Computer Programming using C++ Task # 05: For- loop
Dr. Nour S. Bakr Eng. Manar Fathy

Write a program that reads two integers and prints their


greatest common divisor.

# include <iostream>
using namespace std;
int main ( )
{
int x, y, min, g;
cout << "Enter two integers: ";
cin >> x >> y;
if (x < y)
min = x;
else
min = y;
for (int i = 1; i <= min; i++)
if (x % i == 0 && y % i == 0)
g = i;
cout << "GCD is" << g << endl;
return 0;
}

Dr. Nour S. Bakr 01000-98-1103 dr.nour@ieee.org 12


Computer Programming using C++ Task # 05: For- loop
Dr. Nour S. Bakr Eng. Manar Fathy

Write a program that reads 10 integers and prints the


average of only odd numbers.

# include <iostream>
using namespace std;
int main ( )
{
int x, i, count = 0;
double sum = 0;
cout << "Enter 10 integers:\n";
for (i = 1; i <= 10; i++)
{
cin >> x;
if ( x % 2 != 0)
{
sum += x;
count ++;
}
}
if (count != 0)
cout << "Average of odd numbers: " << sum / count << endl;
else
cout << "No odd numbers!" << endl;
return 0;
}

Dr. Nour S. Bakr 01000-98-1103 dr.nour@ieee.org 13


Computer Programming using C++ Task # 05: For- loop
Dr. Nour S. Bakr Eng. Manar Fathy

Write a program that uses a for structure to calculate and


print the average of several integers. Assume the last value
read is the sentinel 9999.
A typical input sequence might be 10 8 11 7 9 9999
indicating that the program should calculate the average of
all the values preceding 9999.

#include <iostream>
using namespace std;
int main()
{
int number, total, count;
double average;
total = 0;
cout << "Enter the values, 9999 to exit: " << endl;
cin >> number;
for ( count = 0; number != 9999; count ++ )
{
total += number;
cin >> number;
}
average = (double) total / count;
cout << "The average is: " << average << endl;
return 0;
}

Dr. Nour S. Bakr 01000-98-1103 dr.nour@ieee.org 14


Computer Programming using C++ Task # 05: For- loop
Dr. Nour S. Bakr Eng. Manar Fathy

Write a program that reads a positive integer and prints its


factorial. N! = N * (N-1) * (N-2) . . . . . * 3 * 2 * 1

#include <iostream>
using namespace std;
int main ()
{
int x, fact = 1;
cout << "Enter a positive integer: ";
cin >> x;
if (x < 0)
cout << "Invalid number!\n";
else
{
for (int i = 1; i <= x; i = i + 1)
fact *= i;
cout << "Factorial = " << fact << "\n";
}
return 0;
}

Dr. Nour S. Bakr 01000-98-1103 dr.nour@ieee.org 15


Computer Programming using C++ Task # 06: Functions
Dr. Nour S. Bakr Eng. Manar Fathy

Dr. Nour S. Bakr 01000-98-1103 dr.nour@ieee.org 16


Computer Programming using C++ Task # 06: Functions
Dr. Nour S. Bakr Eng. Manar Fathy

Write a function that takes a number and a character and


prints a solid square out of that character with side equal to
the number.
For example, if 4 and # are read, the following square is
printed.
####
####
####
####

# include <iostream>
using namespace std;
void square ( int, char );
int main ( )
{
int size;
char c;
cout << "Enter the size: ";
cin >> size;
cout << "Enter a character: ";
cin >> c;
square ( size, c );
return 0;
}

void square ( int size, char c )


{
for ( int i = 1; i <= size ; i++ )
{
for ( int j = 1; j <= size; j++ )
cout << c;
cout << endl;
}
}

Dr. Nour S. Bakr 01000-98-1103 dr.nour@ieee.org 17


Computer Programming using C++ Task # 06: Functions
Dr. Nour S. Bakr Eng. Manar Fathy

Write a program that inputs three double numbers and


passes them to a function that returns the smallest number.

# include <iostream>
using namespace std;
double smallest ( double, double, double );
int main ( )
{
double x, y, z, min;
cout << "Enter three numbers: ";
cin >> x >> y >> z;
min = smallest ( x, y, z );
cout << "The smallest number is: " << min << endl;
return 0;
}

double smallest ( double x, double y, double z )


{
if ( x < y && x < z )
return x;
if ( y < z )
return y;
return z;
}

Dr. Nour S. Bakr 01000-98-1103 dr.nour@ieee.org 18


Computer Programming using C++ Task # 06: Functions
Dr. Nour S. Bakr Eng. Manar Fathy

Write a function multiple that determines for a pair of


integers whether the second integer is a multiple of the first.
The function should take two integer arguments and return
true if the second is a multiple of the first, false otherwise.
Use this function in a program that inputs a series of pairs of
integers.

# include <iostream>
using namespace std;
bool multiple ( int, int );
int main ( )
{
int x, y;
bool m;
cout << "Enter two numbers (-1 to exit): ";
cin >> x >> y;
while ( x != -1 && y != -1 )
{
m = multiple ( x, y );
if ( m == true )
cout << y << " is a multiple of " << x << endl << endl;
else
cout << y << " is not a multiple of " << x << endl << endl;
cout << "Enter two numbers (-1 to exit): ";
cin >> x >> y;
}
return 0;
}

bool multiple ( int x, int y )


{
if ( y % x == 0 )
return true;
return false;
}

Dr. Nour S. Bakr 01000-98-1103 dr.nour@ieee.org 19


Computer Programming using C++ Task # 07: Arrays
Dr. Nour S. Bakr Eng. Manar Fathy

1- Arrays (Print-Reverse):
Write a program that reads 10 integers and prints them in a
reverse order.

# include <iostream>
using namespace std;
int main ( )
{
int x [10], i;
cout << "Enter 10 integers: " ;
for (i = 0; i < 10; i ++)
cin >> x [i];
cout << "Printing reverse: ";
for (i = 9; i >= 0; i --)
cout << x [i] << " ";
cout << endl;
return 0;
}

Dr. Nour S. Bakr 01000-98-1103 dr.nour@ieee.org 21


Computer Programming using C++ Task # 07: Arrays
Dr. Nour S. Bakr Eng. Manar Fathy

2- Arrays with functions (Sum-Evens):


Write a function that gets an integer array and its number
of elements. The function should return the sum of the even
numbers in the array.

# include <iostream>
using namespace std;
int sum_evens (int [ ], int);
int main ( )
{
int x [10], i, sum;
cout << "Enter 10 integers: ";
for (i = 0; i <= 9; i ++)
cin >> x [i];
sum = sum_evens (x, 10);
cout << "Sum of Even Numbers = " << sum << endl;
return 0;
}

int sum_evens (int x [ ], int n)


{
int sum = 0;
for (int i = 0; i < n; i ++)
if (x [i] % 2 == 0)
sum += x [i];
return sum;
}

Dr. Nour S. Bakr 01000-98-1103 dr.nour@ieee.org 21


Computer Programming using C++ Task # 07: Arrays
Dr. Nour S. Bakr Eng. Manar Fathy

3- Arrays with functions (Average):


Write a function that gets an integer array and its number
of elements. The function should return the average of the
array elements.

# include <iostream>
using namespace std;
double average (int x [ ], int n)
{
int i, sum = 0;
for (i = 0; i < n; i ++)
sum += x [i];
return sum * 1.0 / n;
}

int main ( )
{
int x [10], i;
cout << "Enter 10 integers: ";
for (i = 0; i <10; i++)
cin >> x [i];
cout << "Average = " << average (x, 10) << endl;
return 0;
}

Dr. Nour S. Bakr 01000-98-1103 dr.nour@ieee.org 22


Computer Programming using C++ Flowchart symbols
Dr. Nour S. Bakr Eng. Manar Fathy

Dr. Nour S. Bakr 01000-98-1103 dr.nour@ieee.org 23


Computer Programming using C++ Flowchart symbols
Dr. Nour S. Bakr Eng. Manar Fathy

Dr. Nour S. Bakr 01000-98-1103 dr.nour@ieee.org 24


Computer Programming using C++ Flowchart symbols
Dr. Nour S. Bakr Eng. Manar Fathy

Dr. Nour S. Bakr 01000-98-1103 dr.nour@ieee.org 25

You might also like