+ All Categories
Home > Documents > 1 Programming Application Overview of Structure Programming.

1 Programming Application Overview of Structure Programming.

Date post: 18-Jan-2016
Category:
Upload: byron-ford
View: 232 times
Download: 2 times
Share this document with a friend
63
1 Programming Application Overview of Structure Programming
Transcript
Page 1: 1 Programming Application Overview of Structure Programming.

1

Programming Application

Overview of Structure Programming

Page 2: 1 Programming Application Overview of Structure Programming.

2

C Program

#include <stdio.h>int main ( ){ printf(“ Hello”); return (0);}

Preprocessor Directive

Function header

Function body

Page 3: 1 Programming Application Overview of Structure Programming.

3

Standard Data Types in C

Integral Types represent whole numbers and their negatives declared as int, short, or long

Floating Types represent real numbers with a decimal point declared as float, or double

Character Types represent single characters declared as char

Page 4: 1 Programming Application Overview of Structure Programming.

4

Place Holders%d for an int

%c for a char

%f for a float

int age = 8;double height = 1.13;printf(“ Age is %d and height is %f “, age , height ) ;

Page 5: 1 Programming Application Overview of Structure Programming.

5

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.

Page 6: 1 Programming Application Overview of Structure Programming.

6

Some Math functions ( pages 98, 619 )

sin(x) cos(x) tan(x)

sqrt(x) pow(x,y)

log(x) log10(x) exp(x)

fabs(x) floor(x) ceil(x)

Page 7: 1 Programming Application Overview of Structure Programming.

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

Math in C complex math example-2

Page 8: 1 Programming Application Overview of Structure Programming.

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

,

 

Page 9: 1 Programming Application Overview of Structure Programming.

Control Structures

use logical expressions which may include:

6 Relational Operators

< <= > >= == !=

3 Logical Operators

! && ||

Page 10: 1 Programming Application Overview of Structure Programming.

Operator Meaning Associativity

! NOT Right

*, / , % Multiplication, Division, Modulus Left

+ , - Addition, Subtraction Left

< Less than Left

<= Less than or equal to Left

> Greater than Left

>= Greater than or equal to Left

== Is equal to Left

!= Is not equal to Left

&& AND Left

|| OR Left

= Assignment

Page 11: 1 Programming Application Overview of Structure Programming.

11

Use of blocks recommended

if ( Expression )

{

}

else

{

}

“if clause”

“else clause”

Page 12: 1 Programming Application Overview of Structure Programming.

int p, d=4, a=20;

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

{ p=d+a;

printf(“ Low Risk \n”);}

else

{ p=a-d;

printf(“ Low Risk \n”);}

printf(“ the p is = %d”, p);

Trace

Page 13: 1 Programming Application Overview of Structure Programming.

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 - -;

printf (“ % d “ , delta);

Page 14: 1 Programming Application Overview of Structure Programming.

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;

Page 15: 1 Programming Application Overview of Structure Programming.

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;

}

Page 16: 1 Programming Application Overview of Structure Programming.

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;

Page 17: 1 Programming Application Overview of Structure Programming.

17

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.

Page 18: 1 Programming Application Overview of Structure Programming.

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”.

 

Page 19: 1 Programming Application Overview of Structure Programming.

19

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.

Page 20: 1 Programming Application Overview of Structure Programming.

Find the output

if (x > 10)z= z*m;if ( y< 0)p = p *5;printf(“%d%d”, z, p);

if (x > 10)z= z*m;else if ( y< 0)p = p *5;printf(“%d%d”, z, p);

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

Page 21: 1 Programming Application Overview of Structure Programming.

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.

Page 22: 1 Programming Application Overview of Structure Programming.

Write a complete C program that asks a person for the number of hours he worked in a week and then print out his salary. Given that a person who worked 40 hours or less gets $5 per hour and a person who worked more than 40 hours gets $7 per hour for over 40 hours?

22

Page 23: 1 Programming Application Overview of Structure Programming.

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.

