Presentation is loading. Please wait.

Presentation is loading. Please wait.

C workshop #3 flow control / strings.

Similar presentations


Presentation on theme: "C workshop #3 flow control / strings."— Presentation transcript:

1 C workshop #3 flow control / strings

2 do / while / for Output: * ** *** **** 0, 1, 2, 3, 1, 2, 3, 4,
#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; printf("%d, ",I); } while ( I < 4 ); while ( I++ < 4 ) while ( ++I < 4 ) } Output: * ** *** **** 0, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3,

3 goto Continue executing at different location Output:
#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

4 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 "); case 4: printf("you"); case 5: case 6: case 7: printf("?"); default: printf("*"); } printf("\n"); Output: **How are you???**

5 continue Used in ‘for / while / do’ loops Output:
#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"); Output: 0* 1* 2* 3* 4 5* 6* 7 8* 9*

6 break Used in ‘for / while / do’ loops Output: 0* 1* 2* 3* 4
#include <stdio.h> void main() { int I; for ( I = 0 ; I < 10 ; I++ ) { printf(“ %d", I ); if ( I == 4 ) break; printf(“*"); } printf("\n"); Output: 0* 1* 2* 3* 4

7 characters A single byte (a bytes defined as a number between 0 to 255), represents a single character Sample of character representations: 32= =! 34=" 35=# 36=$ 37=% 38=& 39=' 40=( 41=) 42=* 43= =, 45= = =/ 48= = = =3 52= = = = = = =: 59=; 60=< 61== 62=> 63=? 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=~

8 char sample #1 Output: t u u u
#include <stdio.h> void main() { char C, G, L; C = 't'; printf("%c\n", C ); C = 't' + 1; 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 ); } Output: t u u u Now, dear user, press any key, and then press ENTER 3 You entered: 3

9 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= =! 34=" 35=# 36=$ 37=% 38=& 39=' 40=( 41=) 42=* 43= =, 45= = =/ 48= = = =3 52= = = = = = =: 59=; 60=< 61== 62=> 63=? 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=~

10 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 !

11 strings sample #2 Output: Good Morning! Test0123 s
#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" ); C = ST[2]; printf("%c\n", C ); } Output: Good Morning! Test0123 s

12 strings sample #3 Output: 33 #include <stdio.h>
#include <string.h> void main() { char ST[100]; int I = 33; strcpy( ST, "%d\n" ); printf( ST, I ); } Output: 33

13 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

14 strings sample #5 Output: Good Morning 123!! GOOD MORNING 123!!
#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 ); Output: Good Morning 123!! GOOD MORNING 123!!

15 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 ); Output: Good Morning 123!! GOOD MORNING 123!!

16 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

17 sample #7 (float type) Output: Please enter a number 10
#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 ); } Output: Please enter a number 10 Please enter a fractional number 33.44 Your numbers are:

18 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

19 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!!!'

20 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 ( 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++ ) X[I][J] = rand() % 10; 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 ); Output: Matrix X is: 1, 7, 4, 0, Matrix Y is: 9, 4, 8, 8, Matrix Z is: 65, 60, 36, 16,

21 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.

22 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


Download ppt "C workshop #3 flow control / strings."

Similar presentations


Ads by Google