Title: Sales person receive RM200/week plus 9% of their gross sales for that week. Write an algorithms to calculate the sales person
1Sales person receive RM200/week plus 9 of their
gross sales for that week. Write an algorithms
to calculate the sales persons earning from the
input of gross salary
- Input the first salespersons sales in dollars
- While the input is not -1
- Calculate the salespersons wages for the week
- Print the salespersons wages for the week
- Input the next salespersons sales in dollars
- end while
2- Input the first salespersons sales in dollars
- While the input is not -1
- //Calculate the salespersons wages for the week
- Wages RM200 (Sales 9)
- Print the salespersons wages for the week
- Input the next salespersons sales in dollars
- end while
3Write 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 numberNUMBER -
current input numberLARGEST - 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
4Annual Interest principal rate days
/365Develop 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
5The 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 n0.
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 gt0
- if number 0 then
- factorial 1
- else
- factorial factorial n
- end if
- nn-1
- end while
What wrong with this pseudocode ?
6Introduction to C
7C language character set tokens
- Reserved words
- Identifiers
- Constants
- String constants
- Punctuators
- Operators
8- 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- 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
- can consist of the capital letters A and Z, the
lowercase letters a to z, the digits 0 to 9, and
the underscore character - first character must be a letter or an underscore
- virtually no length limitation, compiler only
recognize first 32 character as significant - reserved word cannot be used
- case sensitive
10Constants
- Four types of constants integer, floating
point, character, and enumeration - 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
ltstdio.hgt int main() printf(This is my first
C program.\n) return 0 Output This is my
first C program.
12Parts of A C Program
- Comments Lines
- /../
- is used to comment your program while writing it
- Library File
- include ltstdio.hgt
- indicates a preprocessor command
- file with extension .h
- stdio.h is a library file that contains standard
I/O functions
13- Main() Function
- indicates the logical beginning of the program
- consist of function header and function body
- Function Header int main()
- int indicates the function returns an integer
value. - Function Body
-
- printf(This is my first c program.\n)
- return 0
14- Statement format,
- every statement will end with
- output function, printf()
- Escape character, \n
- \b, back space
- \r, carriage return
- \\, backslash
- \, double quotation
- \t, tab
15Examples
- Printf(\\ Hello \\ \t Good bye \ \n )
- OUTPUT
- \ Hello \ Good bye
16Data Types in C
- Basic Data Type
- character char
- integer number int
- floating-point number float
- double precision number double
- typeless/valueless void
17Class of Data
- Character constants
- string constants
- Integer constants
- floating-point constants
18Displaying Numbers
- Printf(control string, arg1, arg2,)
- example
- printf(A Simple Control String)
- Output A Simple Control String
- printf(The sum of 1 and 2 is d), 12)
19Conversion 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
20Conversion Specificatiom
- o octal printf(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.
21Conversion 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
22DISPLAYING CHARACTERS
- The character representation
-
- Storage 0 1 0 0 0 0 0 1
- c A
- d 65
23Displaying Formatted Strings
- printf(-20s,Pure C) Pure C
- 20.4s Pure
- -20.4 Pure
- .4s Pure
24- include ltstdio.hgt
- int main()
-
- 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(5s \n, Universiti Malaysia
Sarawak) - return 0
-
- Universiti Malaysia Sarawak
- Universiti Malaysia Sarawak
- Univer
- Univer
- Universiti Malaysia Sarawak
25Variables
- 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
26Common Errors
- Forgetting to declare variables before using them
- placing variable declaration in the middle of the
program - forgetting the data type or declaring a wrong
data type - forgetting the at the end of the statement
27Variable initializations
- 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
28Variable Names/identifier
- 1st character must be a letter or an underscore,
rest can be combination of letters, underscores,
or digit. - Case sensitive
- number of character is compiler-dependent
- whitespace character and special character
(!,,()) is not allowed within a name - no keyword and reserved word
29Data Type Modifier
- Integer modifiers - usually is 2 bytes
- Data Type Max Min
- short/short int 127 -128
- int 32767 -32768
- long/long int 2147483647 -2147483648
- unsigned short 255 0
- unsigned or unsigned int 65535 0
- unsigned long or unsigned
- long int 4294967295 0
30Data Type Modifier
- Floating-Point modifiers - usually is 8 bytes
- only one modifier, long double
- long double long_double / declare a long double
type variable /
31Declaration Initialization Statement
- long int LongNumber
- long Lnum 100L / 100 is fit in short but with
this declaration - will forces to
store in 4 bytes / - long A_Long 5l
- unsigned positive35353U
- unsigned long count1
- short counter
- long double Dlarge
- long double LD1.1e3000
32What You Have Learn ?
- / / comment line
- include directive preprocessor command
- int main ()
-
- declaration statements
- initialization statements
- statements
- return 0
-
33Operators
- A symbol performs a specific mathematical or
logical operation. - Arithmetic Operators
- addition
- - substraction
- Multiple
- / Division
- Remainder
34Mixed Operand
- If all operand are integer type, then the result
is an integer - If any operand is a floating-point or double
precision type, then the result ia a double
precision - Examples
- printf(.2f, 10.0/5) float/int floating
point 2.00 - printf(.2f, 10/5.0) int/float floating
point 2.00 - printf(.2f, 10.0-5) float-int floating
point 5.00
35Assignment Operator
int x, y, z char ch float fnum x 10 ch
C fnum 100.55 y x2 z y
- Variable
- Constant
- Expression
36Thankswill continue on next lecture