Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

Similar documents
Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

C Introduction. Comparison w/ Java, Memory Model, and Pointers

Dynamic Memory Allocation and Command-line Arguments

AMCAT Automata Coding Sample Questions And Answers

Kurt Schmidt. October 30, 2018

EL2310 Scientific Programming

TI2725-C, C programming lab, course

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

High-performance computing and programming Intro to C on Unix/Linux. Uppsala universitet

Memory. What is memory? How is memory organized? Storage for variables, data, code etc. Text (Code) Data (Constants) BSS (Global and static variables)

Introduction to Languages for Scientific Computing, winter semester 14/15: Final Exam

CS16 Midterm Exam 1 E01, 10S, Phill Conrad, UC Santa Barbara Wednesday, 04/21/2010, 1pm-1:50pm

EL2310 Scientific Programming

ECE 264 Exam 2. 6:30-7:30PM, March 9, You must sign here. Otherwise you will receive a 1-point penalty.

ESC101N: Fundamentals of Computing End-sem st semester

ECE264 Spring 2014 Exam 2, March 11, 2014

CSE 303 Winter 2008 Midterm Key

Motivation was to facilitate development of systems software, especially OS development.

Understanding Pointers

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

From Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides

Project 1: How to Make One Dollar

unsigned char memory[] STACK ¼ 0x xC of address space globals function KERNEL code local variables

United States Naval Academy Electrical and Computer Engineering Department EC310-6 Week Midterm Spring AY2017

Sample Examination. Family Name:... Other Names:... Signature:... Student Number:...

Motivation was to facilitate development of systems software, especially OS development.

File IO and command line input CSE 2451

Memory Management. CSC215 Lecture

ch = argv[i][++j]; /* why does ++j but j++ does not? */

Midterm Examination # 2 Wednesday, March 19, Duration of examination: 75 minutes STUDENT NAME: STUDENT ID NUMBER:

Recitation 2/18/2012

Common Misunderstandings from Exam 1 Material

York University Faculty Science and Engineering Fall 2008

Introduction to Computing Systems Fall Lab # 3

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

Dynamic memory allocation

In the case of the dynamic array, the space must be deallocated when the program is finished with it. free(x);

mith College Computer Science CSC352 Week #7 Spring 2017 Introduction to C Dominique Thiébaut

Discussion 3 Richard Guo Advanced C 01/28/09

C Language: Review. INFO High Performance Scientific Computing. 26 septembre 2017

Midterm Examination # 2 Wednesday, March 18, Duration of examination: 75 minutes STUDENT NAME: STUDENT ID NUMBER:

Pointer Casts and Data Accesses

C Programming Review CSC 4320/6320

CS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015

Programming for Engineers Arrays

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

COMP 2001/2401 Test #1 [out of 80 marks]

ECE264 Fall 2013 Exam 3, November 20, 2013

POINTER AND ARRAY SUNU WIBIRAMA

C programming basics T3-1 -

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Arrays and Pointers. CSE 2031 Fall November 11, 2013

Midterm Exam Nov 8th, COMS W3157 Advanced Programming Columbia University Fall Instructor: Jae Woo Lee.

CSE 333 Lecture 2 Memory

15213 Recitation Section C

Project#3 [40 points] COP 2220 Fall 2017

PRINCIPLES OF OPERATING SYSTEMS

Quiz 0 Answer Key. Answers other than the below may be possible. Multiple Choice. 0. a 1. a 2. b 3. c 4. b 5. d. True or False.

ENG120. Misc. Topics

Final Exam 1 /12 2 /12 3 /10 4 /7 5 /4 6 /10 7 /8 8 /9 9 /8 10 /11 11 /8 12 /10 13 /9 14 /13 15 /10 16 /10 17 /12. Faculty of Computer Science

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

Intermediate Programming, Spring 2017*

More Arrays. Last updated 2/6/19

FALL 2017 CSCI 304 LAB1 (Due on Sep-19, 11:59:59pm)

CSCI 4210 Operating Systems CSCI 6140 Computer Operating Systems Homework 3 (document version 1.2) Multi-threading in C using Pthreads

C Programming Language: C ADTs, 2d Dynamic Allocation. Math 230 Assembly Language Programming (Computer Organization) Thursday Jan 31, 2008

CS 108 Computing Fundamentals. October/November Array Bootcamp

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

Computer Security. Robust and secure programming in C. Marius Minea. 12 October 2017

CSE 333 Midterm Exam July 24, Name UW ID#

Programming in C Lecture Tiina Niklander

Computer Architecture I Mid-Term I

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

C introduction: part 1

Operating System Labs. Yuanbin Wu

Arrays and Pointers (part 2) Be extra careful with pointers!

ECE264 Spring 2013 Exam 1, February 14, 2013

Memory and Addresses. Pointers in C. Memory is just a sequence of byte-sized storage devices.

Answer all questions. Write your answers only in the space provided. Full marks = 50

Arrays and Pointers (part 2) Be extra careful with pointers!

C: How to Program. Week /Mar/05

Today s Learning Objectives

Chapter 2. Section 2.5 while Loop. CS 50 Hathairat Rattanasook

Stream Model of I/O. Basic I/O in C

Binary Search Tree 1.0. Generated by Doxygen Mon Jun :12:39

These are reserved words of the C language. For example int, float, if, else, for, while etc.

Programming in C week 1 meeting Tiina Niklander

Computer System and programming in C

ECE264 Spring 2013 Final Exam, April 30, 2013

// file2.c. // file1.c #include <stdio.h> int A1 = 42; // 1.1 static int B1; // 1.2. int A2 = 12; // 2.1 int B2; // 2.2. extern int A2; // 1.

CSE 333 Midterm Exam 7/25/16. Name UW ID#

CSCI-243 Exam 2 Review February 22, 2015 Presented by the RIT Computer Science Community

EE 312 Fall 2018 Midterm 1 Version A October 10, 2018

Hacettepe University Department Of Computer Engineering BBM 103 Introduction to Programming Experiment 5

Lecture 2: C Programming Basic

Variables and literals

Arrays. An array is a collection of several elements of the same type. An array variable is declared as array name[size]

Programming in C First meeting

Transcription:

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 );