While Loop Structure while (condition) { …. // This line will process when while condition is true }
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
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
Nested For Loop Structure for (initial; condition; increment) { …. (outer loop line) for (initial; condition; increment) { …. (inner loop line) } ….. (outer loop line) }
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; }
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); }
A1 : Given The Following Code
i = 0 ; max = 6 ; Solution: Loop1 : i=0, max=6 0 Loop2 : i=3, max=6 3 Loop3 : i=6, max=6
A1 : Given The Following Code i = 0 ; max = 6 ; Result: 03
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
A1 : Given The Following Code i = -3 ; max = 6 ; Result: -303
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 = 9
A1 : Given The Following Code i = 2 ; max = ++i +i +3 ; Result: 36
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
A1 : Given The Following Code max = 8 ; i = max/2; Result: 47
A2 : What is the output of the following program?
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
B9 : What is the output of the following program?
#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 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;
B11 : In the following C program, how many times will i be printed out on the screen?
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)
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
Link to Bubble Sort Example
B5:
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};
B6: 5 Rows 4 Columns
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
B18:
B10:
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’};
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
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]); }
Link to Example of ctype.h
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.
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.
B13:
B21:
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
B14:
B22:
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
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.
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
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.
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
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.
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
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.
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
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
Example: No Output void print_hi() { printf(“Hi”); } No Output
Example: No Input void print_hi() { printf(“Hi”); } void print_hi(void) { printf(“Hi”); } No Input
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
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
Example: Two Input void print_chars(char ch, int n) { int i; for (i=0;i<n;i++) { printf(“%c”,ch); }
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); … }
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; }
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
B4:
Global Variables Local Variables
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
B7:
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); }
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); }
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); }
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); }
Call by Reference We can solved problem of multiple output from function using Call by Reference Link to Example of Call by Reference
B1:
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
B2:
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'
B15:
B16:
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’
B17:
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
B20:
2 p 3451 a. p=a+3; 2 p 3451 c. p++; d. *q=(*p); P q
B3:
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'
B12:
A4 : What is the output of the following program? Link to Solution
A4 : What is the output of the following program? Result: x = 32 y = 20
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
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]); }
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]); }
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); }
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
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
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
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
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
B27:
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
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
Use the given part of the program to answer questions 28-30
B28:
B29: 4 Byte 40 Byte 118 Byte 4 Byte
B29: 4 Byte 40 Byte 118 Byte 4 Byte
B30:
Wrong type argument NULL undeclared
A3 : What is the output of the following program? Link to Solution
A3 : What is the output of the following program? Result: 8 6 6
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.
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.
B23:
B24:
B25:
B26: