CS261: HOMEWORK 2 Due 04/13/2012, at 2pm

Similar documents
SPRING 2017 CSCI 304 LAB1 (Due on Feb-14, 11:59:59pm)

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

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

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

CS201- Introduction to Programming Current Quizzes

Data Structure and Algorithm Homework #3 Due: 2:20pm, Tuesday, April 9, 2013 TA === Homework submission instructions ===

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

Course Text. Course Description. Course Objectives. StraighterLine Introduction to Programming in C++

CpSc 1111 Lab 9 2-D Arrays

Student Number: Computer Science 211b Final Examination. 28 April hours

Arithmetic and IO. 25 August 2017

CSE 5A Introduction to Programming I (C) Homework 4

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

Euclid s algorithm, 133

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

CS164: Programming Assignment 5 Decaf Semantic Analysis and Code Generation

EECE.2160: ECE Application Programming

Introduction to Computer Science Midterm 3 Fall, Points

Programming Tips for CS758/858

Project 5 Handling Bit Arrays and Pointers in C

Functions. Arash Rafiey. September 26, 2017

ECE15: Homework 10. void wordstats(file *fp, char string[]) { void printfrequencies(file *fp) {

CS61C Machine Structures. Lecture 3 Introduction to the C Programming Language. 1/23/2006 John Wawrzynek. www-inst.eecs.berkeley.

EECE.2160: ECE Application Programming

CS201 Some Important Definitions

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

1. Introduction. 2. Deliverables

mith College Computer Science CSC270 Spring 2016 Circuits and Systems Lecture Notes, Week 11 Dominique Thiébaut

COMP 321: Introduction to Computer Systems

Programming Assignment #4

Lecture 04 Introduction to pointers

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Introduction to Programming II Winter, 2015 Assignment 5 (Saturday, April 4, 2015: 23:59:59)

PERIYAR CENTENARY POLYTECHNIC COLLEGE Periyar Nagar- Vallam Thanjavur

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

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

CMSC 412 Project 1: Keyboard and Screen Drivers

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

Contents. A Review of C language. Visual C Visual C++ 6.0

! Determine if a number is odd or even. ! Determine if a number/character is in a range. ! Assign a category based on ranges (wind speed)

CS 610: Intermediate Programming: C/C++ Making Programs General An Introduction to Linked Lists

Project#3 [40 points] COP 2220 Fall 2017

ENGR 3950U / CSCI 3020U (Operating Systems) Simulated UNIX File System Project Instructor: Dr. Kamran Sartipi

CpSc 1111 Lab 5 Formatting and Flow Control

LESSON 4. The DATA TYPE char

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Mid-term Exam. Fall Semester 2017 KAIST EE209 Programming Structures for Electrical Engineering. Name: Student ID:

CpSc 1011 Lab 3 Integer Variables, Mathematical Operations, & Redirection

C mini reference. 5 Binary numbers 12

Self-referential Structures and Linked List. Programming and Data Structure 1

CS 470 Operating Systems Spring 2013 Shell Project

Mid-term Exam. Fall Semester 2017 KAIST EE209 Programming Structures for Electrical Engineering. Name: Student ID:

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).

Review: C Strings. A string in C is just an array of characters. Lecture #4 C Strings, Arrays, & Malloc

Slide Set 3. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

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

Programming in C - Part 2

CS61C : Machine Structures

Review! * follows a pointer to its value! & gets the address of a variable! Pearce, Summer 2010 UCB! ! int x = 1000; Pearce, Summer 2010 UCB!

1. Introduction. 2. Project Submission and Deliverables

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

CS 202, Fall 2017 Homework #4 Balanced Search Trees and Hashing Due Date: December 18, 2017

CS61C : Machine Structures

today cs3157-fall2002-sklar-lect05 1

Data Structure and Algorithm Homework #2 Due: 2:00pm, Thursday, March 29, 2012 TA

Makefiles Makefiles should begin with a comment section of the following form and with the following information filled in:

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

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

CS 142 Style Guide Grading and Details

CS2351 Data Structures. Lecture 7: A Brief Review of Pointers in C

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

CpSc 1011 Lab 5 Conditional Statements, Loops, ASCII code, and Redirecting Input Characters and Hurricanes

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

Q1: /8 Q2: /30 Q3: /30 Q4: /32. Total: /100

CS2223: Algorithms D- Term, Homework I. Teams: To be done individually. Due date: 03/27/2015 (1:50 PM) Submission: Electronic submission only

Computer Programming C++ (wg) CCOs

CS 361 Computer Systems Fall 2017 Homework Assignment 1 Linking - From Source Code to Executable Binary

CpSc 1111 Lab 6 Conditional Statements, Loops, the Math Library, and Random Numbers What s the Point?

Introduction to C. Ayush Dubey. Cornell CS 4411, August 31, Geared toward programmers

Homework 1 - Extracting Data from a CSV file

Input/Output Week 5:Lesson 16.1

CGS 3460 Summer 07 Midterm Exam

CS61C : Machine Structures

Computer Science & Engineering 150A Problem Solving Using Computers

CSCI 2132 Final Exam Solutions

It is academic misconduct to share your work with others in any form including posting it on publicly accessible web sites, such as GitHub.

ECEN 449 Microprocessor System Design. Review of C Programming. Texas A&M University

EECE.2160: ECE Application Programming Spring 2017

CSCI 2132 Final Exam Solutions

CSCI 2132 Software Development. Lecture 29: Dynamic Memory Allocation

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

Compiling and Running a C Program in Unix

Your submitted proj3.c must compile and run on linprog as in the following example:

Out: April 19, 2017 Due: April 26, 2017 (Wednesday, Reading/Study Day, no late work accepted after Friday)

C-LANGUAGE CURRICULAM

Technical Questions. Q 1) What are the key features in C programming language?

PDS Class Test 2. Room Sections No of students

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

C Syntax Arrays and Loops Math Strings Structures Pointers File I/O. Final Review CS Prof. Jonathan Ventura. Prof. Jonathan Ventura Final Review

Transcription:

CS261: HOMEWORK 2 Due 04/13/2012, at 2pm Submit six *.c files via the TEACH website: https://secure.engr.oregonstate.edu:8000/teach.php?type=want_auth 1. Introduction The purpose of HW2 is to help you get started with programming in C, and give you practice with pointer manipulation. If you have any questions regarding HW2, please email cs261-sp12@engr.orst.edu. This assignment consists of a series of 6 small programs, and will be graded for a total of 100 points. The points for each program are indicated at the beginning of the corresponding section in this writeup. Each program should be implemented in a separate.c file (see Section 9). The skeleton code of these 6 programs is already provided to you in 6 hand-out.c files on the class website. In the hand-out files, specific requirements are inserted as comments to explain what you need to do for each function. Download these 6 files of the skeleton code from http://classes.engr.oregonstate.edu/eecs/spring2012/cs261/cs261homework.html and use them as the starting point for HW2. We have also prepared a makefile for compiling your programs. The file can be downloaded from http://classes.engr.oregonstate.edu/eecs/spring2012/cs261/cs261homework.html Make sure that your programs compile well using the provided makefile, i.e., gcc on Unix. You may test your compiling on flop.engr.oregonstate.edu. 2. General Instructions The function prototypes, the function names, and the names of variables in the hand-out files are to be taken as is. Do not modify them. Any modification will slow down, and complicate the TA s job to grade your homework. Strictly follow the input and output formats specified for each program. Do not include getchar() statements, or extraneous printf() statements in your final submission. Unless otherwise specified, all input/output should be accomplished using only scanf()/printf() functions, respectively. Your program should exit once the output is printed. Comment your code following the guidelines given in the overview slides from the very first lecture. Note that points are also allocated for your coding style, clarity of comments, variable naming, indentation, etc.

3. Program1.c (12 points) Write Program1.c to do the following: In the main function, declare an integer, x. Print the memory address of x (using the address of operator). Pass x as an argument to a function void fooa(int* iptr). In fooa(int * iptr), print the value of the integer pointed to by iptr, the address pointed to by iptr, and the address of iptr itself. In the main function, following the call to fooa(), print the value of x. 1) Address of x (2pts) 2) Value of what iptr points to (2 pts) 3) Address pointed to by iptr (3pts) 4) Address of iptr itself (3 pts) 5) Your coding style (2pts) 4. Program2.c (12 points) Write Program2.c with the following: A function int foo(int* a, int *b, int c) which should perform the following computations 1) Increment a. 2) Decrement b. 3) Assign a - b to c. 4) Return the value of c. In the main function, declare three integers x, y, and z, and assign them random integer values in the interval [0, 10]. You may use the C math library random number generator rand() to generate random numbers. Make sure that your use of rand() correctly generates nonnegative integers less than 11. Print the values of x, y, and z. Call foo() appropriately passing x, y, and z as arguments. Print out the values of x, y, and z after calling the function foo(). Also, print the value returned by foo(). In the main function, answer the following question in the comment at the bottom of the file: Is the return value of foo() different from the value of z? Why? 1) Printing x, y, and z before the call to foo() (2 pts) 2) Printing x, y, and z after the call to foo() (2 pts) 3) Return value of foo() (2 pts) 4) Comparison of the return value of foo() and z (4 pts) 5) Your coding style (2pts) 2 / 5

5. Program3.c (19 points) Write Program3.c in which you will consider the following structure: struct student{ char initials[2]; int score; }; and the declaration in the main function: struct student *st = 0; Implement the following functions and demonstrate their functionality by calling them (in the order given) from the main function: struct student* allocate() that allocates memory for 10 students, and returns the pointer. void generate(struct student* students) that generates random initials and scores for each of the 10 students, and stores them in the array students. You may use the C math library random number generator rand() to generate random numbers. To generate the two initial letters of a student you may use the following command: char c1, c2; c1 = rand()%26 + A ; c2 = rand()%26 + A ; where % sign indicate the modulo operation, so that the student s initial letters take values in the English alphabet from A to Z (only capital letters), i.e., the value of (rand()%26) is an integer in the interval between 0 and 25 (both inclusive). Ensure that the score is between 0 and 100 (both inclusive). void output(struct student* students) that prints the initials and scores of all the students. void summary(struct student* students) that prints the minimum score, maximum score and average score of the 10 students. void deallocate(struct student* stud) that frees the memory allocated to students. Check that students is not NULL, that is ==0, before you attempt to free it. 1) Allocate (3 pts) 2) Generate (5 pts) 3) Output (3 pts) 4) Summary (3 pts) 5) Deallocate (3 pts) 6) Your coding style (2pts) 3 / 5

6. Program4.c (19 points) Write a function void sort(int* numbers, int n) to sort a given array of n integers in the ascending order. In the main function, declare an integer n, and assign a value of 20 to it. Allocate memory for an array of n integers using malloc. Fill this array with random numbers, using the C math library random number generator rand(). Since rand() returns a random integer, you will need a for loop in which you will call rand() and set the value for each element of the array. Print the contents of the array. Pass this array along with n to the sort() function. Print the contents of the sorted array following the call to sort(). 1) Creation of the array of random numbers (5 pts) 2) Correctly sorted array of numbers in the ascending order (12 pts) 3) Your coding style (2pts) 7. Program5.c (19 points) Consider the structure student in Program3.c. Modify the sort() function from Program4.c to sort an array of n students based on their first initial. If two students have the same first initial, you may optionally want to compare their second initial. This option will bring you additional points only if you lose some points on the other mandatory tasks of HW2. The function prototype is void sort(struct student* students, int n). As in Program2.c, initials and scores of the students are to be generated randomly by rand() as in Program3.c. 1) Sorts array of student structures correctly (17 pts) 2) Checks if two students have the same first initial, and compares their second initial (optional 8pts) 3) Your coding style (2pts) 4 / 5

8. Program6.c (19 points) In the main function, Program6.c is supposed to read in two variables from the console using the function scanf(). The first variable is a character array word consisting of only letters from the English alphabet, such as America. The second variable is Boolean flag that indicates whether word needs to be converted to lower case, flag==0, or upper case, flag==1. Write two functions void lowercase(char* word) and void uppercase(char* word). lowercase() modifies the input word to have all lower case letters. uppercase() modifies the input word to have all upper case letters. For example, the output of lowercase() for America is america, and for uppercase() is AMERICA. Watch out for the end of the string, which is denoted by \0. Call in the main function lowercase() or uppercase() depending on the user input. NOTE1: You can use the touppercase() and tolowercase() functions provided in the skeletal code to change the case of a character. Notice that touppercase() assumes that the input character is currently in lower case. Therefore, you would have to check the case of a character before calling touppercase(). Similarly applies for tolowercase(). NOTE2: Make sure that you can handle the case when the user has not provided the input word. 1) Properly inputs the word and flag (3 pts) 2) Properly converts by lowercase() any input word to lower case letters (7 pts) 3) Properly converts by uppercase() any input word to upper case letters (7 pts) 4) Your coding style (2pts) 9. What to turn in You will turn in six files Program1.c, Program2.c, Program3.c, Program4.c, Program5.c, and Program6.c via TEACH. Use the provided makefile to compile on flop.engr.oregonstate.edu. 5 / 5