B142L Coursework assignment 1 – PRACTICE SAMPLE WITH SOLUTIONS
Academic year 2009/10
COURSEWORK DUE DATE
PRACTICE SAMPLE
COURSEWORK DESCRIPTION
Students are required to submit their coursework online using their individual coursework pages on theMosaic website.
Answer all questions. This assessment has a total of 20 marks. Each question will be marked according to the following criteria:
Marks
Criteria
5
Source code uses an effective design to answer the question, compiles without errors and includes informative comments.
4
Source code answers the question, compiles without errors and includes informative comments.
3
Source code answers the question and compiles without errors.
2
Source code is relevant to the question, but does not compile.
1
Source code is incorrect or not relevant to the question.
COURSEWORK QUESTIONS – PRACTICE SAMPLE
QUESTION 1
[5 Marks]
Write a C program that accepts 10 input integers and displays a message that the sum of all numbers is odd or even.
QUESTION 2
[5 Marks]
In the same program as question 1, add a for loop to iterate through all 10 input integers and display messages that the number is greater than, lesser than or equal to 100.
QUESTION 3
[5 Marks]
In the same program as question 1, add a while loop to iterate through all 10 input integers and display the result of dividing each integer by 4.192 Display the output values using 2 decimal places of precision.
QUESTION 4
[5 Marks]
In the same program as question 1, add a function that accepts 2 input integers and returns the highest number plus one. Call this function twice in the program with different input numbers and display the return values. For example, if the input integers are 8 and 11, then the function would return 12.
#include <stdio.h>
int maxPlusOne(int, int);
int main(void) { int numbers[10]; int sum = 0; int counter;
/* accept input integers and store them in an array */ for( counter = 0; counter < 10; counter++ ) {
scanf("%d", &numbers[ counter]);
sum = sum + numbers[ counter ]; /* calculate the sum of all inputs */ }
/* Required output for question 1 */ if(( sum % 2) == 0)printf("Sum of inputs is even.\n"); elseprintf("Sum of inputs is odd.\n");
/* Required output for question 2 */ for( counter = 0; counter < 10; counter++ ) { if( numbers[ counter ] > 100)printf("%d is greater than 100\n", numbers[ counter ]); elseif( numbers[ counter ] < 100)printf("%d is less than 100\n", numbers[ counter ]); elseprintf("%d is equal to 100\n", numbers[ counter ]); }
/* Required output for question 3 */
counter = 0; while( counter < 10) { printf("%d divided by 4.192 is equal to %.2f\n", numbers[ counter ], numbers[ counter ] / 4.192);
counter++; }