1 / 63

Programming Application

Programming Application. Overview of Structure Programming. C Program. Preprocessor Directive. # include <stdio.h> int main ( ) { printf(“ Hello”); return (0); }. Function header. Function body. Standard Data Types in C. Integral Types

Download Presentation

Programming Application

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming Application Overview of Structure Programming

  2. C Program Preprocessor Directive #include <stdio.h> int main ( ) { printf(“ Hello”); return (0); } Function header Function body

  3. Standard Data Types in C • Integral Types • represent whole numbers and their negatives • declared as int, short, orlong • Floating Types • represent real numbers with a decimal point • declared as float, or double • Character Types • represent single characters • declared as char

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

  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.

  6. Some Math functions ( pages 98, 619 )

  7. Math in Ccomplex math example-2 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  a b  c rad_angle = alpha * PI / 180; a = sqrt(pow(b,2) + pow(c,2) – 2 * b * c * cos(rad_angle);

  8. Write a program that reads the value of xand calculates the values of yand zusing the following formulas: ,

  9. Control Structures use logical expressions which may include: 6 Relational Operators < <= > >= == != 3 Logical Operators ! && ||

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

  11. Use of blocks recommended if ( Expression ) { } else { } “if clause” “else clause”

  12. Trace 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);

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

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

  15. 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; }

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

  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.

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

  19. Example The 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.

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

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

  22. 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?

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

  24. Example 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 “); }

  25. 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; • }

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

  27. For Loop SYNTAX for ( initialization ; test expression ; update ) { 0 or more statements to repeat }

  28. Example of Repetition int num; for ( num = 1 ; num <= 3 ; num++ ) { printf( “ % d Potato \n ” , num ); }

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

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

  31. 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”);

  32. j = 10; for (i=1; i<=5; ++i) { printf(“%d %d \n “, i, j); j = j-2; }

  33. Find the output for ( m=1 ; m<=9 ; m=m+2 ) { x = pow ( m , 2 ); printf(“ %d %d \n “, m , x ); }

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

  35. Example 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

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

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

  38. for(k=0; k<5; k++) { printf(“**”); if(k %3 <=2) { k++; } else { printf(“%d \t “, k); }

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

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

  41. 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) }

  42. 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”);

  43. While Statement SYNTAX while ( Expression) { . . /*loop body */ . } NOTE: Loop body can be a single statement, a null statement, or a block.

  44. Count-controlled Loop int count ; count = 4; /* initialize loop variable */ while (count > 0) /* test expression */ { printf(“ %d \n ”,count ) ; /* repeated action */ count -- ; /*update loop variable */ } printf( “Done” ) ;

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

  46. 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 ).

  47. Example • Write a C program that read ten integers from the keyboard and returns the number of positive integers including zero?

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

  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.

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

More Related