1 programming 2 overview of programming 1. write the equations in c++ notation : a) r = a + b 24 ...

Post on 17-Jan-2016

221 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

1

Programming 2

Overview of Programming 1

Write the equations in C++ notation :

a) R = a + b 24

2 a

b) W = a12 + b2 – 2abcos(y) 2a

The value that printed is

 

int delta = 0, x = 3;

if (x / 2 = = 1)

delta = delta + x;

x - -;

if (x / 2 = = 0)

delta = delta + x;

x - -;

Cout<<delta;

What is value of x when y is 15.0?

x = 25.0;

if (y != (x-10.0) )

x=x-10.0;

else

x=x / 2.0;

What is the value is assigned to x when y is 15.0?if (y < 15.0) if (y>= 0.0) x=5*y; else x = 2*y;else x= 3 * y;

Programming Examples

Write a program to ask the user for the width and length of a piece of land and then tell him how many orange trees he can grow on it. Given that each orange tree requires 4 m2.

7

ExampleThe Air Force has asked you to write a program to

label aircrafts as military or civilian. Your program input is the plane’s speed and its estimated length. For planes traveling faster than 1100 km/hr, you will label those shorter than 52 m “military”, and longer as “Civilian”. For planes traveling less than 1100, you will issue an “aircraft unknown” statement.

Find the output

if (x > 10)z= z*m;if ( y< 0)p = p *5;cout<<z<< p;

if (x > 10)z= z*m;else if ( y< 0)p = p *5;cout<<z<< p;

•For x=15, y=-5, z=2, p =3 and m=10, find the output of the •following code

A factory has some workers and it divides its workers into three skill levels. Unskilled workers receive L.E. 8.15 per hour, semiskilled workers receive L.E. 12.55 an hour, and skilled workers L.E. 18.60 an hour. Write a program to calculate the worker’s weekly pay formatted to two decimal places. Your program input should consist of the hours worked and a skill level indicator for the worker. Then the wage should be displayed.

int p, d=4, a=20;

if ((d==4) && (a>24))

{ p=d+a;

Cout<<“ Low Risk”;}

else

{ p=a-d;

Cout<<“ Low Risk ”;}

Cout<<“ the p is =“<<p;

Trace

Write a program to get the third side of a triangle (a), given the lengths of the other two sides (b, and c), and the angle using the formula

a2 = b2 + c2 – 2bc cos

b

rad_angle = alpha * PI / 180;

a = sqrt(pow(b,2) + pow(c,2) – 2 * b * c * cos(rad_angle);

c

a

complex math example-2

If--Else for a mail order

Write a program to calculate the total price of a certain purchase. There is a discount and shipping cost:

The discount rate is 25% and the shipping is 10.00 if purchase is over 100.00.Otherwise, The discount rate is 15% and the shipping is 5.00 pounds.

What is the value of variable z after the following code is executed?

 int w = 5, x = 7, y = 9, z = 1;

if (x % y > = 2 + w)

{

z+ +;

if (y – 3 * w > = - x)

z - -;

else

z + +;

}

else

{

z = - 1;

}

int z; z = 10 + 8 / 3 * 2 + 10 % 3; switch (z) { case 23 : cout<<“ Very High”; break; case 21 : cout<<“ High “; break; case 18 : cout<<“ Med “; break; case 15 : cout<<“ Low “; break; case 10 : cout<<“ Very Low “; break; default : cout<<“ None “; break; }

#include<iostream>

main( )

{

int x=2, y=2, k=1;

do

{

y = y * x;

Cout<<“k = “ <<k<<“ y =\n”<< y;

y = y – k;

Cout<<“k = “ <<k<<“ y =\n”<< y;

k = k + 1;

}

while (k < 4);

}

Example Write a function to ask the user for a positive

integer, and then display its factorial. Given that factorial(n) is 1 X 2 X 3 …… x n

int I = 12, J = 0; while ( I > 8) { I = I -1; J = J + I; } Cout<< I <<J;

 

Trace length = 10; count = 2; while ( count < 5 ) { if ( length >= 10 ) length = length – 2; else length = length * count; count++; } cout<<“ The length is ”<< length;

Tracem = 1;

prod = 1;do {

prod = prod * m ;m = m + 1 ;

}while ( m < 6 ) ;cout<<“ prod is “ << prod ;

n = 0 ; sum1 = 0 ;sum2 = 0 ;while ( n < 10 ) {

if ( n % 3 = = 0 )sum1 = sum1 + n ;

if ( n % 2 = = 0 )sum2 = sum2 + n ;

n++; }

Cout<<“ sum1 = ”<< sum1 ;Cout<<“ sum2 = ” << sum2 ;

Write a program to calculate and display a table of n vs n3, as shown below:

1 1

2 8

3 27

4 64

5 125

6 216

7 343

8 512

9 729

10 1000

Write a C program that display the number of odd numbers between its two integers:

Integer 1 integer 2 results 1 -2 1 1 5 1 2 4 1 0 10 5

To produce the output: 8 17 35 71, the condition of while loop should be:

 

int N = 8;

 

do{

cout<<N;

N = N * 2 + 1;

} while ( ? ? ? ?);

 

 

 

N <= 71 2) N < 71 3) N < 35 4) N > = 8 5) N > 143

