Download presentation
Presentation is loading. Please wait.
Published byOsborn O’Neal’ Modified over 9 years ago
2
While Loop Structure while (condition) { …. // This line will process when while condition is true }
3
do - While Loop Structure do { …. // This line will process first then check while condition } while (condition); // if while condition is true then process inside do again
4
For Loop Structure for (initial; condition; increment) { …. // This line will process when for condition is true } initial = initialize value of index (do at once) Condition = check whether true or false Increment = after process then define the increment or decrement of index
5
Nested For Loop Structure for (initial; condition; increment) { …. (outer loop line) for (initial; condition; increment) { …. (inner loop line) } ….. (outer loop line) }
6
How to use “FOR” instead of “WHILE” in the following example main() { int i; i =1; while(i <= 10) { printf(“%d”,i); i = i+1; }
7
How to use “FOR” instead of “WHILE” in the following example main() { int i; i =1; while(i <= 10) { printf(“%d”,i); i =i+1; } main() { int i; for (i=1;i <= 10;i=i+1) { printf(“%d”,i); }
8
A1 : Given The Following Code
9
i = 0 ; max = 6 ; Solution: Loop1 : i=0, max=6 0 Loop2 : i=3, max=6 3 Loop3 : i=6, max=6
10
A1 : Given The Following Code i = 0 ; max = 6 ; Result: 03
11
A1 : Given The Following Code i = -3 ; max = 6 ; Solution: Loop1 : i=-3, max=6 -3 Loop2 : i=0, max=6 0 Loop3 : i=3, max=6 3 Loop4 : i=6, max=6
12
A1 : Given The Following Code i = -3 ; max = 6 ; Result: -303
13
A1 : Given The Following Code i = 2 ; max = ++i +i +3 ; Solution: Loop1 : i=3, max=9 3 Loop2 : i=6, max=9 6 Loop3 : i=9, max=9 ++i + i +3 = 3+3+3 9
14
A1 : Given The Following Code i = 2 ; max = ++i +i +3 ; Result: 36
15
A1 : Given The Following Code max = 8 ; i = max/2; Solution: Loop1 : i=4, max=8 4 Loop2 : i=7, max=8 7 Loop3 : i=10, max=8
16
A1 : Given The Following Code max = 8 ; i = max/2; Result: 47
17
A2 : What is the output of the following program?
18
Result: x is 3(n=7) Solution: Loop1 : x=15, n=4 13 Loop2 : x=13, n=4 11 Loop3 : x=11, n=4 9 Loop4 : x=9, n=4 7 Loop5 : x=7, n=4 5 Loop6 : x=5, n=4 3 Loop7 : x=3, n=4
19
B9 : What is the output of the following program?
21
#include int main(void) { int x; for(x=3;x<20;x++) { if(x==5) continue; if(x==10) break; printf("%d ",x); } return 0; } Result: b. 3 4 6 7 8 9 Output Loop1(x=3) 3 Loop2(x=4) 4 Loop3(x=5) continue; Loop4(x=6) 6 Loop5(x=7) 7 Loop6(x=8) 8 Loop7(x=9) 9 Loop3(x=10) Break;
22
B11 : In the following C program, how many times will i be printed out on the screen?
23
B11 : In the following C program, how many times will i be printed out on the screen? #include int main(void) { int i=10; do{ printf("i"); i++; }while(i<10); } Result: d. 1 Output Loop1(i=10) i Loop2(i=11)
25
Array Structure int digits[10] = {1,2,3,4,5,6,7,8,9,10}; static float x[6] = { 0,0.25,0,0.50,0,0}; char color[] = {'R','E','D'}; char color2[] = "RED" digits[0] = 1 x[0] = 0 color[0] = 'R‘ digits[1] = 2 x[1] = 0.25 color[1] = 'E' digits[2] = 3 x[2] = 0 color[2] = 'D' digits[3] = 4 x[3] = 0.50 digits[4] = 5 x[4] = 0 color2[0] = 'R' digits[5] = 6 x[5] = 0 color2[1] = 'E' digits[6] = 7 color2[2] = 'D' digits[7] = 8 color2[3] = '\0' digits[8] = 9 digits[9] = 10
27
Link to Bubble Sort Example
28
B5:
29
Solution: a.int a[3]={2,3,4}; b.int a[] = {2,3,4}; c.int a[5] = {2,3,4}; d.int a[3] = {2,3,4,0,0}; 234 23400 234 23400 234
30
B6: 5 Rows 4 Columns
31
B6: Solution: a.int array[10][4]; array with 10 rows and 4 columns b.int array[4][10]; array with 4 rows and 10 columns c.int array[4,10]; ERROR d.int a {10} {4}; ERROR
32
B18:
33
B10:
36
String is defined as an ARRAY of CHARACTERS String is terminated by a special character which is null parameter (\0). We declare a string by char color[] = “blue”; char color[] = {‘b’, ‘l’, ‘u’, ‘e’,‘\0’}; char msg[10] = “Computer”; char msg[10] = {‘C’,’o’,’m’,’p’,’u’,’t’,’e’,’r’,’\0’};
37
String Input Functions scanf (“%s”,name); Scan until first space gets(name); Scan until end of line sscanf(data,”%s”,name); Read from data and keep string in name EXAMPLE: This is a Book scanf : This gets: This is a Book sscanf : This
38
String Output Functions #include main() { int i; char name[]= “Smith”; printf(“Name = %s”, name); puts(name); for(i=0;i<5;i++) { printf(“%c”, name[i]); }
39
Link to Example of ctype.h
40
string.h strcat(); Appends the string pointed to by str2 to the end of the string pointed to by str1. strncat(); Appends the string pointed to by str2 to the end of the string pointed to by str1 up to n characters long. strchr(); Searches for the first occurrence of the character c (an unsigned char) in the string pointed to by the argument str. strcmp(); Compares the string pointed to by str1 to the string pointed to by str2. strncmp(); Compares at most the first n bytes of str1 and str2. Stops comparing after the null character. strcpy(); Copies the string pointed to by str2 to str1. strncpy(); Copies up to n characters from the string pointed to by str2 to str1.
41
string.h strcspn(); Finds the first sequence of characters in the string str1 that does not contain any character specified in str2. strlen(); Computes the length of the string str up to but not including the terminating null character. strpbrk(); Finds the first character in the string str1 that matches any character specified in str2. strrchr(); Searches for the last occurrence of the character c (an unsigned char) in the string pointed to by the argument str. strspn(); Finds the first sequence of characters in the string str1 that contains any character specified in str2. strstr(); Finds the first occurrence of the entire string str2 (not including the terminating null character) which appears in the string str1. strtok(); Breaks string str1 into a series of tokens. strxfrm(); Transforms the string str2 and places the result into str1.
43
B13:
45
B21:
46
Solution printf("%d %c %c\n", toupper('a')+1, toupper('b')+1, tolower('C')-2); toupper('a')+1 A (65) +1 = 66 %d 66 toupper('b')+1 B(66) +1 = 67 %c C tolower('C')-2); c(99) – 2 = 97 %c a
47
B14:
49
B22:
50
Solution a. string.h This header file defines several functions to manipulate C strings and arrays b. stdio.h This header file for the standard functions that deal with input and output c. stdlib.h This header defines several general purpose functions, including dynamic memory management and random d. ctype.h This header allow the programmer to check, compare, and manipulate characters
51
A5:
52
Write a single statement to accomplish each of the following. Assume that variables c (which stores a character) is of type int, and arrays s1[100] and s2[100] are of type char. 5.1 Convert the character stored in variable c to an uppercase letter. Assign the result to variable c.
53
A5: Write a single statement to accomplish each of the following. Assume that variables c (which stores a character) is of type int, and arrays s1[100] and s2[100] are of type char. 5.1 Convert the character stored in variable c to an uppercase letter. Assign the result to variable c. #include main() { int c = 'a'; c = toupper(c); printf("%c",c); } Answer
54
A5: Write a single statement to accomplish each of the following. Assume that variables c (which stores a character) is of type int, and arrays s1[100] and s2[100] are of type char. 5.2 Convert the string “8.635” to double and print the value.
55
A5: Write a single statement to accomplish each of the following. Assume that variables c (which stores a character) is of type int, and arrays s1[100] and s2[100] are of type char. 5.2 Convert the string “8.635” to double and print the value. #include main() { char s2[100] = "8.635"; printf("%.3f",atof(s2)); } Answer
56
A5: Write a single statement to accomplish each of the following. Assume that variables c (which stores a character) is of type int, and arrays s1[100] and s2[100] are of type char. 5.3 Copy the string stored in array s2 into array s1.
57
A5: Write a single statement to accomplish each of the following. Assume that variables c (which stores a character) is of type int, and arrays s1[100] and s2[100] are of type char. 5.3 Copy the string stored in array s2 into array s1. #include main() { char s1[100], s2[100] = "8.635"; strcpy (s1,s2); printf("%s",s1); } Answer
58
A5: Write a single statement to accomplish each of the following. Assume that variables c (which stores a character) is of type int, and arrays s1[100] and s2[100] are of type char. 5.4 Determine the length of the string in s1. Print the result.
59
A5: Write a single statement to accomplish each of the following. Assume that variables c (which stores a character) is of type int, and arrays s1[100] and s2[100] are of type char. 5.4 Determine the length of the string in s1. Print the result. #include main() { char s1[100] = "8.635"; printf("%d",strlen(s1)); } Answer
61
Why? Main reason – Often be used more than once in a program – Fit naturally with a top-down design approach – Provide a natural method for dividing a programming task among team – Can be tested individually
62
Example: No Output void print_hi() { printf(“Hi”); } No Output
63
Example: No Input void print_hi() { printf(“Hi”); } void print_hi(void) { printf(“Hi”); } No Input
64
Example: Input as Integer int factorial(int n) { int ans=1; int i; for (i=2;i<n;i++) { ans*=i; } return ans; } n is input
65
Example: Output as Integer int factorial(int n) { int ans=1; int i; for (i=2;i<n;i++) { ans*=i; } return ans; } Output as Integer Output value of ans
66
Example: Two Input void print_chars(char ch, int n) { int i; for (i=0;i<n;i++) { printf(“%c”,ch); }
67
Function Prototypes double cal_area(double r); void main() { … area= cal_area(r); … } double cal_area(double r) { return 3.14*r*r; } double cal_area(double r) { return 3.14*r*r; } void main() { … area= cal_area(r); … }
68
Global vs Local Variable int pi=3.14; double cal_area(double r) { return pi*r*r; } void main() { … area= cal_area(r); cir= 2*pi*r; } double cal_area(double r) { int pi=3.14; return pi*r*r; } void main() {… int pi=3.14; area= cal_area(r); cir = 2*pi*r; }
69
Extract function from code void main() { double r,area; printf(“input radius>”); scanf(“%lf”,&r); area= 3.14*r*r; printf(“area is %lf”,area); } r is an input Type of r is double Need to output as double Link to Example of Function
70
B4:
71
Global Variables Local Variables
72
B4: Solution: a.local variable; A variable defined inside a function b.general variable A variable as input to the function c.prototype variable A function prototype is a function header without implementation. d. global variable A variable defined outside a function
73
B7:
79
What is the output? #include int x,y,z; void junk(int x,int y,int z) { x++; y++; z++; printf(“%10d%10d%10d\n”,x,y,z); } main() { x=1; y=2; z=3; printf(“%10d%10d%10d\n”,x,y,z); junk(x,y,z); printf(“%10d%10d%10d\n”,x,y,z); }
80
What is the output? #include int x,y,z; void junk(int x,int y,int z) { x++; y++; z++; printf(“%10d%10d%10d\n”,x,y,z); } main() { x=1; y=2; z=3; printf(“%10d%10d%10d\n”,x,y,z); junk(x,y,z); printf(“%10d%10d%10d\n”,x,y,z); } 1 2 3 2 3 4 1 2 3
82
What is the output? #include int x,y,z; void junk(int *x,int *y,int *z) { (*x)++; (*y)++; (*z)++; printf(“%10d%10d%10d\n”,*x,*y,*z); } main() { x=1; y=2; z=3; printf(“%10d%10d%10d\n”,x,y,z); junk(&x,&y,&z); printf(“%10d%10d%10d\n”,x,y,z); }
83
What is the output? #include int x,y,z; void junk(int *x,int *y,int *z) { (*x)++; (*y)++; (*z)++; printf(“%10d%10d%10d\n”,*x,*y,*z); } main() { x=1; y=2; z=3; printf(“%10d%10d%10d\n”,x,y,z); junk(&x,&y,&z); printf(“%10d%10d%10d\n”,x,y,z); } 1 2 3 2 3 4
84
Call by Reference We can solved problem of multiple output from function using Call by Reference Link to Example of Call by Reference
85
B1:
86
Solution: a.int x; creates a variable x as integer type b.ptr x; Not found this type of declaration c.int &x; creates a reference to an int named 'x' d.int *x; x is a pointer to an integer
87
B2:
88
Solution: a.*a; value of memory address of variable named 'a' b.a; variable named 'a c.&a; memory address of variable named 'a' d.address(a); function named 'address'
89
B15:
91
B16:
92
Solution: a.char * Example: “123”, “123abc”, ”abc” b.float *; Example: 1.23, 1.0, 100 c.int Example: 1, 2, 3, 10, 100, 1000 d.char Example: ‘a’, ‘b’, ‘1’, ‘2’, ‘3’
93
B17:
94
Solution: a.char *s=“Test” b.char s[5]={'t','e','s','t',0}; c.char s[]=“Hello” d.char s[5]=“Hello” TEST\0 TEST Hello Hello!!@
95
B20:
96
2 p 3451 a. p=a+3; 2 p 3451 c. p++; 23451 d. *q=(*p); P q
97
B3:
98
Solution: a.*p; value of the variable pointed to by pointer p b.p; memory address of the variable pointed to by pointer p c.&p; memory address of pointer p d.address(p); function named 'address'
99
B12:
101
A4 : What is the output of the following program? Link to Solution
102
A4 : What is the output of the following program? Result: x = 32 y = 20
104
Structure Structures are the C equivalent of records. A structure type can define in 2 ways, which are struct Foo { int x; int array[100]; }; Usage: struct Foo f; f.x = 54; f.array[3]=9; typedef struct { int x; int array[100]; } Foo; Usage: Foo f; f.x = 54; f.array[3]=9; Link to Example of Structure
105
Structure Passing Structs as Parameters Structs are passed by value, just like primitives void func(struct Foo foo){ foo.x = 56; foo.array[3]=55; } void main(){ struct Foo f; f.x = 54; f.array[3]=9; func(f); printf("%d and %d",f.x,f.array[3]); }
106
Structure Passing Structs as Parameters To have changes occur, send a pointer to the struct void func(struct Foo* foo){ (*foo).x = 56; (*foo).array[3]=55; } void main(){ struct Foo f; f.x = 54; f.array[3]=9; func(&f); printf("%d and %d",f.x,f.array[3]); }
107
What will be the output of the following code and why? #include typedef struct { char *a, *b, *c; } colors; void funct(colors sample){ sample.a = “Cyan”; sample.b = “Brown”; sample.c = “Yellow”; printf(“%s %s %s\n”, sample.a,sample.b,sample.c); } main() { colors sample = {“red”, “green”, “blue”}; printf(“%s %s %s\n”, sample.a,sample.b,sample.c); funct(sample); printf(“%s %s %s\n”, sample.a,sample.b,sample.c); }
108
What will be the output of the following code and why? #include typedef struct { char *a, *b, *c; } colors; void funct(colors sample){ sample.a = “Cyan”; sample.b = “Brown”; sample.c = “Yellow”; printf(“%s %s %s\n”, sample.a,sample.b,sample.c); } main() { colors sample = {“red”, “green”, “blue”}; printf(“%s %s %s\n”, sample.a,sample.b,sample.c); funct(sample); printf(“%s %s %s\n”, sample.a,sample.b,sample.c); } red green blue Cyan Brown Yellow red green blue
109
If we want output as followed, how to change the program? #include typedef struct { char *a, *b, *c; } colors; void funct(colors sample){ sample.a = “Cyan”; sample.b = “Brown”; sample.c = “Yellow”; printf(“%s %s %s\n”, sample.a,sample.b,sample.c); } main() { colors sample = {“red”, “green”, “blue”}; printf(“%s %s %s\n”, sample.a,sample.b,sample.c); funct(sample); printf(“%s %s %s\n”, sample.a,sample.b,sample.c); } red green blue Cyan Brown Yellow
110
If we want output as followed, how to change the program? #include typedef struct { char *a, *b, *c; } colors; void funct(colors *sample){ sample.a = “Cyan”; sample.b = “Brown”; sample.c = “Yellow”; printf(“%s %s %s\n”, sample.a,sample.b,sample.c); } main() { colors sample = {“red”, “green”, “blue”}; printf(“%s %s %s\n”, sample.a,sample.b,sample.c); funct(sample); printf(“%s %s %s\n”, sample.a,sample.b,sample.c); } red green blue Cyan Brown Yellow
111
If we want output as followed, how to change the program? #include typedef struct { char *a, *b, *c; } colors; void funct(colors *sample){ sample.a = “Cyan”; sample.b = “Brown”; sample.c = “Yellow”; printf(“%s %s %s\n”, sample.a,sample.b,sample.c); } main() { colors sample = {“red”, “green”, “blue”}; printf(“%s %s %s\n”, sample.a,sample.b,sample.c); funct(&sample); printf(“%s %s %s\n”, sample.a,sample.b,sample.c); } red green blue Cyan Brown Yellow
112
If we want output as followed, how to change the program? #include typedef struct { char *a, *b, *c; } colors; void funct(colors *sample){ (*sample).a = “Cyan”; (*sample).b = “Brown”; (*sample).c = “Yellow”; printf(“%s %s %s\n”, sample.a,sample.b,sample.c); } main() { colors sample = {“red”, “green”, “blue”}; printf(“%s %s %s\n”, sample.a,sample.b,sample.c); funct(&sample); printf(“%s %s %s\n”, sample.a,sample.b,sample.c); } red green blue Cyan Brown Yellow
113
B27:
114
B8: C++ Spring 2000 Arrays typedef struct tnode { int a,b,c,d;} node; main() { node x; int *msg = &x;} 2 msg 345 x.a x.b x.c x.d
115
B8: C++ Spring 2000 Arrays typedef struct tnode { int a,b,c,d;} node; main() { node x; int *msg = &x;} 2 msg 345 x.a x.b x.c x.d 4 Bytes
116
Use the given part of the program to answer questions 28-30
117
B28:
119
B29: 4 Byte 40 Byte 118 Byte 4 Byte
120
B29: 4 Byte 40 Byte 118 Byte 4 Byte
121
B30:
122
Wrong type argument NULL undeclared
123
A3 : What is the output of the following program? Link to Solution
124
A3 : What is the output of the following program? Result: 8 6 6
129
B19: Solution: a.fseek sets the file position indicator for the stream pointed to by stream. b.fscanf read data from file into specified variable. c.rewind Moving the file position marker back to the beginning of the file. d.feof Check whether the end-of-file marker is reached.
130
B19: Solution: a.fseek sets the file position indicator for the stream pointed to by stream. b.fscanf read data from file into specified variable. c.rewind Moving the file position marker back to the beginning of the file. d.feof Check whether the end-of-file marker is reached.
131
B23:
134
B24:
136
B25:
138
B26:
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.