Download presentation

Presentation is loading. Please wait.

1
Lab 5 rC language Elements rVariables Declarations and Data Types rGeneral Form of a C Program rArithmetic Expressions rFormatting Numbers rExercises Note: Look at String.h (Appendix B AP16), time.h (Appendix B AP16). Be Careful Read Chapter 4(All Self-Check exercises ). C KEYWORDS and format must be learned by heart

2
Hello, World 1./* Hello, world program */ 2.#include 3. 4.int main(void) 5.{ 6. printf("hello, "); 7. printf("world\n"); 8. return 0; 9.}

3
Basic Programming Concepts Comment To explain what the program is for and why it is the way it is. Preprocessing Done before the program is compiled. To include header files containing information about C libraries Statement A line of code that tells computer to perform some action. Always ended with a semicolon(;). Function A module often consisting of a set of statements. Two functions in the hello world program: main, printf

4
Arithmetic Operators To form expressions Many statements are merely expressions. Normal order of operations is followed. Parentheses can be used. +Addition -Subtraction *Multiplication /Division % Modulus (remainder) ++Increment --Decrement

5
Arithmetic Operators: An Example 1./* Arithmetic operators */ 2.#include 3. 4.int main(void) 5.{ 6. printf("7+3=%d\n",7+3); 7. printf("7-3=%d\n",7-3); 8. printf("7*3=%d\n",7*3); 9. printf("7/3=%d\n",7/3); 10. printf("7%3=%d\n",7%3); 11. printf("7.0/3.0=%f\n",7.0/3.0); 12. return 0; 13.}

6
Variables Variable Name for a memory object. Variable names must start with letters and contain letters, digits, and underscores. a, b, i, j, counter, number_of_points, c1234, … Type What the variable represents. Could be of integer, floating point number, character, string, and many others. int, float, double, char, … Declaration Tells compiler about variables and their types. int i,j; float sum;

7
Numeric Types intinteger floatfloating point charcharater short or short intshort integer long or long intlong integer doublelong floating point long doublevery long floating point

8
Rules of Evaluating Expressions A-Parentheses rules: all expressions in parentheses must be evaluated separately. Nested parenthesized expressions must be evaluated from the inside out. B-Operator Precedence: unary+, - *, /,% binary +,- C-Associativity rule: Unary operators are evaluated right to left. Binary operators are evaluated left to right. Exp: x*y*z +a /b –c*d area=PI*radius*radius

9
Reading Inputs 1.#include 2.int main(void) 3.{ 4. float a,b; 5. printf("Please input the first number:\n"); 6. scanf("%f",&a); 7. printf("Please input the second number:\n"); 8. scanf("%f",&b); 9. printf("a+b=%f\n",a+b); 10. printf("a-b=%f\n",a-b); 11. printf("a*b=%f\n",a*b); 12. printf("a/b=%f\n",a/b); 13. return 0; 14.}

10
Assignment 1./* Arithmetic operators */ 2.#include 3. 4.int main() 5.{ 6. int a,c; 7. int b=4; 8. a=3; 9. c=a+b; 10. printf(“Sum: %d + %d -> %d\n”,a,b,c); 11. a++;b--; 12. printf(“Now a=%d and b=%d\n”,a,b); 13. return 0; 14.}

11
Printf Function printf(format string, print list) Exp: printf(“I am %d years old and my gpa is %f \n”,age,gpa); Placeholder: is a symbol beginning with % that indicates where and how to display the output value. (%c char, %d int, %f float and %lf for double). Exr1: if letter_1 holds ‘E’, letter_2 holds ‘A’ (they are of type char) and age of type int holds 24, what should be displayed by the following: printf(“Hi %c%c- your age is %d \n”, letter_1, letter_2, age);

12
Scanf Function scanf (format string, input list) Exp: scanf (“%c%d”,&first_initial,&age); Each input is preceded by ampersand &. Commas are used to separate variable names. The order of the placeholders must correspond to the order of variables in the input list. Exr2:Write a C program that asks the user to enter the radius of a circle and then displays the area and the circumference of the circle.PI is the constant macro 3.14159 (use #define)

13
Formatting numbers Format Value Displayed Output %6d 234 %1d 234 %6d -234 %1d -234 %5.1f 3.14159 %.4 -3.14159

14
Other Exercises rThe New-Wave Computer Company sells its product, the NW-PC for 7000 Dhs. In addition, it sells memory extension cards for 750 Dhs, disk drives for 2000 Dhs, and software for 350 Dhs. Given the number of memory cards, disk drives, and software packages desired by a customer purchasing an NW-PC, write a C to print out a bill of sale. rWe would like to develop an algorithm that produces a student’s interim reports for a computer science class. To solve this problem you need to know the following: l There are 3 tests, each worth 10% l All the Quizzes are worth 10% l Projects and assignments are worth 30% l The Final Exam is worth 30% Write a C program that given the grades of all of the above components calculates the final average grade.

Similar presentations

© 2019 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google