CpSc 1111 Lab 9 2-D Arrays

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

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

CpSc 1111 Lab 5 Formatting and Flow Control

CpSc 111 Lab 5 Conditional Statements, Loops, the Math Library, and Redirecting Input

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

CpSc 1011 Lab 4 Formatting and Flow Control Windchill Temps

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

CpSc 1111 Lab 4 Formatting and Flow Control

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

CpSc 1111 Lab 1 Introduction to Unix Systems, Editors, and C

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

CS 356, Fall 2018 Data Lab (Part 2): Manipulating Bits Due: Mon, Sep. 17, 11:59PM

Physics 2660: Fundamentals of Scientific Computing. Lecture 3 Instructor: Prof. Chris Neu

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

CpSc 101, Fall 2015 Lab7: Image File Creation

CS 356, Fall 2018 Data Lab (Part 1): Manipulating Bits Due: Wednesday, Sep. 5, 11:59PM

CS : Programming for Non-majors, Fall 2018 Programming Project #2: Census Due by 10:20am Wednesday September

General Instructions. You can use QtSpim simulator to work on these assignments.

Lecture 10. Daily Puzzle

COP4530 Data Structures, Algorithms and Generic Programming Recitation 3 Date: January 20 & 22, 2009

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #03 The Programming Cycle

COMP26120 Academic Session: Lab Exercise 2: Input/Output; Strings and Program Parameters; Error Handling

Copy: IF THE PROGRAM or OUTPUT is Copied, then both will have grade zero.

CS : Programming for Non-Majors, Fall 2018 Programming Project #5: Big Statistics Due by 10:20am Wednesday November

Spring 2017 CMSC 140 Programming Project 7: Payroll

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

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

Computer Science 322 Operating Systems Mount Holyoke College Spring Topic Notes: C and Unix Overview

How to declare an array in C?

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

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop

COMP s1 Lecture 1

CS 135, Fall 2010 Project 4: Code Optimization Assigned: November 30th, 2010 Due: December 12,, 2010, 12noon

15213 Recitation Section C

Regis University CC&IS CS362 Data Structures

An array is a collection of data that holds fixed number of values of same type. It is also known as a set. An array is a data type.

Computer Programming: C++

ASSIGNMENT TWO: PHONE BOOK

CS 2505 Fall 2013 Data Lab: Manipulating Bits Assigned: November 20 Due: Friday December 13, 11:59PM Ends: Friday December 13, 11:59PM

CMSC 201 Spring 2018 Project 3 Minesweeper

9/10/2016. Time for Some Detailed Examples. ECE 120: Introduction to Computing. Let s See How This Loop Works. One Statement/Step at a Time

Computer Science & Engineering 150A Problem Solving Using Computers

Data Lab: Manipulating Bits

Introduction: The Unix shell and C programming

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

CS : Programming for Non-majors, Summer 2007 Programming Project #2: Census Due by 12:00pm (noon) Wednesday June

CprE 381 Computer Organization and Assembly Level Programming

ECEn 424 Data Lab: Manipulating Bits

15-110: Principles of Computing, Spring 2018

CS 2505 Fall 2018 Data Lab: Data and Bitwise Operations Assigned: November 1 Due: Friday November 30, 23:59 Ends: Friday November 30, 23:59

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #17. Loops: Break Statement

Laboratory 1 Semester 1 11/12

UNIVERSITY OF CALIFORNIA, SANTA CRUZ BOARD OF STUDIES IN COMPUTER ENGINEERING

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #13. Loops: Do - While

Programming Studio #9 ECE 190

Lab 1 Introduction to UNIX and C

Variables, Data Types, and Arithmetic Expressions Learning Objectives:

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

CS 051 Homework Laboratory #2

Programming in Java Prof. Debasis Samanta Department of Computer Science Engineering Indian Institute of Technology, Kharagpur

Formatting & Style Examples

CpSc 1011 Lab 11 Pointers

CGS 3460 Summer 07 Midterm Exam

