Thread: another exam question

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    11

    another exam question

    This another question that will be placed on the exam but with a different layout. Give it a try... I have others that I'm considering.
    Give me your answer and opinion (fair or not fair) for a beginner learning C. Thanks for your help!

    The following function supposedly computes the sum and average of the numbers in the array a, which has the length n. avg and sum point to variables that the function should modify. Unfortunately, the function contains several error; find and correct them.

    Code:
    void avg_sum(float a[], int n, float *avg, float *sum)
    {
    int i;
    
    sum = 0.0;
    
    for(i = 0; i < n; i++);
         sum += a[i];
    avg = sum / n;
    
    }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Looks like a fair question to me.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    OK, you seem to be on a hunt for questions.... here's one for you.

    Here's some code that works.... or does it?!

    Code:
    /*
     * sample1.c
     * This program adds up the values in the array,
     * and prints the results to the screen
     */
    
    #include <stdio.h>
    
    int Adder(int a[])
    {
      int i;
      int myTotal;
      for (i = 0; i < sizeof(a); i++)
        myTotal += a[i];
      return myTotal;
    }
    
    int main(void)
    {
      int myarray[6] = {5, 1, 3, 2, 4};
      int Total;
      
      Total = Adder(myarray);
      printf ("The total of the %d elements is %d\n", sizeof(myarray), Total);
      
      return(0);
    }
    As you've guessed/worked out, it doesn't do what it's supposed to. Explain the problems and suggest some corrections.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    How about this?

    In C99, what will the following code print?

    Code:
    #include<stdio.h>
    
    int main(void)
    {
        for(unsigned char i=0;i<=255;printf("%i\n",i++));
    }
    a) The program will print characters from the character set from value 0 through 255 each on a new line.
    b) The program will print numbers from 0 through 255 each on a new line.
    c) The program will print the numbers 0 through 255 each on a new line and repeat forever.
    d) Both b and c are possible results.
    e) It is not a valid for loop because of where i is declared.
    f) It is not a valid for loop because of where the call to printf occurs.
    g) It is not a valid for loop because it has no body.
    h) The program will not compile because main doesn't return anything.

  5. #5
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Talking

    As an instructor as well I have posted questions in the past on this board. The average question is a fair question. I teach C as well to beginners and intermediate students.
    Mr. C: Author and Instructor

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  3. Question about linked lists.
    By cheeisme123 in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2003, 01:36 PM
  4. sort problem for an exam
    By rjeff1804 in forum C Programming
    Replies: 10
    Last Post: 02-12-2003, 10:33 PM
  5. The AP Exam.....
    By RoD in forum A Brief History of Cprogramming.com
    Replies: 32
    Last Post: 02-10-2003, 09:46 PM