CS 102 Lecture Notes
Selected C Topics


The C Language

I/O: formatted printf() and scanf()





__________________________________

Note:
     scanf("%d%d%lf", &x, &y, &z);   with no spaces between placeholders is the same as
     scanf(" %d %d %lf", &x, &y, &z);

Variables of type char are different!

     scanf("%d%c", &x, &ch);     on input   7<blank>A   stores a blank in ch.
     scanf("%d %c", &x, &ch);    on input   7<blank>A   stores A in ch.

A blank space before %c informs the system to ignore white spaces;
without the space the blank is treated as an input character.

__________________________________

  • Using the scanf() return code
  • Syntax:   return_code = scanf("control_str", var_list);

    
     /* Read, count, and sum up to N integers. */
    
     #include <stdio.h>
     #define N 1000
     
     void read_and_sum_it(int *ct, int *sum);
     
     int main()  
     {
       int count, sum;
    
       read_and_sum_it(&count, &sum);
       printf("count: %d    sum: %d\n", count, sum); 
     }
     
     void read_and_sum_it(int *count, int *sum)  
     {
       int x;
     
       for(*count = *sum = 0; 
           scanf("%d", &x) != EOF && *count < N; 
           (*count)++, *sum += x);
     }
    

  • More I/O
  • Formatting Output Values

  • Data Types Revisited
  • Structures


  • Array-based Insertion Sort     O(n^2)

    Array-based linked list     (WOW!!)

    Below we have a representation of parallel arrays. Array data is an array of strings; array link is an array of integers. Integer variable first holds the index of (points to) the first name in the logically sorted array of strings. The initial value, -1, indicates an empty array. Integer variable next holds the index (points to) the next available slot in which to insert a new string.
      data link +-------------+-------+ 0| | | +-------------+-------+ 1| | | +-------------+-------+ 2| | | +-------------+-------+ 3| | | +-------------+-------+ 4| | | +-------------+-------+ 5| | | +-------------+-------+ 6| | | +-------------+-------+ 7| | | +-------------+-------+ 8| | | +-------------+-------+ 9| | | +-------------+-------+

    Can you do this on your own?  Try it at home, then click for results.

    We might have more sophisticated commands that call functions to process the list. This is an example of information hiding and procedural abstraction. Some possible functions are:

    Try: There is room for Don but we are unable to reference an unused slot.

    Solution - keep a linked list that is implemented as a stack of available slots in the link field!

      data link +-----------------------+----------------+ 0| | 1 | +-----------------------+----------------+ 1| | 2 | +-----------------------+----------------+ 2| | 3 | +-----------------------+----------------+ 3| | 4 | +-----------------------+----------------+ 4| | -1 | +-----------------------+----------------+
    input:  1) add Mel   2) add Linda    3) add Sara    4) add Carol   5) add Tori 
        6) delete Sara 7) add Belle 8) delete Belle 9) delete Tori
    The result after command 5 is this:
    
      data link +-----------------------+-----------------+ 0| Mel | 2 | +-----------------------+-----------------+ 1| Linda | 0 | +-----------------------+-----------------+ 2| Sara | 4 | +-----------------------+-----------------+ 3| Carol | 1 | +-----------------------+-----------------+ 4| Tori | -1 | +-----------------------+-----------------+
      first: 3 avail: -1

    We continue with commands 6 through 9:
        6) delete Sara  7) add Belle   8) delete Belle   9) delete Tori
    
    The final result after commands 6, 7, 8, and 9 is this:
    
      data link +-------------+-------+ 0| Mel | -1 | +-------------+-------+ 1| Linda | 0 | +-------------+-------+ 2| Belle | -1 | +-------------+-------+ 3| Carol | 1 | +-------------+-------+ 4| Tori | 2 | +-------------+-------+
      first: 3 avail: 4
    Notice:


    Click for another example.