Lab 9: Linking, I/O, and Caches

COMP combinational logic 1 Jan. 18, 2016

ECE15: Lab #4. Problem 1. University of California San Diego

Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring Topic Notes: C and Unix Overview

15-213, Spring 2008 Lab Assignment L1: Manipulating Bits Assigned: Jan. 15, Due: Wed., Jan. 30, 11:59PM

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

PA101 Learning the Ropes

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

CS 211 Programming Practicum Fall 2018

gcc o driver std=c99 -Wall driver.c everynth.c

Prerequisites: Read all chapters through Chapter 4 in the textbook before attempting this lab. Read through this entire assignment before you begin.

CMPSCI 187 / Spring 2015 Sorting Kata

CIS* Programming

Outline for Today. Lab Equipment & Procedures. Teaching Assistants. Announcements

INTRODUCTION TO C++ PROGRAM CONTROL. Dept. of Electronic Engineering, NCHU. Original slides are from

CP Lab 5: Functions, pointers, some arrays

CP Lab 5: Functions, pointers, some arrays

CS 101, Spring 2016 March 22nd Exam 2

Binghamton University. CS-220 Spring Includes & Streams

Warm-up sheet: Programming in C

Using C++, design an Abstract Data Type class named MyGrades. The class must have the following private members :

CS4023 Week06 Lab Exercise

CPSC 427: Object-Oriented Programming

Repetition Structures

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

Due: 9 February 2017 at 1159pm (2359, Pacific Standard Time)

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

Pointer Casts and Data Accesses

COMP Assignment 1

Computer Programming: Skills & Concepts (CP) arithmetic, if and booleans (cont)

CS 1803 Pair Homework 3 Calculator Pair Fun Due: Wednesday, September 15th, before 6 PM Out of 100 points

CIS*1500 Introduction to Programming

Numerical Computing in C and C++ Jamie Griffin. Semester A 2017 Lecture 2

CSSE2002/7023 The University of Queensland

XMT-HW1: Matrix-Vector Multiplication

Programming Assignment #4 Arrays and Pointers

CpSc 1010, Fall 2014 Lab 10: Command-Line Parameters (Week of 10/27/2014)

Transcription:

CpSc 1111 Lab 9 2-D Arrays Overview This week, you will gain some experience with 2-dimensional arrays, using loops to do the following: initialize a 2-D array with data from an input file print out the values in an array compute averages for each row of the 2-D array compute averages for each column of the 2-D array sort all the values of the 2-D array Background Information In last week s lab, you wrote a program that computed the inner product of two 1-dimensional arrays. This week, you will be storing data from a file (grades of 7 labs for 6 students) into a 2-dimensional array. 2-dimensional arrays are often used for tables of data. They can be visualized as a matrix consisting of rows and columns. Declaration of a 2-D array is similar to a 1-D array except that an additional number is provided in brackets representing the size of the second dimension. The first size represents the number of rows and the second size represents the number of columns. You also used loops in last week s lab to iterate through arrays to perform various tasks. You will use loops again this week to initialize the array, print the values, traverse through the array to do calculations and store values into new 1- dimensional arrays, and to sort the values of the array. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Sorting Arrays There are many different sorting algorithms that may be used to sort values in an array. You may recall the Selection Sort as one of the sorts from the slides that was covered in lecture. It determines the location of the minimum of the list and swaps it with the element at the index where it is supposed to be. 1

