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

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

ECE264 Spring 2013 Exam 1, February 14, 2013

PRINCIPLES OF OPERATING SYSTEMS

AMCAT Automata Coding Sample Questions And Answers

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

Problem Set 1: Unix Commands 1

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

ECE264 Fall 2013 Exam 1, September 24, 2013

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

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

Project 1: How to Make One Dollar

Dynamic Memory Allocation and Command-line Arguments

ECE264 Spring 2014 Exam 2, March 11, 2014

EL2310 Scientific Programming

CSE 333 Midterm Exam July 24, Name UW ID#

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

Approximately a Test II CPSC 206

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

Kurt Schmidt. October 30, 2018

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

Programming for Engineers Arrays

Approximately a Final Exam CPSC 206

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

15213 Recitation Section C

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

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

Warm-up sheet: Programming in C

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

EL2310 Scientific Programming

COMP s1 Lecture 1

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

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

بسم اهلل الرمحن الرحيم

Project 2: Shell with History1

Recitation 2/18/2012

CpSc 1111 Lab 4 Formatting and Flow Control

ECE264 Summer 2013 Exam 1, June 20, 2013

C programming basics T3-1 -

ECE264 Spring 2013 Final Exam, April 30, 2013

mith College Computer Science CSC231 Bash Labs Week #10, 11, 12 Spring 2017 Introduction to C Dominique Thiébaut

CSE 333 Midterm Exam 7/25/16 Sample Solution. Question 1. (10 points) Preprocessor. Suppose we have the following two files:

C Programming Review CSC 4320/6320

School of Computer Science Introduction to Algorithms and Programming Winter Midterm Examination # 1 Wednesday, February 11, 2015

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

Lesson #8. Structures Linked Lists Command Line Arguments

SU 2017 May 11/16 LAB 2: Character and integer literals, number systems, character arrays manipulation, relational operator

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

Intermediate Programming, Spring 2017*

Understanding Pointers

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

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

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

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

upper and lower case English letters: A-Z and a-z digits: 0-9 common punctuation symbols special non-printing characters: e.g newline and space.

C Functions. CS 2060 Week 4. Prof. Jonathan Ventura

SU2017. LAB 1 (May 4/9) Introduction to C, Function Declaration vs. Definition, Basic I/O (scanf/printf, getchar/putchar)

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

Subject: PROBLEM SOLVING THROUGH C Time: 3 Hours Max. Marks: 100

CSE101-Lec#17. Arrays. (Arrays and Functions) Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU. LPU CSE101 C Programming

CSE 333 Midterm Exam 5/10/13

JTSK Programming in C II C-Lab II. Lecture 3 & 4

Basic C Programming. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

CS 101, Spring 2016 March 22nd Exam 2

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

Introduction to C Language (M3-R )

Introduction to C programming. By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE

File IO and command line input CSE 2451

Programming in C. Pointers and Arrays

Chapter 14 - Advanced C Topics

This exam is to be taken by yourself with closed books, closed notes, no calculators.

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

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

York University Faculty Science and Engineering Fall 2008

printf( Please enter another number: ); scanf( %d, &num2);

Today s Learning Objectives

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

Dynamic memory allocation

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

gcc hello.c a.out Hello, world gcc -o hello hello.c hello Hello, world

Final CSE 131B Spring 2004

CSE 333 Midterm Exam July 24, 2017 Sample Solution

CS-211 Fall 2017 Test 1 Version A Oct. 2, Name:

ECE264 Fall 2013 Exam 3, November 20, 2013

UNIVERSITY OF WINDSOR Fall 2006 QUIZ # 1. Examiner:Ritu Chaturvedi Dated : Oct 3rd, Student Name: Student Number:

CSE 303 Winter 2008 Midterm Key

CMPT 102 Introduction to Scientific Computer Programming. Input and Output. Your first program

ESC101N: Fundamentals of Computing End-sem st semester

Compiling and Running a C Program in Unix

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

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

Question 2. [2 points] True False By default, structures are passed-by-reference.

Lecture 2: C Programming Basic

EC 413 Computer Organization

ME 172. Lecture 2. Data Types and Modifier 3/7/2011. variables scanf() printf() Basic data types are. Modifiers. char int float double

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

Topic 6: A Quick Intro To C

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

Intermediate Programming, Spring 2017*

LSN 3 C Concepts for OS Programming

Transcription:

CMPT 127 Spring 2019 Grade: / 20 First name: Last name: Student Number: Lab Exam 1 D400 1. [1 mark] Give an example of a sample input which would make the function scanf( "%f", &f ) return -1? Answer: EOF or CTRL + D 2. [3 marks] Circle the errors in the following program and write the correct/missing statements in order for the program to compile and execute. The program would then be executed with this command line statement:./a.out 99 4.567 #include <stdio.h> #include <stdlib.h> int main( int argc, char* argv[] ) { if ( argc!= 3 ) { printf( "I wanted 2 arguments!\n" ); else { int i = atoi( argv[1] ); float f = atof( argv[2] ); printf( "i = %n and f = %.2f.\n", i, f ); -> %d printf( "i = %n and f = %.2f.\n", i, f ); OR #include <stdio.h> #include <stdlib.h> int main( int argc, char* argv[] ) {

if ( argc!= 3 ) { printf( "I wanted 2 arguments!\n" ); else { int i = atoi( argv[1] ); float f = atof( argv[2] ); printf( "i = %n and f = %.2f.\n", i, f ); -> %d OR #include <stdio.h> #include <stdlib.h> int main( int argc, char* argv[] ) { int i; float f; if ( argc!= 3 ) { printf( "I wanted 2 arguments!\n" ); else { i = atoi( argv[1] ); f = atof( argv[2] ); printf( "i = %n and f = %.2f.\n", i, f ); -> %d 3. [2 marks] Write the compile command you would enter at the command line to compile the files test.c and stack.c such that all warning are turned on. Answer: gcc Wall test.c stack.c

CMPT 127 Spring 2019 First name: Last name: Student Number: 4. [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 used when we allocate memory space for a variable by calling malloc( )? Answer: heap 5. [2 marks] Where are the function declarations located when you write C programs? Hint: There are a few correct answers to this question and the Internet is not one of them. Answer: Some function declarations are located in the header files which are in the preprocessor include statements at the top of our C program in our code file. For example, #include <stdio.h>. Some function declarations (for the functions we implemented in our code file) are located above the function(s) (like the main function) which call(s) them. 6. [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!

CMPT 127 Spring 2019 First name: Last name: Student Number: 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 <stdlib.h> int main( void ) { int arraysize = 1024; int numofages = 0; int sum = 0; float theaverage = 0.0; int * arrayofages = getages( &numofages, arraysize ); // Add more code here free( arrayofages ); You are asked to write the function getages which must 3. Allocate an array of arraysize of integers. 4. Fill this array with ages read from standard input, i.e., integers entered at the command line, separated by one or more spaces or newline characters, until EOF is reached, and 5. Return the number of ages read via the parameter numofages. It also returns the array as the returned value, which is then assigned to arrayofages. 6. To keep the problem simple, you can assume that only integers, spaces and newline characters will be entered at the command line to test your program. This is to say that no other characters will be entered such as punctuation, letters, etc

7. On reaching EOF, the getages function returns to the main function which must then calculate the average of the ages contained in arrayofages and print to stdout the number of ages read, the sum of these ages and their average (as well as a newline character \n ) using a format that will produce the exact format exemplified in the samples outputs below: 8. Sample input 1 - Assuming the executable is called le1: $./le1 25 57 78 12 45 <- sample input # of ages: 5, sum of ages: 217, age average: 43 <- sample output $ 9. Sample input 2 - Assuming the executable is called le1: $./le1 <- sample input # of ages: 0, sum of ages: 0, age average: 0 <- sample output $ When adding code to the main function, you can only do so after the call to the getages 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 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!

CMPT 127 Spring 2019 First name: Last name: Student Number: /* Lab Exam 1 - Q7 - D400 */ #include <stdio.h> // for printf() #include <stdlib.h> // for atoi() int * getages( int * numofages, int asize ) { int result = 0; unsigned int index = 0; int d = 0; // explicitly allocate space for an array of ints int * anarray = malloc( asize * sizeof(int) ); // always check a system call for errors if( anarray == NULL ) { perror( "Allocating an array of size asize failed!" ); exit(1); *numofages = 0; // fill array read ages while ( ( result = scanf( "%d", &d ) )!= EOF ) { if ( d!= '\n' && d!= ' ' ) anarray[index++] = d; // return the number of ages read *numofages = index; return anarray; int main( void ) { int arraysize = 1024; int numofages = 0; int sum = 0; float theaverage = 0.0; int * arrayofages = getages( &numofages, arraysize ); // running sum for (unsigned int i = 0; i < numofages; i++) { sum += arrayofages[i];

// if at least one age has been read, compute average if ( numofages > 0 ) theaverage = sum / numofages; // print stats printf( "# of ages: %d, sum of ages: %d, age average: %.0f\n", numofages, sum, theaverage ); // de-allocate the memory allocated by malloc free( arrayofages );