C workshop #3 flow control / strings 1 do / while / for #include <stdio.h> void main() { int I = 1, J; do { for ( J = 0 ; J < I ; J++ ) printf("*"); printf("\n"); I++; } while ( I < 5 ); I = 0; do { printf("%d, ",I); I++; } while ( I < 4 ); printf("\n"); Output: * ** *** **** 0, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, I = 0; while ( I++ < 4 ) printf("%d, ",I); printf("\n"); } I = 0; while ( ++I < 4 ) printf("%d, ",I); printf("\n"); 2 goto • Continue executing at different location #include <stdio.h> void main() { printf("1\n"); goto LABEL_QQ; printf("2\n"); Output: 1 3 LABEL_QQ: printf("3\n"); } • • • The label can be anything, e.g. Cont_here, Line123, AfterTest. Cannot jump into a block, or into a different function. NOT RECOMMENDED TO USE. Used mainly with error handling. 3 switch / case #include <stdio.h> void main() { int I; for ( I = 0 ; I < 10 ; I++ ) { switch (I) { Output: **How are you???** case 2: printf("How "); break; case 3: printf("are "); break; case 4: printf("you"); break; case 5: case 6: case 7: printf("?"); break; default: printf("*"); } } printf("\n"); } 4 continue • Used in ‘for / while / do’ loops #include <stdio.h> void main() { Output: 0* 1* 2* 3* 4 5* 6* 7 8* 9* int I; for ( I = 0 ; I < 10 ; I++ ) { printf(“ %d", I ); if ( I == 9 || I==3 ) continue; printf(“*"); } printf("\n"); } 5 break • Used in ‘for / while / do’ loops #include <stdio.h> void main() { Output: 0* 1* 2* 3* 4 int I; for ( I = 0 ; I < 10 ; I++ ) { printf(“ %d", I ); if ( I == 4 ) break; printf(“*"); } printf("\n"); } 6 characters • A single byte (a bytes defined as a number between 0 to 255), represents a single character Sample of character representations: 32= 33=! 34=" 35=# 36=$ 37=% 38=& 39=' 40=( 41=) 42=* 43=+ 44=, 45=- 46=. 47=/ 48=0 49=1 50=2 51=3 52=4 53=5 54=6 55=7 56=8 57=9 58=: 59=; 60=< 61== 62=> 63=? 64=@ 65=A 66=B 67=C 68=D 69=E 70=F 71=G 72=H 73=I 74=J 75=K 76=L 77=M 78=N 79=O 80=P 81=Q 82=R 83=S 84=T 85=U 86=V 87=W 88=X 89=Y 90=Z 91=[ 92=\ 93=] 94=^ 95=_ 96=` 97=a 98=b 99=c 100=d 101=e 102=f 103=g 104=h 105=i 106=j 107=k 108=l 109=m 110=n 111=o 112=p 113=q 114=r 115=s 116=t 117=u 118=v 119=w 120=x 121=y 122=z 123={ 124=| 125=} 126=~ 7 char sample #1 #include <stdio.h> void main() { char C, G, L; C = 't'; printf("%c\n", C ); C = 't' + 1; Output: t u uu Now, dear user, press any key, and then press ENTER 3 You entered: 3 printf("%c\n", C ); G = C; printf("%c %c\n", G, C ); printf("Now, dear user, press any key, and then press ENTER\n"); L = getchar(); printf("You entered: %c\n", L ); } 8 char sample #2 #include <stdio.h> void main() { char C; for ( C = 32 ; C < 127 ; C++ ) { printf("%3d=%c } printf("\n"); } ", C, C ); Output: 32= 33=! 34=" 35=# 36=$ 37=% 38=& 39=' 40=( 41=) 42=* 43=+ 44=, 45=- 46=. 47=/ 48=0 49=1 50=2 51=3 52=4 53=5 54=6 55=7 56=8 57=9 58=: 59=; 60=< 61== 62=> 63=? 64=@ 65=A 66=B 67=C 68=D 69=E 70=F 71=G 72=H 73=I 74=J 75=K 76=L 77=M 78=N 79=O 80=P 81=Q 82=R 83=S 84=T 85=U 86=V 87=W 88=X 89=Y 90=Z 91=[ 92=\ 93=] 94=^ 95=_ 96=` 97=a 98=b 99=c 100=d 101=e 102=f 103=g 104=h 105=i 106=j 107=k 108=l 109=m 110=n 111=o 112=p 113=q 114=r 115=s 116=t 117=u 118=v 119=w 120=x 121=y 122=z 123={ 124=| 125=} 126=~ 9 strings • string is an array of characters • To have meaningful data, the last character of the string has to be \0 (not to confuse with the character ‘0’). Output: #include <stdio.h> void main() { char S[100]; Hi! Hi! 72 105 33 0 S[0] = 'H'; S[1] = 'i'; S[2] = '!'; S[3] = '\0'; printf("%s\n", S ); printf("%c %c %c %ce\n", S[0], S[1], S[2], S[3] ); printf("%d %d %d %d\n", S[0], S[1], S[2], S[3] ); } 10 strings sample #2 #include <stdio.h> #include <string.h> void main() { char ST[100]; char ST2[] = "Good Morning!"; char C; strcpy( ST, ST2 ); printf("%s\n", ST ); Output: Good Morning! Test0123 s strcpy( ST, "Test0123" ); printf("%s\n", ST ); C = ST[2]; printf("%c\n", C ); } 11 strings sample #3 #include <stdio.h> #include <string.h> void main() { char ST[100]; int I = 33; strcpy( ST, "%d\n" ); printf( ST, I ); } Output: 33 12 strings sample #4 (strcat, strlen, strstr) #include <stdio.h> #include <string.h> void main() { char ST[100]; char *P; int L; strcpy( ST, "Beginning " ); strcat( ST, "End"); L = strlen( ST ); P = strstr( ST, "nni" ); if ( P != NULL ) *P = 'N'; printf( "%s, %d\n", ST, L ); } Output: BegiNning End, 13 13 strings sample #5 #include <stdio.h> #include <string.h> void ToCap( char *ST ) { while ( *ST != 0 ) { if ( *ST >= 'a' && *ST <='z' ) *ST = (*ST - 'a') + 'A'; ST++; Output: } } Good Morning 123!! GOOD MORNING 123!! void main() { char ST[100]; strcpy( ST, "Good Morning 123!!\n" ); printf( ST ); ToCap( ST ); printf( ST ); } 14 strings sample #5.5 (SAME) #include <stdio.h> #include <string.h> void ToCap( char *P ) { while ( *P != 0 ) { if ( *P >= 'a' && *P <='z' ) *P = (*P - 'a') + 'A'; P++; } } Output: Good Morning 123!! GOOD MORNING 123!! void main() { char ST[100]; strcpy( ST, "Good Morning 123!!\n" ); printf( ST ); ToCap( ST ); printf( ST ); } 15 strings sample #6 (scanf) #include <stdio.h> #include <string.h> void main() { char ST[100]; printf("Please enter a word\n"); scanf("%s", ST ); printf("Your word is: \"%s\“,\nand it has %d characters\n", ST, strlen(ST) ); } Output: Please enter a word Hello!!! Your word is: "Hello!!!", and it has 8 characters 16 sample #7 (float type) #include <stdio.h> void main() { int J; float F; double D; printf("Please enter a number\n"); scanf("%d", &J ); Output: Please enter a number 10 Please enter a fractional number 33.44 Your numbers are: 10 33.44 33.44 printf("Please enter a fractional number\n"); scanf("%f", &F ); D = F; printf("Your numbers are: %d %g %g\n", J, F, D ); } 17 strings sample #8 (sprintf) #include <stdio.h> #include <string.h> void main() { char ST[250]; int I = 22; sprintf( ST, "Test123 %d", I ); Output: Test123 22 printf( "%s\n", ST ); } 18 strings sample #9 (gets) #include <stdio.h> #include <string.h> void main() { char ST[250]; printf("Enter a word\n"); gets( ST ); printf("You entered: '%s'\n", ST ); } Output: Enter a word Too many examples!!! You entered: 'Too many examples!!!' 19 Matrix multiplication example #include <stdio.h> #include <stdlib.h> void MatMult( int N, double A[10][10], double B[10][10], double C[10][10] ) { int I,J,K; double D; for ( I = 0 ; I < N ; I++ ) { for ( J = 0 ; J < N ; J++ ) { D = 0.0; for ( K = 0 ; K < N ; K++ ) D = D + A[I][K] * B[K][J]; C[I][J] = D; } } } void PrintMat( int N, double A[10][10] ) { int I,J; for ( I = 0 ; I < N ; I++ ) { for ( J = 0 ; J < N ; J++ ) printf("%g, ", A[I][J] ); printf("\n"); } } void main() { double X[10][10], int N = 2, I, J; for ( I = 0 ; I < for ( J = 0 ; X[I][J] = Y[10][10], Z[10][10]; N ; I++ ) J < N ; J++ ) rand() % 10; for ( I = 0 ; I < N ; I++ ) for ( J = 0 ; J < N ; J++ ) Y[I][J] = rand() % 10; } MatMult( N, X, Y, Z ); printf("Matrix X is: \n"); printf("\nMatrix Y is: \n"); printf("\nMatrix Z is: \n"); PrintMat( N, X ); PrintMat( N, Y ); PrintMat( N, Z ); Output: Matrix X is: 1, 7, 4, 0, Matrix Y is: 9, 4, 8, 8, Matrix Z is: 65, 60, 36, 16, 20 Quiz (15 minutes) 1. Write a program that prints the multiplication table 2. Write a FUNCTION that gets as an input two strings, compares them, and returns either 0 or 1. If the strings are the same it returns 1, if not, it returns 0. Write a program that has two hard-coded (predefined) strings and uses the above function. 21 Next • Wednesday More standard libraries (math.h, stdlib.h, etc.) How to create a library of our own functions More pointers (type casting) The “Numerical Recipes” package Financial applications • Saturday C/C++ Interface to excel Question session 22