Lab Assignment For this week s lab assignment, you will write a program, called lab9.c. Reminder About Formatting and Comments The top of your file should have a header comment, which should contain: o Your name o Course and semester o Lab number o Brief description about what the program does o Any other helpful information that you think would be good to have. Variables should be declared at the top of the main function, and should have meaningful names. Always indent your code in a readable way. Some formatting examples may be found here: https://people.cs.clemson.edu/~chochri/assignments/formatting_examples.pdf Don t forget to use the Wall flag when compiling, for example: gcc Wall lab9.c In your program, perform the following steps. (NOTE: use for loops for all the loops in your program.) 1. Once you log in to your account, navigate to a directory where you will do this week s lab work. Then type the following command: cp /group/course/cpsc111/public_html/s18labs/lab9/input.txt. Don t forget the dot at the end this says to copy file called input.txt from that specified path into your current working directory. 2. Declare a 2-D array called grades. It should be 6 rows X 7 columns. Create a for loop to read in the values using scanf(). When you run your program, you will redirect the input from the file called input.txt so you won t need to prompt the user to enter values. 3. Create a for loop to print the values of the grades array. (compile and run) Your output should look like the following: 80 90 70 100 60 90 85 78 93 80 70 98 98 85 100 99 89 90 72 0 78 98 100 65 67 11 28 89 85 90 98 85 87 56 69 22 85 72 95 75 64 88 To get the values to line up as shown above, format the integer output with a size of 4 in the format string of the printf() statement. 2

4. Each row of the table is a student. Add an additional printf() statement to the for loop from step 3 so that your output looks like the following: (compile and run) 5. Use another for loop to compute the average for each student. Put the averages for each student (each row) into a new 1-D array called stuavg. Then write another loop to print the averages from this 1-D array out. (compile and run) Your output now should look like the following: (NOTE: the averages are floating point values (or doubles) and are printed to 2 decimal places.) 82.14 86.00 75.43 65.43 81.43 71.57 6. Add an additional printf() statement to the for loop from step 5 so that your output for the averages indicates which student, like the following: (compile and run) 3

7. Each grade in each column can be thought of as a lab grade. Use another for loop to compute the average for each lab (or column). Put those averages into a new 1-D array called labavg. Then write another loop to print the averages from this 1-D array out. (compile and run) Adding the additional printf() for each lab average indicating which lab, your output now should look like the following: Lab 1 average: 77.17 Lab 2 average: 92.83 Lab 3 average: 79.00 Lab 4 average: 84.50 Lab 5 average: 67.17 Lab 6 average: 56.00 Lab 7 average: 82.33 8. Use the Selection Sort shown on page 1 of the lab write-up to sort the values in the grades array. One suggested way of doing this is to copy the values from the 2-D array to a 1-D array called sortedgrades, and then sort that 1-D array. Then print the values in that sorted array. (compile and run) Your output for the entire program should now look like the following: Lab 1 average: 77.17 Lab 2 average: 92.83 Lab 3 average: 79.00 Lab 4 average: 84.50 Lab 5 average: 67.17 Lab 6 average: 56.00 Lab 7 average: 82.33 0 11 22 28 56 60 64 65 67 69 70 70 72 72 75 78 78 80 80 85 85 85 85 85 87 88 89 89 90 90 90 90 93 95 98 98 98 98 99 100 100 100 4

Turn In Work 1. Before turning in your assignment, make sure you have followed all of the instructions stated in this assignment and any additional instructions given by your lab instructor(s). Always test, test, and retest that your program compiles and runs successfully on our Unix machines before submitting it. 2. Show your TA that you completed the assignment. Then submit your lab9.c program using the handin page: http://handin.cs.clemson.edu. Don t forget to always check on the handin page that your submission worked. You can go to your bucket to see what is there. Grading Rubric For this lab, points will be based on the following: Functionality 75 should have the following functionality in loops: loop(s) to initialize grades array loop(s) to print grades array loop(s) to compute student averages and put into stuavg array loop to print stuavg values loop(s) to compute lab averages and put into labavg array loop to print labavg values loop(s) to copy 2-D grades array into 1-D sortedgrades array loop(s) to sort sortedgrades array loop to print sortedgrades array Code formatting 10 No warnings when compile 5 including forgetting return statement at end of main Uses a 2-D array 5 Uses scanf() to initialize 2-D array 5 redirecting input from the input.txt file NOTE: There could be other possible point deductions for things not listed, such as global variables, use of break not in switch statements, naming the file incorrectly, etc. 5