Grade: / 20 Lab Exam 1 D500 1. [1 mark] Give an example of a sample input which would make the function scanf( "%f", &f ) return 0? Answer: Anything that is not a floating point number such as 4.567 or EOF (CTRL+D). 2. [2 marks] Write the compile command you would enter at the command line to compile the files main.c and image.c such that the name of the executable file is mi. Answer: gcc o mi main.c image.c 3. [3 marks] Circle the errors in the following program and write the correct statements in order for the program to compile, execute and print the data read from standard input properly, without being truncated. The program would then be executed with this command line statement:./a.out 99 4.567 #include <stdio.h> int main( char* argv[], int argc ) { -> int argc, char* argv[] int i; float f; if ( argc!= 3 ) { printf( "I wanted 2 arguments!\n" ); else { f = atof( argv[1] ); -> 2 i = atoi( argv[2] ); -> 1
printf( "i = %d and f = %.2f.\n", i, f ); OR #include <stdio.h> int main( char* argv[], int argc ) { -> int argc, char* argv[] if ( argc!= 3 ) { printf( "I wanted 2 arguments!\n" ); else { float f = atof( argv[1] ); -> 2 int i = atoi( argv[2] ); -> 1 printf( "i = %d and f = %.2f.\n", i, f ); OR #include <stdio.h> int main( char* argv[], int argc ) { -> int argc, char* argv[] if ( argc!= 3 ) { printf( "I wanted 2 arguments!\n" ); else { float f = atof( argv[1] ); -> 2 int i = atoi( argv[2] ); -> 1 printf( "i = %d and f = %.2f.\n", i, f ); printf( "i = %d and f = %.2f.\n", i, f );
4. [3 marks] Imagine there is a program that reads course marks (assignment marks, exam marks, etc ) and computes a student final mark for a particular course. Imagine also that, contrary to most of our tasks so far, the input to this program is not guaranteed to be well-formed. Your task in this question is to list two different sample input, and corresponding output, you would use in order to test this program to ensure it works no matter what data is entered. 1. Sample input: Well-formed input (valid input) such as 74 67 89 65 (we can assume that they are all out of 100) Sample output: Final mark: 73.75% 2. Sample input: Not well-formed input (invalid input) such as sausage Sample output: Sausage is not a number. Unable to compute final mark 5. [2 marks] We have seen that the memory is divided in various segments. For example, the program's text (compiled code) is stored in the text segment. Which of these memory segments is being used when we deallocate memory space for a variable by calling free( )? Answer: heap 6. [2 marks] What does the following syntactically correct C statement print on the computer monitor screen? printf( "%lu", sizeof(250/2)); Answer: 4
7. [7 marks - Read the entire question first] Write a program called LabExam1.c which is to satisfy the following requirements: Use this incomplete main function as your starter program: #include <stdio.h> #include <stdint.h> int main( void ) { int floordim = 8; // Floor is 8 x 8 uint8_t * thefloor = createfloor( floordim ); // Add more code here free( thefloor ); You are asked to write the function createfloor which must i. Allocate an array representing a square floor of floordim by floordim of values of uint8_t data type. ii. Fill this array with two given values: 0 and 1. Careful: this floor has a particular pattern. Each row of tiles (array cells) are alternatively set to each of these two values.
Once createfloor function returns to the main function, the main function must print the floor pattern as a square to stdout as follows (assuming the executable is called le1) - sample output: $./le1 00000000 11111111 00000000 11111111 00000000 11111111 00000000 11111111 $ When adding code to the main function, you can only do so after the call to the createfloor function in the main function, i.e., where it says // Add more code here. You cannot change the code you have been given above. Make sure your last printed character is a newline character \n as shown above in the sample output. Make sure your code file LabExam1.c compiles and executes as expected. When it does, submit it to CourSys before or at the end of your lab exam. Remember, late submissions will get 0. Suggestion: Implement small sections of your LabExam1.c at a time, then compile it, test it and submit it to CourSys. Repeat this until you have implemented your entire LabExam1.c. Know that you can submit your file many times to CourSys. Also know that only the last submission will be marked.
Good luck! /* Lab Exam 1 - Q7 - D500 */ #include <stdio.h> // for printf() #include <stdint.h> // for uint8_t uint8_t * createfloor( int floordim ) { uint8_t value1 = 0; uint8_t value2 = 1; // explicitly allocate space for an array of ints uint8_t * anarray = malloc( floordim*floordim * sizeof(uint8_t) ); // always check a system call for errors if( anarray == NULL ) { perror( "Allocating an array of size failed!" ); exit(1); // fill in the floor pattern for (unsigned int row = 0; row < floordim; row++ ) { for (unsigned int col = 0; col < floordim; col++ ) { if ( ( row % 2 ) == 0 ) anarray[row * floordim + col] = value1; else anarray[row * floordim + col] = value2; // return the array (the floor pattern) return anarray; int main( void ) { int floordim = 8; uint8_t * thefloor = createfloor( floordim ); // print the floor pattern by rows for (unsigned int row = 0; row < floordim; row++ ) { for (unsigned int col = 0; col < floordim; col++ ) { printf( "%d", thefloor[row * floordim + col] ); printf("\n");
// de-allocate the memory allocated by malloc free( thefloor );