# Thread: another exam question

1. ## 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. Looks like a fair question to me.

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

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

Popular pages Recent additions