Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sales person receive RM200/week plus 9% of their gross sales for that week. Write an algorithms to calculate the sales person’s earning from the input.

Similar presentations


Presentation on theme: "Sales person receive RM200/week plus 9% of their gross sales for that week. Write an algorithms to calculate the sales person’s earning from the input."— Presentation transcript:

1 Sales person receive RM200/week plus 9% of their gross sales for that week. Write an algorithms to calculate the sales person’s earning from the input of gross salary Input the first salesperson’s sales in dollars While the input is not -1 Calculate the salesperson’s wages for the week Print the salesperson’s wages for the week Input the next salesperson’s sales in dollars end while

2 Input the first salesperson’s sales in dollars While the input is not -1 //Calculate the salesperson’s wages for the week Wages = RM200 + (Sales * 9%) Print the salesperson’s wages for the week Input the next salesperson’s sales in dollars end while

3 Write an algorithms that inputs a series of 10 numbers, and determines and prints the largest of the numbers with 3 variable COUNTER- a counter to count to 10, keep track the number NUMBER - current input number LARGEST - largest number found so far Input the first number directly into the variable largest Increment counter to 2 While counter is less than or equal to 10 input a new variable into the variable number If number is greater than largest replace largest with number Increment counter Print the value of largest

4 Annual Interest = principal * rate * days /365 Develop an algorithm using pseudocode will input principal, rate, and days for several loans, and will calculate and display simple interest for each loan, using the formula Input the first loan principal in dollars While the input is not -1 Input the interest rate input the term of the loan in days calculate the simple interest for the loan Print the simple interest for the loan Input the loan principal for the next loan

5 The factorial of nonnegative integer n is written n! and defined as n!=n*(n-1)*(n-2)*…*1 for n greater than or equal to 1 and n!=1 for n=0. Write an algorithms that read a non -ve integer and computes and prints it factorial Do Input number positive while number is negative While number >=0 if number = 0 then factorial = 1 else factorial = factorial * n end if n=n-1 end while What wrong with this pseudocode ?

6 Introduction to C

7 C language character set & tokens n Reserved words n Identifiers n Constants n String constants n Punctuators n Operators

8 n Reserved words –keywords that identify language entities, such as statements, data types, and language element attributes –they have a special meaning to compiler –must appear in the correct location –C reserved word must be typed fully in lowercase –const, double, int

9 n Identifiers –when writing a program, we must come up with additional words to represent and reference certain program entities. –Needed for program variables, functions, and other program constructs –Rules for constructing identifiers n can consist of the capital letters A and Z, the lowercase letters a to z, the digits 0 to 9, and the underscore character n first character must be a letter or an underscore n virtually no length limitation, compiler only recognize first 32 character as significant n reserved word cannot be used n case sensitive

10 Constants n Four types of constants ; integer, floating point, character, and enumeration n Integer can be +ve or -ve numbers and can be decimal, octal or hexadecimal

11 /* File Name : prog1_1.c Definition : A simple program that displays a message. */ # include int main() { printf(“This is my first C program.\n”); return 0; } Output This is my first C program.

12 Parts of A C Program n Comments Lines –/*……..*/ –is used to comment your program while writing it n Library File –# include –# include –# indicates a preprocessor command –file with extension.h –stdio.h is a library file that contains standard I/O functions

