Final BSC212

download Final BSC212

of 14

Transcript of Final BSC212

  • BSC212

    PROGRAMMING IFOR ENGINEERINGLAB MANUAL

  • MSA BSC212Faculty 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 BSC212Faculty of Computer Science Programming I for Engineering

    3

    1. Variables:

    1. Write a program to read three numbers from the user then display sum andaverage.

    2. Write a program that accept X, Y for two points P1 = (X1, Y1), P2 = (X2, Y2)

    and calculate the Slope (m) =X1-X2Y1-2Y

    3. Write a program to calculate Total Resistance of three resistances in parallel.

    Rt =3213*2*1

    RRRRRR

    ++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 withthem.

    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

    ba +

    Y is the geometric mean Y = ab

    Z is the harmonic mean Z =

    ba11

    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 anglebetween them in degree (Q) ,we can compute the length of the third side (a)using the following formulas

    Qbccba cos2222 ++=2=b 4=c 78=Q

    8. Given the lengths a, b, c of the sides of a triangle, write a function to computethe area A of a triangle. The formula for computing A is given by

    A= ))()(( csbsass ---

    Where S is the semi perimeter of the triangle S =2

    cba ++

    Write a program to get values for a, b, and c and compute A.

    cb

    a

  • MSA BSC212Faculty of Computer Science Programming I for Engineering

    4

    9. Write a program to ask the user about the distance in(kilo meter) and the timein 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 BSC212Faculty of Computer Science Programming I for Engineering

    5

    2. Control Structure:

    A. IF- Then:

    1. Create the equivalent of a four-function (+, - ,*, / ) the program shouldask the user to enter a number, an operator, and another number. Itshould then display the operation: adding, subtracting, multiplying, ordividing the two numbers.

    2. Write a program to read 3 numbers from the user and then ask the userto 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 therelation 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 thesummation of numbers between a and b, make sure that the secondnumber (b) is greater than the first number (a) otherwise displayThe second number should be greater than the first.

    6. You are developing a program to control the warning signs as theexists of major tunnels. If roads are slick ( road_status is S) , youwant to advise drives that stopping times are doubled or quadrupled ,depending on whether the road are wet or icy. Your program willalso have access to the current temperature in degrees Celsius (temp),

  • MSA BSC212Faculty of Computer Science Programming I for Engineering

    6

    so a check as to whether the temperature is above or below freezingwould allow you to choose the correct message. If the road wet youwill 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 thefollowing:

    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 printnumber to screen.

  • MSA BSC212Faculty 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 userto 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 >100F(x) = 7x + 2x +5 if X < 100F(x) = 2 if X = 100

    3. Create the equivalent of a four-function, the program should ask theuser to enter a number, an operator, and another number. It should thendisplay the operation: adding, subtracting, multiplying, or dividing thetwo numbers. Use a switch statement to select the operation.

  • MSA BSC212Faculty 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 numberF=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) = X2 + 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= yx

    Example:Please enter coefficient: 5Please enter power: 2

    6. Write a program that calculates the squares and cubes of numbers from 0 to 10and display the number, its square and its cubes as follows:

    Number Square Cubes 0 0 0 1 1 1 2 4 8

  • MSA BSC212Faculty of Computer Science Programming I for Engineering

    9

    4. Functions:

    1. Write a program that contains function square to calculate the squares ofthe integers from 1 to 10.

    2. Write a program that ask user to enter a number and make a function todetermine if this number odd or even.

    3. Write a program that take Length and Width of Rectangle from the userand 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 = 4Remainder = 5

    6. Write a function that calculate midpoint of line from points P1 = (X1, Y1),P2 = (X2, Y2 ) and display the result.(X, Y) = ( (X1 + X2) / 2 , (Y1 + Y2) / 2 )

    7. Write a function IntegerPower (Base, Exponent) that returns the value ofBase Exponent. For example IntegerPower (3,4) = 3*3*3*3. Assume thatexponent is positive, nonzero integer and base is an integer.

    8. Two positive numbers I and j are considered to be relatively prime if thereexists no integer greater then 1 that divides them both .Write a functionreplm that has two inputs parameters and return 1 if true if and only if Iand j relatively prime otherwise return false

    9. Write a function to calculate the Distance between two pointsP1 = (X1, Y1), P2 = (X2, Y2) and display result from main.

    d = (X2 X1)2 + (Y2 Y1)2

    10. Write a function to find Greatest Common Divisor (GCD) for twonumbers.Example: A = 456 B = 624

    GCD = 24

  • MSA BSC212Faculty of Computer Science Programming I for Engineering

    10

    11. Suppose you are biologist written a program to report statistics about thegrowth rate of a fruit fly population. You will compute the percentage ofpopulation increase over one day period .first you will determine todayspopulation and yesterdays population make a function to get the result ifand only if todays population greater than yesterdays population.

  • MSA BSC212Faculty 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 thefollowing 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 valuethen find scalar multiplication.

    4. Write a program to find intersection between array of 10 elements and array of3 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 3elements.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=VIWhere 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 Current110 5.5220 23.590 13.6370 44.4

  • MSA BSC212Faculty 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 BSC212Faculty 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. ThenI. 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 34 5 67 8 9

    0 2 04 0 60 8 0

    Out Put:0,0 10,2 3 0 2 01,1 5 4 0 62,0 7 0 8 02,2 9

    3. Write a program to store Brightness for each point of a photo 10 x 10 anddisplay 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 anddisplay the summation and subtraction matrices.

    5. Write a program that accepts two matrices first 2x3 and the second 3x4 andcalculate 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 andthen display the position of the select number if found, otherwise displaynumber not found

    Example:

    Please enter the number you want to search for: 30The number is found at: 0,1 2,2OrThe number is not found.

    12 30 10

    14 40 20

    16 50 30

  • MSA BSC212Faculty of Computer Science Programming I for Engineering

    14

    7. Write a program to read a matrix (NxM) form the user. The user shall enter Nand 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 ornot. The magic square is a matrix of natural numbers in which the sums of allthe rows, columns and the two major diagonals are equal. Assume the size ofthe 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 thesum of the first half of the matrix is equal to the total of the second half ofmatrix).Example:

    1 2 3 45 6 7 812 33 45 442 3 4 01 0 11 15

    This matrix is mirror.

    2 12 7 14 358 13 2 11 3410 3 16 5 3415 6 9 4 3435 34 34 34 35

    is not a magic square

    1 12 7 14 348 13 2 11 3410 3 16 5 3415 6 9 4 3434 34 34 34 34

    is a magic square

    1 12 7 14 Sum8 13 2 11 Sum

    10 3 16 5 Sum15 6 9 4 Sum

    Sum Sum Sum Sum Sum

    2. Control Structure:A. IF- Then:B. Switch:

    3. Loops:4. Functions:5. Arrays:One Dimensional Array:Two Dimensional Array: