Anda di halaman 1dari 14

BSC212

PROGRAMMING I
FOR ENGINEERING
LAB MANUAL
MSA BSC212
Faculty of Computer Science Programming I for Engineering
2
Table of Contents
1. Variables:......................................................................................................... - 3 -
2. Control Structure: ........................................................................................... - 5 -
A. IF- Then: ..................................................................................................... - 5 -
B. Switch: ......................................................................................................... - 7 -
3. Loops:............................................................................................................... - 8 -
4. Functions: ........................................................................................................ - 9 -
5. Arrays: ........................................................................................................... - 11 -
A. One Dimensional Array:........................................................................... - 11 -
B. Two Dimensional Array: .......................................................................... - 13 -
MSA BSC212
Faculty of Computer Science Programming I for Engineering
3
1. Variables:
1. Write a program to read three numbers from the user then display sum and
average.
2. Write a program that accept X, Y for two points P
1
= (X
1
, Y
1
), P
2
= (X
2
, Y
2
)
and calculate the Slope (m) =
X1 - X2
Y1 - 2 Y
3. Write a program to calculate Total Resistance of three resistances in parallel.
Rt =
3 2 1
3 * 2 * 1
R R R
R R R
+ +
4. Write a program to calculate Total Resistance of four resistances in circuit,
three of them connected in parallel and the last one connected in series with
them.
Rt = R in parallel + R in series.
5. Write a program to calculate I total (It) of a circuit contains voltage source
(Vs) and five resistances connected in parallel.
It = Vs / Rt
6. X is the mathematic mean two numbers X =
2
b a +
Y is the geometric mean Y = ab
Z is the harmonic mean Z =
b a
1 1
2
+
Write a C++ program that accepts two inputs a, b then display X, Y, Z.
7. If you know the lengths of two sides ( b and c) of triangle and the angle
between them in degree (Q) ,we can compute the length of the third side (a)
using the following formulas
Q bc c b a cos 2
2 2 2
+ + =
2 = b 4 = c 78 = Q
8. Given the lengths a, b, c of the sides of a triangle, write a function to compute
the area A of a triangle. The formula for computing A is given by
A= ) )( )( ( c s b s a s s - - -
Where S is the semi perimeter of the triangle S =
2
c b a + +
Write a program to get values for a, b, and c and compute A.
c
b
a
MSA BSC212
Faculty of Computer Science Programming I for Engineering
4
9. Write a program to ask the user about the distance in(kilo meter) and the time
in hours then display the speed in the four forms:
Speed in kilo meter per hour.
Speed in kilo per minute.
Speed in meter per second.
Speed in meter per minute.
MSA BSC212
Faculty of Computer Science Programming I for Engineering
5
2. Control Structure:
A. IF- Then:
1. Create the equivalent of a four-function (+, - ,*, / ) the program should
ask the user to enter a number, an operator, and another number. It
should then display the operation: adding, subtracting, multiplying, or
dividing the two numbers.
2. Write a program to read 3 numbers from the user and then ask the user
to select from the following options:
a) Display all odd numbers.
b) Display the average of the even numbers.
c) Count numbers which dividable by 5.
d) Display the maximum number.
3. Write a program to do the following:
Ask the user to enter 3 values
Ask the user to enter a number N
Ask the user to enter a number M
Calculate the average of values divisible by N
Calculate the average of values divisible by M
Display the difference between the two averages
Example:
Input:
Please enter 3 values : 45,15,24
Please enter a number N: 5
Please enter a number M: 3
Output:
Average of values divisible by N is: 30 (because
(45+15)/2).
Average of values divisible by N is: 28 (because
(45+15+24)/3).
4. Write a program to read two numbers from the user and display the
relation between them (greater than, less than, greater than or equal,
less than or equal or equals).
5. Write a program that read two numbers from the user and calculate the
summation of numbers between a and b, make sure that the second
number (b) is greater than the first number (a) otherwise display
The second number should be greater than the first.
6. You are developing a program to control the warning signs as the
exists of major tunnels. If roads are slick ( road_status is S) , you
want to advise drives that stopping times are doubled or quadrupled ,
depending on whether the road are wet or icy. Your program will
also have access to the current temperature in degrees Celsius (temp),
MSA BSC212
Faculty of Computer Science Programming I for Engineering
6
so a check as to whether the temperature is above or below freezing
would allow you to choose the correct message. If the road wet you
will tell the drivers and tell him that stopping time is doubled. If the
road is icy tell the drivers that and the stopping time is quadruple.
Otherwise tell him to Drive carefully
7. Write the program that read a number from user , then do the
following:
a) Check if the number is greater that 20 then do the following:
If this number is 24 then display you entered 24
If this number is 32 then display you entered 32
Else of the above then do the following
Display the next text the number is greater than 20 but not
24 or 32
If the number is divisible by 5 and divisible by 3 then
display you enter a number divisible by 3 and 5.
Else print on screen not divisible by 3 and 5
b) And if the number is less than or equal to 20 then print
number to screen.
MSA BSC212
Faculty of Computer Science Programming I for Engineering
7
B. Switch:
1. Write a program to read 3 numbers from the user and then ask the user
to select from the following options:
Display all odd numbers.
Display the average of the even numbers.
Count numbers which dividable by 5.
Display the maximum number.
2. Write a program to calculate F(x) based on the following:
F(x) = 3x + 1 if X >100
F(x) = 7x + 2x +5 if X < 100
F(x) = 2 if X = 100
3. Create the equivalent of a four-function, the program should ask the
user to enter a number, an operator, and another number. It should then
display the operation: adding, subtracting, multiplying, or dividing the
two numbers. Use a switch statement to select the operation.
MSA BSC212
Faculty of Computer Science Programming I for Engineering
8
3. Loops:
1. Write a program to calculate summation of even numbers between 2 and 100.
2. Write a program to calculate factorial of a number
F=x!
Ie: 5! =5*4*3*2*1
Example:
Enter a number: 5
Result =120.
3. Write a program to find the values of the function F(x) = X
2
+ 3X 4 at
X = 1 to X = 5
4. Write a program that calculate graph points of equation Y = 2X + 5 from
0 to 20 by step 5.
X = 0 Y = 5 (0, 5)
X = 5 Y = 15 (5, 15)
5. Write a program to calculate:
F=
y
x
Example:
Please enter coefficient: 5
Please enter power: 2
6. Write a program that calculates the squares and cubes of numbers from 0 to 10
and display the number, its square and its cubes as follows:
Number Square Cubes
0 0 0
1 1 1
2 4 8
MSA BSC212
Faculty of Computer Science Programming I for Engineering
9
4. Functions:
1. Write a program that contains function square to calculate the squares of
the integers from 1 to 10.
2. Write a program that ask user to enter a number and make a function to
determine if this number odd or even.
3. Write a program that take Length and Width of Rectangle from the user
and make two functions:
o Area: That calculates area of the Rectangle and display the result.
o Perimeter: That calculates perimeter of the Rectangle and display the
result.
4. Write a program that take three values from the user and make two
functions:
Maximum: That determines the maximum number and returns.
Minimum: That determines the minimum number and returns
5. Write a function to calculate Quotient and Remainder for two numbers.
Example:
Divide 37 by 8
Quotient = 4
Remainder = 5
6. Write a function that calculate midpoint of line from points P
1
= (X
1
, Y
1
),
P
2
= (X
2
, Y
2
) and display the result.
(X, Y) = ( (X
1
+ X
2
) / 2 , (Y
1
+ Y
2
) / 2 )
7. Write a function IntegerPower (Base, Exponent) that returns the value of
Base
Exponent.
For example IntegerPower (3,4) = 3*3*3*3. Assume that
exponent is positive, nonzero integer and base is an integer.
8. Two positive numbers I and j are considered to be relatively prime if there
exists no integer greater then 1 that divides them both .Write a function
replm that has two inputs parameters and return 1 if true if and only if I
and j relatively prime otherwise return false
9. Write a function to calculate the Distance between two points
P
1
= (X
1
, Y
1
), P
2
= (X
2
, Y
2
) and display result from main.
d = (X
2
X
1
)
2
+ (Y2 Y
1
)
2
10. Write a function to find Greatest Common Divisor (GCD) for two
numbers.
Example:
A = 456 B = 624
GCD = 24
MSA BSC212
Faculty of Computer Science Programming I for Engineering
10
11. Suppose you are biologist written a program to report statistics about the
growth rate of a fruit fly population. You will compute the percentage of
population increase over one day period .first you will determine todays
population and yesterdays population make a function to get the result if
and only if todays population greater than yesterdays population.
MSA BSC212
Faculty of Computer Science Programming I for Engineering
11
5. Arrays:
A. One Dimensional Array:
1. Write a C++ program that accepts an array of 20 elements then apply the
following operations :
A. Reverse the order of the array and display it.
B. Display the sum and the average of the array.
C. Allow user to input a number and search for this number in the array.
2. Write a program to do the following:
Read 20 values from the user.
Ask the user about a number N.
Display how many numbers of N in this array.
3. Write a program that accepts an array of ten elements and an integer value
then find scalar multiplication.
4. Write a program to find intersection between array of 10 elements and array of
3 elements.
Example:
A[10] = { 1,2,3,4,5,6,7,8,9,10 }
B [3] = {2, 5, 11}
Result: 2, 5
5. Write a program to find Union between array of 10 elements and array of 3
elements.
Example:
A[10] = { 1,2,3,4,5,6,7,8,9,10 }
B [3] = {2, 5, 11}
Result: 1,2,3,4,5,6,7,8,9,10,11
6. Write a program to find complement of the set A [3] from the set U [7].
Example:
U [7] = {1, 2, 3, 4, 5, 6, 7}
A [3] = {2, 7, 9}
Result: 1, 3, 4, 5, 6, 7
7. The power consumed by a heater can be calculated b the equation P=VI
Where P = power (watts)
V= voltage (volts)
I= current (amperes)
Write a program to calculate the power consumed by various types of heaters.
The input as:
Voltage Current
110 5.5
220 23.5
90 13.6
370 44.4
MSA BSC212
Faculty of Computer Science Programming I for Engineering
12
8. Write a C++ program to do the following:
Ask the user to input 5 numbers in an array.
Ask the user for a number A.
Calculate how many numbers in the array = absolute value of A
MSA BSC212
Faculty of Computer Science Programming I for Engineering
13
B. Two Dimensional Array:
1. Write a program to read 2-D array from the user of size 3x3. Then
I. Replace all odd numbers by 0.
II. Display the positions of odd numbers and the odd number.
III. Display array after replacement.
Example:
1 2 3
4 5 6
7 8 9
0 2 0
4 0 6
0 8 0
Out Put:
0,0 1
0,2 3 0 2 0
1,1 5 4 0 6
2,0 7 0 8 0
2,2 9
3. Write a program to store Brightness for each point of a photo 10 x 10 and
display the Brightest pixel value and the X, Y coordinates for this point.
4. Write a program that ask user to enter the values of two matrices 2x3 and
display the summation and subtraction matrices.
5. Write a program that accepts two matrices first 2x3 and the second 3x4 and
calculate the multiplication of the two matrices then display result.
6. Write a program to read a matrix (3x3) and ask the user to chose a number and
then display the position of the select number if found, otherwise display
number not found
Example:
Please enter the number you want to search for: 30
The number is found at: 0,1 2,2
Or
The number is not found.
12 30 10
14 40 20
16 50 30
MSA BSC212
Faculty of Computer Science Programming I for Engineering
14
7. Write a program to read a matrix (NxM) form the user. The user shall enter N
and M where N and M should be even numbers. Then do the following:
Ask the about a specific column, then swap this column with its next.
If next column exceeds the last column then start form the first column.
8. Write a program to check if a set of numbers can form the magic square or
not. The magic square is a matrix of natural numbers in which the sums of all
the rows, columns and the two major diagonals are equal. Assume the size of
the matrix is 4x4.
Write a function to get the natural numbers from the user
Write a function to check these numbers can form the magic square or not.
Example for 4x4 magic square
9. Write program to check if a matrix is a mirror or not (a matrix is mirror if the
sum of the first half of the matrix is equal to the total of the second half of
matrix).
Example:
1 2 3 4
5 6 7 8
12 33 45 44
2 3 4 0
1 0 11 15
This matrix is mirror.
2 12 7 14 35
8 13 2 11 34
10 3 16 5 34
15 6 9 4 34
35 34 34 34 35
is not a magic square
1 12 7 14 34
8 13 2 11 34
10 3 16 5 34
15 6 9 4 34
34 34 34 34 34
is a magic square
1 12 7 14 Sum
8 13 2 11 Sum
10 3 16 5 Sum
15 6 9 4 Sum
Sum Sum Sum Sum Sum

Anda mungkin juga menyukai