Getting Started - Al Akhawayn University

Download Report

Transcript Getting Started - Al Akhawayn University

Lab 5
 C language Elements
 Variables Declarations and Data Types
 General Form of a C Program
 Arithmetic Expressions
 Formatting Numbers
 Exercises
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
Hello, World
1. /* Hello, world program */
2. #include <stdio.h>
3.
4. int main(void)
5. {
6.
printf("hello, ");
7.
printf("world\n");
8.
return 0;
9. }
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
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
%
++
--
Arithmetic Operators: An Example
1. /* Arithmetic operators */
2. #include <stdio.h>
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.}
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;
Numeric Types
int
integer
float
floating point
char
charater
short or short int
short integer
long or long int
long integer
double
long double
long floating point
very long floating point
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
Reading Inputs
1. #include <stdio.h>
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.
10.
11.
12.
13.
14.}
printf("a+b=%f\n",a+b);
printf("a-b=%f\n",a-b);
printf("a*b=%f\n",a*b);
printf("a/b=%f\n",a/b);
return 0;
Assignment
1. /* Arithmetic operators */
2. #include <stdio.h>
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.}
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);
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)
Formatting numbers
Format
Value
%6d
234
%1d
234
%6d
-234
%1d
-234
%5.1f
3.14159
%.4
-3.14159
Displayed Output
Other Exercises
 The 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.
 We 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:

There are 3 tests, each worth 10%

All the Quizzes are worth 10%

Projects and assignments are worth 30%

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.