C 101 Davide Vernizzi April 9, 011 1 Input/Output Basic I/O. The basic I/O shown here can be used to get input from the user and to send output to the user. Note that usually input must be verified before it can be used. 1 printf (" Insert val "); scanf ("%d", & val ); Basic I/O with control. The following I/O expands the basic one with a simple loop for verifying that the user inserted a correct value. In the example we want a positive number. 1 do { printf (" Insert a positive number "); 3 scanf ("%d", & num ); 4 if ( num < 0) { printf (" You inserted a negative number. Try again \n"); } 8 } while ( num < 0); The do-while loop keeps asking the number until it is correclty inserted. Using a do-while ensures that it is executed at least once. Note that the condition of the loop is the opposite than what we accept, since the do-while loops when condition is true*/ Common mistakes. at beginning. 1 printf (" Insert val "); scanf ("%d", val ); These are common mistakes that are usually done 1
3 4 printf (" You inserted %d and %f\n"); printf (" You inserted %d and %f\n", f); printf (" You inserted %d and %f\n", f, val ); printf (" You inserted %d and %f\n", &val, &f); In line & is missing. In line 4 there are no parameters. In line there are not enough parameters. In line there are wrong parameters type. In line the & must not go in the printf. Loops.1 Defined loops Defined loops reiterate a block of instructions a defined number of times. Note that the number of iterations may be defined by the programmer (using a #define) or by the user (get the number in input from the user). Usually a #for statement is used for defined loops: 1 # define TIMES /*... */ 8 for (i =1; i <= TIMES ; i ++) { 9 /*... */ 10 } 11 1 /*... */ 13 14 } In line 1 we defined the number of loops and we used this definition in the for in line 8. Also a while statement can be used for defined loops: 1 # define TIMES
/*... */ 8 i =1; 9 while (i <= TIMES ) { 10 /*... */ 11 1 i ++; 13 } 14 1 /*... */ 1 1 } In line 8 we initialize the loop iterator, in line 9 we check if we finished the loop, and in line 1 we increment the loop iterator. Example 1. int num =0; int j =0; 8 Ask user a number num and write the first num numbers. 9 /* Get input from the user */ 10 printf (" Insert num "); 11 scanf ("%d", & num ); 1 13 /* for version */ 14 for (i =1; i <= num ; i ++) { 1 printf ("%d\n", i); 1 } 1 18 /* while version */ 19 j = 1; 0 while (j <= num ) { 1 printf ("%d\n", j); j ++; 3 } 3
4 return 0; } Why we did not use the #define here? Example. int num =0; int j =0; 8 Do the same of Example 1, but in a reversed order. 9 /* Get input from the user */ 10 printf (" Insert num "); 11 scanf ("%d", & num ); 1 13 /* for version */ 14 for (i= num ; i >=1; i - -) { 1 printf ("%d\n", i); 1 } 1 18 /* while version */ 19 j = num ; 0 while (j >=1) { 1 printf ("%d\n", j); j - -; 3 } 4 return 0; } Note the usage of iterators.. Undefined loops Undefined loops reiterate a block of instructions a undefined number of times. The number of iterations is not known a-priori, but depends on a condition. Usually a while or do-while statement is used for defined loops: 4
1 int main () { 3 4 /*... */ while ( cond ) { /*... */ 8 } 9 10 do { 11 } while ( cond ); 1 13 /*... */ 14 1 } In line 11 note that the do-while requires the ; (instead the while does not require it). It is possible to use a for for undefined loops, but it is not usual: 1 int main () { 3 4 /*... */ for (; cond ;) { /*... */ 8 } 9 10 /*... */ 11 1 } Example 3. Read numbers until the user inserts a 0 and compute the sum of the number inserted. 4 int num =0; int sum =0; do {
8 scanf ("%d", & num ); 9 sum += num ; 10 } while ( num!=0); 11 1 printf (" Sum = %d\n", sum ); 13 14 return 0; 1 } We used the do-while because there is at least one input from the user. Note that we initialized the sum variable. Example 4. of do-while. 4 int num =0; int sum =0; Do the same of Example 3, but using while and for instead /* while version */ 8 sum = 0; 9 10 scanf ("%d", & num ); 11 sum += num ; 1 13 while ( num!= 0) { 14 scanf ("%d", & num ); 1 sum += num ; 1 } 1 18 printf (" Sum = %d\n", sum ); 19 0 /* for version */ 1 sum = 0; 3 scanf ("%d", & num ); 4 sum += num ; for (; num!=0;) { scanf ("%d", & num ); 8 sum += num ; 9 }
30 31 printf (" Sum = %d\n", sum ); 3 33 return 0; 34 }.3 Nested loops It is possible to nest a loops inside other loops: 1 # define N 10 4 int i, j; /*... */ 8 for (i =1; i <=N; i ++) { 9 for (j =1; j <=N; j ++) { 10 /*... */ 11 } 1 } 13 14 /*... */ 1 1 } Note that in case of for (or other defined loops) different iterators must be used. It is also possible to nest different types of loops together: 1 # define N 10 4 int i, j; /*... */ 8 while ( cond ) { 9 for (j =0; j<n; j ++) { 10 /*... */ 11 } 1 }
13 14 /*... */ 1 1 } Example. Print the multiplication table until 10. # define N 10 3 4 int main () { int i, j; int val ; 8 for (i =1; i <=N; i ++) { 9 for (j =1; j <=N; j ++) { 10 val = i*j; 11 printf ("%3d ", val ); 1 } 13 printf ("\n"); 14 } 1 return 0; 1 } Note the indentation! Example. Ask user a number num and write the first num numbers. Repeat until user inserts 0. int num =0; do { 8 /* Get input from the user */ 9 printf (" Insert num "); 10 scanf ("%d", & num ); 11 1 /* for version */ 13 for (i =1; i <= num ; i ++) { 14 printf ("%d\n", i); 8
1 } 1 } while ( num!= 0); 1 18 return 0; 19 } 3 Arrays Arrays I/O. For getting arrays in input and for putting arrays in output, usually a for is used: 1 # define DIM 10 int array [ DIM ]; for (i =0; i<dim ; i ++) { 8 printf (" Insert the vector element % d ", 9 i); 10 scanf ("%d", & array [i ]); 11 } 1 13 for (i =0; i<dim ; i ++) { 14 printf (" Vector element %d: %d\n", 1 i, array [i ]); 1 } 1 18 return 0; 19 } In line 1 we defined the length of the array which we used in the for in line and line 13. Unless you have very special reasons, always define the size of the array and use the defined size and never use the number directly. Example. Ask user a number num (max 10), get num numbers in input from the user and print them in reversed order. # define DIM 10 3 4 int main () { 9
int i; int num ; int array [ DIM ]; 8 9 do { 10 printf (" Insert a number ( max 10) "); 11 scanf ("%d", & num ); 1 if ( num > 10) { 13 printf (" Max 10!\ n"); 14 } 1 } while ( num > 10); 1 1 for (i =0; i<num ; i ++) { 18 printf (" Insert the vector element % d ", 19 i); 0 scanf ("%d", & array [i ]); 1 } 3 for (i=( num -1);i >=0; i - -) { 4 printf (" Vector element %d: %d\n", i, array [i ]); } 8 return 0; 9 } Note that it is essential to verify the input in this case because we could operate outside the array. Why we start from (num-1) in the second for? 10