13 n Main() Function –indicates the logical beginning of the program –consist of function header and function body –Function Header int main() n int indicates the function returns an integer value. –Function Body n{n{n{n{ printf(“This is my first c program.\n”); printf(“This is my first c program.\n”); return 0; }

14 n Statement format, –every statement will end with ; n output function, printf() n Escape character, \n –\b, back space –\r, carriage return –\\, backslash –\”, double quotation –\t, tab

15 Examples Printf(“\\ Hello \\ \t “ Good bye \” \n “); OUTPUT \ Hello \ “ Good bye “

16 Data Types in C n Basic Data Type –characterchar –integer numberint –floating-point numberfloat –double precision numberdouble –typeless/valuelessvoid

17 Class of Data n Character constants n string constants n Integer constants n floating-point constants

18 Displaying Numbers n Printf(“control string”, arg1, arg2,…); n example printf(“A Simple Control String”); Output : A Simple Control String printf(“The sum of 1 and 2 is %d), 1+2); Conversion specification argument

19 Conversion Specification %c - single character printf(“%c”,’A’);A %d - signed decimal integer printf(“%d”,10);10 %f - floating-point num.,decimal notation printf(“%f”,5.6);5.600000 %e - floating-point num., e notation printf(“%e”,52.6);5.26e1

20 Conversion Specificatiom %o octalprintf(“%o”,100) 120 %d decimal 100 %x hexadecimal 64 Floating point numbers printf(“[ 10.3f ]”, 21.12); [ 21.120 ] 10.3 = display the number in a total field of 10 and 3 digit to the right of the decimal point.

21 Conversion Specification Flags - (minus sign) left justify + display the + or - sign of the number blank display blank if +ve and - if -ve Example printf(“This number is : %+.2f \n”, 123.456); printf(“Next number is : %+d \n”, -123); OUTPUT This number is : +123.45 Next number : is -123

22 DISPLAYING CHARACTERS n The character representation Storage 0 1 0 0 0 0 0 1 “%c”A “%d”65

23 Displaying Formatted Strings printf(“[%-20s]”,”Pure C”); [Pure C ] [%20.4s] [ Pure] [-%20.4] [Pure ] [%.4s] [Pure]

24 #include #include int main() { printf(“[30s] \n”, “Universiti Malaysia Sarawak”); printf(“[%-30s] \n”, “Universiti Malaysia Sarawak”); printf(“[%-30s] \n”, “Universiti Malaysia Sarawak”); printf(“[%15.6s] \n”, “Universiti Malaysia Sarawak”); printf(“[%.6s] \n”, “Universiti Malaysia Sarawak”); printf(“[%.6s] \n”, “Universiti Malaysia Sarawak”); printf(“[%5s|] \n”, “Universiti Malaysia Sarawak”); printf(“[%5s|] \n”, “Universiti Malaysia Sarawak”); return 0; } [ Universiti Malaysia Sarawak] [ Univer] [Univer] [Universiti Malaysia Sarawak]

25 Variables Variable type declaration variable type variable list; variable type - must be valid C data type variable list - consists of one of more variable names separate by commas

26 Common Errors n Forgetting to declare variables before using them n placing variable declaration in the middle of the program n forgetting the data type or declaring a wrong data type n forgetting the ; at the end of the statement

27 Variable initializations n It is possible to assign an initial value to a variable when it is declared. int I, x; char ch = ‘A’ float f _number = 10.00; C does not automatically initialize declared variables

28 Variable Names/identifier n 1st character must be a letter or an underscore, rest can be combination of letters, underscores, or digit. n Case sensitive n number of character is compiler-dependent n whitespace character and special character (!,$,()) is not allowed within a name n no keyword and reserved word

29 Data Type Modifier n Integer modifiers - usually is 2 bytes Data Type Max Min Data Type Max Min short/short int 127-128 short/short int 127-128 int32767-32768 long/long int2147483647 -2147483648 unsigned short2550 unsigned or unsigned int 655350 unsigned long or unsigned long int4294967295 0

30 Data Type Modifier n Floating-Point modifiers - usually is 8 bytes –only one modifier, long double long double long_double; /* declare a long double type variable */

31 Declaration & Initialization Statement long int LongNumber; long Lnum = 100L; /* 100 is fit in short but with this declaration will forces to store in 4 bytes */ will forces to store in 4 bytes */ long A_Long = 5l; unsigned positive=35353U; unsigned long count1; short counter; long double Dlarge; long double LD=1.1e+3000

32 What You Have Learn ? n /* */ comment line n #include directivepreprocessor command n int main () n{n{n{n{ n declaration statements n initialization statements n statements n return 0; n}n}n}n}

33 Operators n A symbol performs a specific mathematical or logical operation. n Arithmetic Operators + addition -substraction *Multiple /Division %Remainder

34 Mixed Operand n If all operand are integer type, then the result is an integer n If any operand is a floating-point or double precision type, then the result ia a double precision n Examples printf(%.2f”, 10.0/5);float/intfloating point2.00 printf(%.2f”, 10/5.0);int/floatfloating point2.00 printf(%.2f”, 10.0-5);float-intfloating point5.00

35 Assignment Operator Lvalue = Rvalue Variable Constant Expression Variable int x, y, z; char ch; float fnum x = 10; ch = ‘C’; fnum = 100.55; y = x*2 z = y;

36 Thanks…will continue on next lecture


Download ppt "Sales person receive RM200/week plus 9% of their gross sales for that week. Write an algorithms to calculate the sales person’s earning from the input."

Similar presentations


Ads by Google