Input, output, control and structure of C programs
Coursework Due Date:
6pm March 20, 2009
Lecturer:
C Nguyen
COURSEWORK DESCRIPTION
This is the referral assessment for Coursework 1. This assessment has a total of 20 marks. Students must obtain a mark of 8 or higher for this referral assessment in order to increase their Coursework 1 mark to 40%.
Students are required to submit their programs online using the Mosaic website. Add the programs to the top of your coursework page.
Each question will be marked according to the following criteria:
Marks
Criteria
5
Program uses an effective design to answer the question, compiles without errors and includes informative comments.
4
Program answers the question, compiles without errors and includes informative comments.
3
Program answers the question and compiles without errors.
2
Program is relevant to the question, but does not compile.
1
Program is incorrect or not relevant to the question.
COURSEWORK QUESTIONS
QUESTION 1
Write a C program that accepts 10 input numbers and displays a message that the sum of all numbers is higher, lower or equal to 300. [5 Marks]
int main(void) { int number, counter, sum;
for( sum = 0, counter = 0; counter < 10; counter++ ) { printf("\nPlease enter a number: ");
scanf("%d", &number );
sum += number; }
if( sum > 300) printf("\nSum is higher than 300"); if( sum < 300) printf("\nSum is lower than 300"); if( sum == 300) printf("\nSum is equal to 300"); }
QUESTION 2
Write a C program that uses an iterative loop to display all multiples of 3, 5 or 7 starting at 300 and increasing to 800. [5 Marks]
Write a C program that accepts an input text string. If the input string contains the letter 'w', then displays it to screen. Otherwise, displays an error message, "Not found". [5 Marks]
#include <string.h>
int main(void) { char text[256] = {'\0'}; int found = 0; int counter;
printf("Please enter a string: ");
scanf("%s", text );
if( found ) printf("\n%s", text ); else printf("\nNot found"); }
QUESTION 4
Write a C program that accepts 20 input numbers and stores the values in an array. The program must only accept positive numbers. When the input number is not positive, the program must display an error message, "Invalid input", and allow the user to input another number. [5 Marks]
int main(void) { int numbers[20] = {0}; int input = 0; int counter = 0;
do { printf("\nPlease enter a number: ");
scanf("%d", &input );