Top Banner
139

While Loop Structure while (condition) { …. // This line will process when while condition is true }

Dec 28, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: While Loop Structure while (condition) { …. // This line will process when while condition is true }
Page 2: While Loop Structure while (condition) { …. // This line will process when while condition is true }

While Loop

Structure

while (condition){

…. // This line will process when while condition is true

}

Page 3: 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

Page 4: While Loop Structure while (condition) { …. // This line will process when while condition is true }

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 falseIncrement = after process then define the increment or decrement of index

Page 5: While Loop Structure while (condition) { …. // This line will process when while condition is true }

Nested For Loop

Structure

for (initial; condition; increment){

…. (outer loop line)for (initial; condition; increment){

…. (inner loop line)}….. (outer loop line)

}

Page 6: While Loop Structure while (condition) { …. // This line will process when while condition is true }

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;

}}

Page 7: While Loop Structure while (condition) { …. // This line will process when while condition is true }

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); }

}

Page 8: While Loop Structure while (condition) { …. // This line will process when while condition is true }

A1 : Given The Following Code

Page 9: While Loop Structure while (condition) { …. // This line will process when while condition is true }

A1 : Given The Following Code

i = 0 ; max = 6 ;

Solution:Loop1 : i=0, max=6 0Loop2 : i=3, max=6 3Loop3 : i=6, max=6

Page 10: While Loop Structure while (condition) { …. // This line will process when while condition is true }

A1 : Given The Following Code

i = 0 ; max = 6 ;

Result: 03

Page 11: While Loop Structure while (condition) { …. // This line will process when while condition is true }

A1 : Given The Following Code

i = -3 ; max = 6 ;

Solution:Loop1 : i=-3, max=6 -3Loop2 : i=0, max=6 0Loop3 : i=3, max=6 3Loop4 : i=6, max=6

Page 12: While Loop Structure while (condition) { …. // This line will process when while condition is true }

A1 : Given The Following Code

i = -3 ; max = 6 ;

Result: -303

Page 13: While Loop Structure while (condition) { …. // This line will process when while condition is true }

A1 : Given The Following Code

i = 2 ; max = ++i +i +3 ;

Solution:Loop1 : i=3, max=9 3Loop2 : i=6, max=9 6Loop3 : i=9, max=9

++i + i +3 = 3+3+3 9

Page 14: While Loop Structure while (condition) { …. // This line will process when while condition is true }

A1 : Given The Following Code

i = 2 ; max = ++i +i +3 ;

Result: 36

Page 15: While Loop Structure while (condition) { …. // This line will process when while condition is true }

A1 : Given The Following Code

max = 8 ; i = max/2;

Solution:Loop1 : i=4, max=8 4Loop2 : i=7, max=8 7Loop3 : i=10, max=8

Page 16: While Loop Structure while (condition) { …. // This line will process when while condition is true }

A1 : Given The Following Code

max = 8 ; i = max/2;

Result: 47

Page 17: While Loop Structure while (condition) { …. // This line will process when while condition is true }

A2 : What is the output of the following program?

Page 18: While Loop Structure while (condition) { …. // This line will process when while condition is true }

A2 : What is the output of the following program?

Result: x is 3 (n=7)

Solution:Loop1 : x=15, n=4 13Loop2 : x=13, n=4 11Loop3 : x=11, n=4 9Loop4 : x=9, n=4 7Loop5 : x=7, n=4 5Loop6 : x=5, n=4 3Loop7 : x=3, n=4

Page 19: While Loop Structure while (condition) { …. // This line will process when while condition is true }

B9 : What is the output of the following program?

Page 20: While Loop Structure while (condition) { …. // This line will process when while condition is true }
Page 21: While Loop Structure while (condition) { …. // This line will process when while condition is true }

B9 : What is the output of the following program?#include<stdio.h> 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;

Page 22: While Loop Structure while (condition) { …. // This line will process when while condition is true }

B11 : In the following C program, how many times will i be printed out on the screen?

Page 23: While Loop Structure while (condition) { …. // This line will process when while condition is true }

B11 : In the following C program, how many times will i be printed out on the screen?

#include<stdio.h> int main(void) { int i=10; do{ printf("i"); i++; }while(i<10); }

Result: d. 1

Output Loop1(i=10) i Loop2(i=11)

Page 24: While Loop Structure while (condition) { …. // This line will process when while condition is true }
Page 25: While Loop Structure while (condition) { …. // This line will process when while condition is true }

ArrayStructure

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

Page 26: While Loop Structure while (condition) { …. // This line will process when while condition is true }
Page 27: While Loop Structure while (condition) { …. // This line will process when while condition is true }

Link to Bubble Sort Example

Page 28: While Loop Structure while (condition) { …. // This line will process when while condition is true }

B5:

Page 29: While Loop Structure while (condition) { …. // This line will process when while condition is true }

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};

2 3 4

2 3 4 0 0

2 3 4

2 3 4 0 0

2 3 4

Page 30: While Loop Structure while (condition) { …. // This line will process when while condition is true }

B6:

5 Rows

4 Columns

Page 31: While Loop Structure while (condition) { …. // This line will process when while condition is true }

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

Page 32: While Loop Structure while (condition) { …. // This line will process when while condition is true }

B18:

Page 33: While Loop Structure while (condition) { …. // This line will process when while condition is true }

B10:

Page 34: While Loop Structure while (condition) { …. // This line will process when while condition is true }

B10:

Page 35: While Loop Structure while (condition) { …. // This line will process when while condition is true }
Page 36: While Loop Structure while (condition) { …. // This line will process when while condition is true }

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’};

Page 37: While Loop Structure while (condition) { …. // This line will process when while condition is true }

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 Bookscanf : Thisgets: This is a Booksscanf : This

Page 38: While Loop Structure while (condition) { …. // This line will process when while condition is true }

String Output Functions #include <stdio.h>main(){

int i;char name[]= “Smith”;printf(“Name = %s”, name);puts(name);for(i=0;i<5;i++){

printf(“%c”, name[i]);}

}

Page 39: While Loop Structure while (condition) { …. // This line will process when while condition is true }

Link to Example of ctype.h

Page 40: While Loop Structure while (condition) { …. // This line will process when while condition is true }

string.hstrcat(); 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.

Page 41: While Loop Structure while (condition) { …. // This line will process when while condition is true }

string.hstrcspn(); 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.

Page 42: While Loop Structure while (condition) { …. // This line will process when while condition is true }
Page 43: While Loop Structure while (condition) { …. // This line will process when while condition is true }

B13:

Page 44: While Loop Structure while (condition) { …. // This line will process when while condition is true }

B13:

Page 45: While Loop Structure while (condition) { …. // This line will process when while condition is true }

B21:

Page 46: While Loop Structure while (condition) { …. // This line will process when while condition is true }

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

Page 47: While Loop Structure while (condition) { …. // This line will process when while condition is true }

B14:

Page 48: While Loop Structure while (condition) { …. // This line will process when while condition is true }

B14:

Page 49: While Loop Structure while (condition) { …. // This line will process when while condition is true }

B22:

Page 50: While Loop Structure while (condition) { …. // This line will process when while condition is true }

B22:

Solution

a. string.h This header file defines several functions to manipulate C strings and arraysb. stdio.h This header file for the standard functions that deal with input and outputc. stdlib.h This header defines several general purpose functions, including dynamic memory management and randomd. ctype.h This header allow the programmer to check, compare, and manipulate characters

Page 51: While Loop Structure while (condition) { …. // This line will process when while condition is true }

A5:

Page 52: While Loop Structure while (condition) { …. // This line will process when while condition is true }

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.

Page 53: While Loop Structure while (condition) { …. // This line will process when while condition is true }

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 <ctype.h>main() { int c = 'a'; c = toupper(c); printf("%c",c);}

Answer

Page 54: While Loop Structure while (condition) { …. // This line will process when while condition is true }

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.

Page 55: While Loop Structure while (condition) { …. // This line will process when while condition is true }

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 <stdlib.h>main() { char s2[100] = "8.635"; printf("%.3f",atof(s2));

}

Answer

Page 56: While Loop Structure while (condition) { …. // This line will process when while condition is true }

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.

Page 57: While Loop Structure while (condition) { …. // This line will process when while condition is true }

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 <string.h>main() { char s1[100], s2[100] = "8.635"; strcpy (s1,s2);

printf("%s",s1);}

Answer

Page 58: While Loop Structure while (condition) { …. // This line will process when while condition is true }

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.

Page 59: While Loop Structure while (condition) { …. // This line will process when while condition is true }

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 <string.h>main() { char s1[100] = "8.635"; printf("%d",strlen(s1));

}

Answer

Page 60: While Loop Structure while (condition) { …. // This line will process when while condition is true }
Page 61: While Loop Structure while (condition) { …. // This line will process when while condition is true }

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

Page 62: While Loop Structure while (condition) { …. // This line will process when while condition is true }

Example: No Output

void print_hi(){

printf(“Hi”);

}

No Output

Page 63: While Loop Structure while (condition) { …. // This line will process when while condition is true }

Example: No Input

void print_hi(){

printf(“Hi”);

}

void print_hi(void){

printf(“Hi”);

}

No Input

Page 64: While Loop Structure while (condition) { …. // This line will process when while condition is true }

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

Page 65: While Loop Structure while (condition) { …. // This line will process when while condition is true }

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

Page 66: While Loop Structure while (condition) { …. // This line will process when while condition is true }

Example: Two Input

void print_chars(char ch, int n){

int i;for (i=0;i<n;i++){

printf(“%c”,ch);}

}

Page 67: While Loop Structure while (condition) { …. // This line will process when while condition is true }

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);…

}

Page 68: While Loop Structure while (condition) { …. // This line will process when while condition is true }

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;

}

Page 69: While Loop Structure while (condition) { …. // This line will process when while condition is true }

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 inputType of r is double

Need to output as double

Link to Example of Function

Page 70: While Loop Structure while (condition) { …. // This line will process when while condition is true }

B4:

Page 71: While Loop Structure while (condition) { …. // This line will process when while condition is true }

B4:

Global Variables

Local Variables

Page 72: While Loop Structure while (condition) { …. // This line will process when while condition is true }

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

Page 73: While Loop Structure while (condition) { …. // This line will process when while condition is true }

B7:

Page 74: While Loop Structure while (condition) { …. // This line will process when while condition is true }

B7:

Page 75: While Loop Structure while (condition) { …. // This line will process when while condition is true }
Page 76: While Loop Structure while (condition) { …. // This line will process when while condition is true }
Page 77: While Loop Structure while (condition) { …. // This line will process when while condition is true }
Page 78: While Loop Structure while (condition) { …. // This line will process when while condition is true }
Page 79: While Loop Structure while (condition) { …. // This line will process when while condition is true }

What is the output?#include <stdio.h>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);

}

Page 80: While Loop Structure while (condition) { …. // This line will process when while condition is true }

What is the output?#include <stdio.h>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 32 3 41 2 3

Page 81: While Loop Structure while (condition) { …. // This line will process when while condition is true }
Page 82: While Loop Structure while (condition) { …. // This line will process when while condition is true }

What is the output?#include <stdio.h>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);

}

Page 83: While Loop Structure while (condition) { …. // This line will process when while condition is true }

What is the output?#include <stdio.h>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 32 3 42 3 4

Page 84: While Loop Structure while (condition) { …. // This line will process when while condition is true }

Call by Reference

• We can solved problem of multiple output from function using Call by Reference

Link to Example of Call by Reference

Page 85: While Loop Structure while (condition) { …. // This line will process when while condition is true }

B1:

Page 86: While Loop Structure while (condition) { …. // This line will process when while condition is true }

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

Page 87: While Loop Structure while (condition) { …. // This line will process when while condition is true }

B2:

Page 88: While Loop Structure while (condition) { …. // This line will process when while condition is true }

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'

Page 89: While Loop Structure while (condition) { …. // This line will process when while condition is true }

B15:

Page 90: While Loop Structure while (condition) { …. // This line will process when while condition is true }

B15:

Page 91: While Loop Structure while (condition) { …. // This line will process when while condition is true }

B16:

Page 92: While Loop Structure while (condition) { …. // This line will process when while condition is true }

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’

Page 93: While Loop Structure while (condition) { …. // This line will process when while condition is true }

B17:

Page 94: While Loop Structure while (condition) { …. // This line will process when while condition is true }

B17:

Solution:

a. char *s=“Test”

b. char s[5]={'t','e','s','t',0};

c. char s[]=“Hello”

d. char s[5]=“Hello”

T E S T \0

T E S T \0

H e l l o \0

H e l l o !! @

Page 95: While Loop Structure while (condition) { …. // This line will process when while condition is true }

B20:

Page 96: While Loop Structure while (condition) { …. // This line will process when while condition is true }

B20:

2

p

3 4 51

a. p=a+3;

2

p

3 4 51

c. p++;

2 3 4 51d. *q=(*p);

P

q

Page 97: While Loop Structure while (condition) { …. // This line will process when while condition is true }

B3:

Page 98: While Loop Structure while (condition) { …. // This line will process when while condition is true }

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'

Page 99: While Loop Structure while (condition) { …. // This line will process when while condition is true }

B12:

Page 100: While Loop Structure while (condition) { …. // This line will process when while condition is true }

B12:

Page 101: While Loop Structure while (condition) { …. // This line will process when while condition is true }

A4 : What is the output of the following program?

Link to Solution

Page 102: While Loop Structure while (condition) { …. // This line will process when while condition is true }

A4 : What is the output of the following program?

Result: x = 32 y = 20

Page 103: While Loop Structure while (condition) { …. // This line will process when while condition is true }
Page 104: While Loop Structure while (condition) { …. // This line will process when while condition is true }

StructureStructures are the C equivalent of records. A

structure type can define in 2 ways, which arestruct 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

Page 105: While Loop Structure while (condition) { …. // This line will process when while condition is true }

StructurePassing Structs as ParametersStructs 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]); }

Page 106: While Loop Structure while (condition) { …. // This line will process when while condition is true }

StructurePassing Structs as ParametersTo 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]); }

Page 107: While Loop Structure while (condition) { …. // This line will process when while condition is true }

What will be the output of the following code and why?

#include <stdio.h>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);

}

Page 108: While Loop Structure while (condition) { …. // This line will process when while condition is true }

What will be the output of the following code and why?

#include <stdio.h>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 blueCyan Brown Yellowred green blue

Page 109: While Loop Structure while (condition) { …. // This line will process when while condition is true }

If we want output as followed, how to change the program?

#include <stdio.h>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 blueCyan Brown YellowCyan Brown Yellow

Page 110: While Loop Structure while (condition) { …. // This line will process when while condition is true }

If we want output as followed, how to change the program?

#include <stdio.h>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 blueCyan Brown YellowCyan Brown Yellow

Page 111: While Loop Structure while (condition) { …. // This line will process when while condition is true }

If we want output as followed, how to change the program?

#include <stdio.h>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 blueCyan Brown YellowCyan Brown Yellow

Page 112: While Loop Structure while (condition) { …. // This line will process when while condition is true }

If we want output as followed, how to change the program?

#include <stdio.h>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 blueCyan Brown YellowCyan Brown Yellow

Page 113: While Loop Structure while (condition) { …. // This line will process when while condition is true }

B27:

Page 114: While Loop Structure while (condition) { …. // This line will process when while condition is true }

B8:

C++ Spring 2000 Arrays

typedef struct tnode {int a,b,c,d;} node;main() {node x; int *msg = &x;}

2

msg

3 4 5x.a x.b x.c x.d

Page 115: While Loop Structure while (condition) { …. // This line will process when while condition is true }

B8:

C++ Spring 2000 Arrays

typedef struct tnode {int a,b,c,d;} node;main() {node x; int *msg = &x;}

2

msg

3 4 5x.a x.b x.c x.d

4 Bytes

Page 116: While Loop Structure while (condition) { …. // This line will process when while condition is true }

Use the given part of the program to answer questions 28-30

Page 117: While Loop Structure while (condition) { …. // This line will process when while condition is true }

B28:

Page 118: While Loop Structure while (condition) { …. // This line will process when while condition is true }

B28:

Page 119: While Loop Structure while (condition) { …. // This line will process when while condition is true }

B29:

4 Byte40 Byte118 Byte

4 Byte

Page 120: While Loop Structure while (condition) { …. // This line will process when while condition is true }

B29:

4 Byte40 Byte118 Byte

4 Byte

Page 121: While Loop Structure while (condition) { …. // This line will process when while condition is true }

B30:

Page 122: While Loop Structure while (condition) { …. // This line will process when while condition is true }

B30:

Wrong type argument

NULL undeclared

Page 123: While Loop Structure while (condition) { …. // This line will process when while condition is true }

A3 : What is the output of the following program?

Link to Solution

Page 124: While Loop Structure while (condition) { …. // This line will process when while condition is true }

A3 : What is the output of the following program?

Result: 8 6 6

Page 125: While Loop Structure while (condition) { …. // This line will process when while condition is true }
Page 126: While Loop Structure while (condition) { …. // This line will process when while condition is true }
Page 127: While Loop Structure while (condition) { …. // This line will process when while condition is true }
Page 128: While Loop Structure while (condition) { …. // This line will process when while condition is true }
Page 129: While Loop Structure while (condition) { …. // This line will process when while condition is true }

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.

Page 130: While Loop Structure while (condition) { …. // This line will process when while condition is true }

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.

Page 131: While Loop Structure while (condition) { …. // This line will process when while condition is true }

B23:

Page 132: While Loop Structure while (condition) { …. // This line will process when while condition is true }

B23:

Page 133: While Loop Structure while (condition) { …. // This line will process when while condition is true }

B23:

Page 134: While Loop Structure while (condition) { …. // This line will process when while condition is true }

B24:

Page 135: While Loop Structure while (condition) { …. // This line will process when while condition is true }

B24:

Page 136: While Loop Structure while (condition) { …. // This line will process when while condition is true }

B25:

Page 137: While Loop Structure while (condition) { …. // This line will process when while condition is true }

B25:

Page 138: While Loop Structure while (condition) { …. // This line will process when while condition is true }

B26:

Page 139: While Loop Structure while (condition) { …. // This line will process when while condition is true }

B26: