Big Quiz 2.doc

advertisement
Big Quiz 2
Time: 50 minutes
Notes:
- Implement each problem in a different file
- Implement Problem1
- Implement any two problems from 2, 3 and 4
<File Name: BigQuiz1.c>
1. The following program asks the user to enter the scores of 6 students and
computes their average. However, there are some bugs in it, please fix.
#include<stdio.h>
int * getValue(void)
{
static int a[6];
int i;
for (i=1; i<6; i++)
scanf("%d", a++);
return a;
}
int main(void)
{
int j, sum;
int *score;
printf("Enter the 6 scores:");
score = getValue ();
for (j=1; j<6; j++)
sum += &score[j];
printf("Average score is: %.2f\n", sum/6);
return 0;
}
<File Name: BigQuiz2.c>
2. Write the following function:
Function name: WhichTriangle
Argument Names and Types: sides (integer array indicating length of the three sides),
perimeter (pointer to an integer)
Returns: an integer, 0 if the triangle is scalene (all sides different), 1 if it is equilateral, 2
if only two sides are equal.
Requirement: Cannot use array brackets inside the function
<File Name: BigQuiz3.c>
3. Implement a program that asks the user to input a positive
value n, then calculate the value of
1*2+2*3+3*4+...+(n-1)*n
by using a recursive function with the prototype:
void func(int n);
Do not just print out the solution, print the sequence of
numbers as well [i.e. for n = 4, print "1*2+2*3+3*4 = 20"]
**No loops are allowed.
<File Name: BigQuiz4.c>
4. Given the following declarations, convert each statement
below to an equivalent statement without using square
brackets, [].
/* declarations */
int a, b[3] = {2, 4, 6}, c[2] = {0, 1};
int *ptr1;
/* statements */
ptr1 = &a[0];
a = c[1];
b[2] = a;
Download