6) none of the above

 

Write a program that reads 100 integer numbers, determines how many positive and negative values have been read, and computes the average of the input values.

A shop need to keep an inventory of its 100 items for sale. Write a console application to input the item code, price and quantity of each item. Then the program displays a neat table of all items (code, price and quantity) with two asterisks ** next to items whose price > 100 pounds, and one asterisk * next to items whose price is between 50 and 100 pounds.

for(k=0; k<5; k++)

{

cout<<“**”;

if(k %3 <=2)

{

k++;

}

else

{

cout<<k;

}

Write a C program that reads in 30 integer numbers and then print out their sum and average

for (int N = 1; N < 10; N++)

{

if ( N % 2 == 0) cout<<“X”;

else if ( N % 3 == 0) cout<<“Y”;

else cout<<“Z”;

}

cout<<“End”;

 

Find the output int x= 5, y= 2;

char op = ‘*’;

switch (op)

{

case ‘+’ : x = x+y;

case ‘-‘ : x=x- y; break;

case ‘*’ : x= x * y ;

case ‘/’ : x = x / y; break;

default : x =x + 1; break;

}

cout<<x << y;

j = 10;

for (i=1; i<=5; ++i)

{

cout<<i<< j;

j = j-2;

}

31

for ( m=1 ; m<=9 ; m=m+2 )

{

x = pow ( m , 2 );

cout<<m<< x;

}

Find the output

Write a program that reports the speed of the wind. The program input is 100 speed of wind:

 Wind speed Content

Below 25 Not strong

25 - 38 Strong wind

39 - 54 Gale

55 - 72 Whole gale

above 72 Hurricane

Find output n = 5; j = 1; while ( j < 4 ) { if ( n >= 10 ) n = n – 2; else n = n * j; j++; } cout<< “ The n is <<n;

Locate 10 syntax errors in the code and write a correction for each error

#include <ioostrem>

float main

int x=2, n;

switch ( 1 <= x < 10 )

case 2: x ++:

case 3: x - -:

for ( n=1 , n<10 , n++ )

{

scanf( %d “ , n, a ) ;

b = = 3a + 1 ) % ( a + ( 18 - a % 3 ) ;

cout<<(n x)

}

Write a program that reads a number in range 0-100 and print ‘F’ if the number is less than 50, ‘P’ if the number is in the range 50-59, and ‘S’ if the number is greater than 65.

Write a program that reads the value of x and calculates the values of y and z using the following formulas:

,

 

Find the outputfor (i=1; i<5; i++)

{

for (j=0; j<I; j++)

cout<<i+j;

}

Write a C program using functions to ask the user to enter two real numbers and read them. Then, your program will display the following menu:

Select an operation:

1. Addition

2. Multiplication

3. Power

The user will select the operation by entering a number from 1 to 3. After that your program will display the entered numbers with the performed operation using functions and the result up. If the selected operation is invalid, display “Invalid operation”.

 

Example Write a C++ program to calculate the

following formula for any given n. The program should read n from the user.

n

xxxx n

2...

6421

2642

Trace the following code

void main()

{

int a = 2;

int b = 2;

int c = 3;

b = mult( a , c ) ;

cout<<a<<b <<c ;

c = squares( a , b ) ;

cout<<a<<b <<c ;

int mult (int x, int y )

{

int z = x * y ;

return z;

}

int squares ( int p , int q )

{

int r = p * p + q * q ;

return r ;

}

A year with 366 days is called a leap year. A year is a leap year if it is divisible by 4 (for example, 1980). However, since the introduction of the Gregorian calendar on October 15, 1582, a year is not a leap year if it is divisible by 100 (for example, 1900); however, it is a leap year if it is divisible by 400 (for example, 2000). Write a C Program that accepts an int argument called “year” and print out if the year is a leap year or not.

top related