220 likes | 334 Views
C workshop #3. flow control / strings. do / while / for. #include <stdio.h> void main() { int I = 1, J; do { for ( J = 0 ; J < I ; J++ ) printf("*"); printf("<br>"); I++; } while ( I < 5 ); I = 0; do { printf("%d, ",I); I++; } while ( I < 4 ); printf("<br>");
E N D
C workshop #3 flow control / strings
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"); I = 0; while ( I++ < 4 ) printf("%d, ",I); printf("\n"); I = 0; while ( ++I < 4 ) printf("%d, ",I); printf("\n"); } Output: * ** *** **** 0, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3,
goto • Continue executing at different location #include <stdio.h> void main() { printf("1\n"); goto LABEL_QQ; printf("2\n"); 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. Output: 1 3
switch / case Output: **How are you???** #include <stdio.h> void main() { int I; for ( I = 0 ; I < 10 ; I++ ) { switch (I) { 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"); }
continue Output: 0* 1* 2* 3* 4 5* 6* 7 8* 9* • Used in ‘for / while / do’ loops #include <stdio.h> void main() { int I; for ( I = 0 ; I < 10 ; I++ ) { printf(“ %d", I ); if ( I == 9 || I==3 ) continue; printf(“*"); } printf("\n"); }
break Output: 0* 1* 2* 3* 4 • Used in ‘for / while / do’ loops #include <stdio.h> void main() { int I; for ( I = 0 ; I < 10 ; I++ ) { printf(“ %d", I ); if ( I == 4 ) break; printf(“*"); } printf("\n"); }
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=~
char sample #1 Output: t u u u Now, dear user, press any key, and then press ENTER 3 You entered: 3 #include <stdio.h> void main() { char C, G, L; C = 't'; printf("%c\n", C ); C = 't' + 1; 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 ); }
char sample #2 #include <stdio.h> void main() { char C; for ( C = 32 ; C < 127 ; C++ ) { printf("%3d=%c ", C, C ); } printf("\n"); } 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=~
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’). #include <stdio.h> void main() { char S[100]; 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] ); } Output: Hi! H i ! 72 105 33 0
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 ); strcpy( ST, "Test0123" ); printf("%s\n", ST ); C = ST[2]; printf("%c\n", C ); } Output: Good Morning! Test0123 s
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
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
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++; } } void main() { char ST[100]; strcpy( ST, "Good Morning 123!!\n" ); printf( ST ); ToCap( ST ); printf( ST ); } Output: Good Morning 123!! GOOD MORNING 123!!
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++; } } void main() { char ST[100]; strcpy( ST, "Good Morning 123!!\n" ); printf( ST ); ToCap( ST ); printf( ST ); } Output: Good Morning 123!! GOOD MORNING 123!!
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
sample #7 (float type) Output: Please enter a number 10 Please enter a fractional number 33.44 Your numbers are: 10 33.44 33.44 #include <stdio.h> void main() { int J; float F; double D; printf("Please enter a number\n"); scanf("%d", &J ); printf("Please enter a fractional number\n"); scanf("%f", &F ); D = F; printf("Your numbers are: %d %g %g\n", J, F, D ); }
strings sample #8 (sprintf) #include <stdio.h> #include <string.h> void main() { char ST[250]; int I = 22; sprintf( ST, "Test123 %d", I ); printf( "%s\n", ST ); } Output: Test123 22
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!!!'
Matrix multiplication example Output: Matrix X is: 1, 7, 4, 0, Matrix Y is: 9, 4, 8, 8, Matrix Z is: 65, 60, 36, 16, #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], Y[10][10], Z[10][10]; int N = 2, I, J; for ( I = 0 ; I < N ; I++ ) for ( J = 0 ; J < N ; J++ ) X[I][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"); PrintMat( N, X ); printf("\nMatrix Y is: \n"); PrintMat( N, Y ); printf("\nMatrix Z is: \n"); PrintMat( N, Z ); }
Quiz (15 minutes) • Write a program that prints the multiplication table • 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.
Next • WednesdayMore standard libraries (math.h, stdlib.h, etc.)How to create a library of our own functionsMore pointers (type casting)The “Numerical Recipes” package Financial applications • Saturday C/C++Interface to excel Question session