Page 24: 1 Programming Application Overview of Structure Programming.

24

int bright ;printf(“Enter the bulb brightness: “);scanf(“%d”, &bright);switch ( bright ){ case 25 : printf(“ Expected Lifetime is 2500 hours”); break; case 40 : case 60 : printf ( “Expected Lifetime is 1000 hours “); break; case 75 : case 100 : printf(“Expected Lifetime is 750 hours “); break; default : printf(“Wrong Input “);}

int bright ;printf(“Enter the bulb brightness: “);scanf(“%d”, &bright);switch ( bright ){ case 25 : printf(“ Expected Lifetime is 2500 hours”); break; case 40 : case 60 : printf ( “Expected Lifetime is 1000 hours “); break; case 75 : case 100 : printf(“Expected Lifetime is 750 hours “); break; default : printf(“Wrong Input “);}

Example

Page 25: 1 Programming Application Overview of Structure Programming.

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

Page 26: 1 Programming Application Overview of Structure Programming.

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;

}

printf(“ %d % d”, x , y);

Page 27: 1 Programming Application Overview of Structure Programming.

27

For Loop

SYNTAX

for ( initialization ; test expression ; update )

{

0 or more statements to repeat

}

Page 28: 1 Programming Application Overview of Structure Programming.

28

Example of Repetition

int num;

for ( num = 1 ; num <= 3 ; num++ )

{

printf( “ % d Potato \n ” , num );

}

Page 29: 1 Programming Application Overview of Structure Programming.

Write a program to display all the numbers divisible by 5 in the range 0 to 5000.

 

29

Page 30: 1 Programming Application Overview of Structure Programming.

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

Page 31: 1 Programming Application Overview of Structure Programming.

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

{

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

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

else printf(“Z”);

}

printf(“End”);

 

Page 32: 1 Programming Application Overview of Structure Programming.

j = 10;

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

{

printf(“%d %d \n “, i, j);

j = j-2;

}

Page 33: 1 Programming Application Overview of Structure Programming.

33

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

{

x = pow ( m , 2 );

printf(“ %d %d \n “, m , x );

}

Find the output

Page 34: 1 Programming Application Overview of Structure Programming.

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

Page 35: 1 Programming Application Overview of Structure Programming.

35

Write a program 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

Example

Page 36: 1 Programming Application Overview of Structure Programming.

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.

Page 37: 1 Programming Application Overview of Structure Programming.

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

Page 38: 1 Programming Application Overview of Structure Programming.

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

{

printf(“**”);

if(k %3 <=2)

{

k++;

}

else

{

printf(“%d \t “, k);

}

Page 39: 1 Programming Application Overview of Structure Programming.

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.

Page 40: 1 Programming Application Overview of Structure Programming.

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

Page 41: 1 Programming Application Overview of Structure Programming.

Locate 10 syntax errors in the code and write a correction for each error#include <stdo.h>

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 ) ;

printf(“%d” n x)

}

Page 42: 1 Programming Application Overview of Structure Programming.

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

{

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

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

else printf(“Z”);

}

printf(“End”);

Page 43: 1 Programming Application Overview of Structure Programming.

While Statement

SYNTAX

while ( Expression )

{ .

. /*loop body */

.

}

NOTE: Loop body can be a single statement, a null statement, or a block.

Page 44: 1 Programming Application Overview of Structure Programming.

int count ;

count = 4; /* initialize loop variable */

while (count > 0) /* test expression */

{

printf(“ %d \n ”,count ) ; /* repeated action */

count -- ; /*update loop variable */

}

printf( “Done” ) ;

Count-controlled Loop

Page 45: 1 Programming Application Overview of Structure Programming.

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

Page 46: 1 Programming Application Overview of Structure Programming.

Example

write a program that reads in the grades of

50 students in a course (out of 100 points

each ) and then count the number of A

students ( grade > 85 ) and the number of B

students (grade > 75 ).

