CS101 Prelab 10


Name: __________________________________

NetId: ____________________________      Section: _____________  
 
  Each blank is worth 1/6 point.

Print out this page and fill in the blanks to answer the following questions. Hand in this assignment at the beginning of Lab 10. As with all prelabs/labs, if you are not sure of the answer you may go to any EWS lab on campus and check your results by using the gcc compiler, or you can ask anyone of the CS101 staff for help.

The following exercises should help prepare you for the in-lab activities and MP2. You will use the C loop statements (while, do-while,for) and arrays.  
 
  1. (1/6 pts)Write the output of the following code:
    #include <stdio.h>

    void main(void){
    int i,j;
    int temp;
    int x[3] = { 3, 4, 1};

    /* bubbles(smaller values) float up(move to the beginning of the array) */
    for(j=3; j > 1; j=j-1)
    for(i=0; i < j-1; i=i+1)
    if(x[i] > x[i+1])
    {
    /* swap */
    temp = x[i];
    x[i] = x[i+1];
    x[i+1] = temp;
    }


    for(i=0; i< 3; i=i+1)
    printf("%i ",x[i]);

    printf("\n");
    }



    ____________________1 3 4 ___________________________  

  2. (2/6 pts) The following program compiles and runs without errors. Write the output the program produces.

  3. #include <stdio.h>
    void main(void)
    {
    char string1[] = {'H','e','l','l','o','\n'};
      char string2[] = "Hello";  
    int result = strcmp(string1,string2);

      if (result == 0) 
         printf("Equal!");
    else 
        printf("Not equal!");
    printf("\n");

    if( strcmp("Hello", string2) == 0)
    printf("Equal!");
    else
    printf("Not equal!");
    printf("\n");
    }

    ______Not equal!______________



    _______Equal!___________________
     
  1. (4/6 pts)Suppose you have an integer array scores[40] that already contains the exam scores for 40 students. Complete the following code to compute the average score, and store it in the variable average. (Don't use integer division to calculate the average).
  2. int i,total = 0;
    int scores[40];
    double average;
    /* some code not shown here that assigns values to the scores array
    */

    for(i=0; ____i < 40_____; ______i++______or____i=i+1_____or_____ ++i_______) {


       _______ total = total + scores[i];_________

    }
    average = __________total / 40.0__________;
  3. (4/6 pts)Given an array exactly as in the previous question. Complete the following code to find the lowest score. The variable min is an integer.
  4. min = scores[0];

    for(i=1; ______i < 40________; ______i = i + 1_____or variations ) {


      if ( ______min >_________ scores[i])


            min = ____________scores[i]____________;
    }
    printf("The lowest score is %i.\n",min);
  5. (2/6 pts)Complete the following program that prompts a user for her first name and then reads the name and responds with a personal greeting.
    For example:
    Enter your first name: Sue
    Hello Sue !
    #include <stdio.h>
    void main(void)
    {
    char
    fname[72];
    char greeting[132];
    printf("Enter your first name:
    ");


    scanf("%s", ___fname____);


    strcpy(greeting , "Hello ");

    strcat(greeting , ____fname______);


    strcat(greeting , " !");

    printf(" %s \n", greeting);
    }
  6. (5/6 pts)Write the output the following program produces when run. See lecture 15-21.

    #include <stdio.h>

    void main(void)
    {
    char password[32] = "CS101";
    char course[32] = {'C','S','1','\0','5'};
    printf("%s \n", password);
    printf("%s \n", course);
    printf("%c \n",password[1]);
    strcpy(password, course);
    printf("%s \n", password);
    if (strcmp( password, "CS101")  > 0)
    printf("yes \n");
    else
    printf("no \n");
    }
    ____________CS101___________________________


    ___________CS1____________________________


    ___________S____________________________


    ___________CS1____________________________


    ___________no____________________________