Page 47: 1 Programming Application Overview of Structure Programming.

Example Write a C program that read ten integers

from the keyboard and returns the number of positive integers including zero?

47

Page 48: 1 Programming Application Overview of Structure Programming.

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

 

int N = 8;

 

do{

printf(“%d”, 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

 

Page 49: 1 Programming Application Overview of Structure Programming.

49

Do-While Statement

Is a looping control structure in which the loop condition is tested after each iteration of the loop.

SYNTAX

do{

Statements

} while ( Expression ) ;

Loop body statement can be a single statement or a block.

Page 50: 1 Programming Application Overview of Structure Programming.

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

Page 51: 1 Programming Application Overview of Structure Programming.

if x < 3

printf ( “Small value” ) ;

 

while ( 1 <= x < 10 )

x ++;

 

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

printf( “ n = %d \n “ , n ) ;

 

x = ( 3 + 4 ) / ( 3 * ( 7 + 2 ) ;

 

while ( a > 5 ) ;

{

printf ( “ * ” ) ;

a = a - 3 ;

}

Page 52: 1 Programming Application Overview of Structure Programming.

Tracem = 1;

prod = 1;do {

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

}while ( m < 6 ) ;printf (“ prod is %d \n “, prod )

Page 53: 1 Programming Application Overview of Structure Programming.

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

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

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

n++; }

printf ( “ sum1 = %d \n ” , sum1 ) ;printf ( “ sum2 = %d \n ” , sum2 ) ;

Page 54: 1 Programming Application Overview of Structure Programming.

#include<stdio.h>

main( )

{

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

do

{

y = y * x;

printf(“k = %d y = %d \n”, k, y);

y = y – k;

printf(“k = %d y = %d \n”, k, y);

k = k + 1;

}

while (k < 4);

}

Page 55: 1 Programming Application Overview of Structure Programming.

int I = 12, J = 0; while ( I > 8) { I = I -1; J = J + I; } Printf( “ % d % d”, I ,J);  

Page 56: 1 Programming Application Overview of Structure Programming.

56

ExampleA sensitive drug cannot sustain a change in temperature of

more than 30o C in a 24-hour period. The temperatures

are monitored and recorded every two hours. Write

console application the laboratory technician can use

once a day to determine whether or not to throw the drug

away.

Page 57: 1 Programming Application Overview of Structure Programming.

57

Example

In one university, it has a rule that at least 75% of student

in each course must pass. This means that the pass

mark for each course will differ. Write a program that

will read 100 students’ marks and determine the pass

mark.

Page 58: 1 Programming Application Overview of Structure Programming.

58

Example

A shuttle bus operates between two terminals

of an airport, departing every hour and half-

hour. Write a program that is given a time

(e.g. 2116), it will calculate how long it is

until the next bus departs (e.g. 14).

Page 59: 1 Programming Application Overview of Structure Programming.

59

Example Assume you put 1000 pounds in a projects

that returns a profit of about 5% per year. How long will it take for your money to double ?

Assume you put 5000 pounds in a projects that returns a profit of about 10% per year. How much money will you have in 5 years ?

Page 60: 1 Programming Application Overview of Structure Programming.

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

{

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

printf("%d", i+j);

}

Page 61: 1 Programming Application Overview of Structure Programming.

61

Example

A librarian covers books with plastic. She has a roll of plastic

2.2m wide and 100 m long. Books range between

20x14cm and 21x30cm and a book needs twice its size

plus a border of 4cm of plastic. As books come in, she

works across the roll. When book does not fit (first

horizontally, then vertically) she cuts a straight line across

the plastic and starts a new row. Generate random book

size and show for 20 books how the plastic will be used.

Page 62: 1 Programming Application Overview of Structure Programming.

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

Page 63: 1 Programming Application Overview of Structure Programming.

Example 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 boolean method in C# (not a whole class) that accepts an int argument called “year” and returns true if the year is a leap year and false otherwise.

63


Recommended