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

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

Pointer Casts and Data Accesses

struct _Rational { int64_t Top; // numerator int64_t Bottom; // denominator }; typedef struct _Rational Rational;

You will provide an implementation for a test driver and for a C function that satisfies the conditions stated in the header comment:

Here is a C function that will print a selected block of bytes from such a memory block, using an array-based view of the necessary logic:

// Initially NULL, points to the dynamically allocated array of bytes. uint8_t *data;

For this assignment, you will implement a collection of C functions to support a classic data encoding scheme.

Both parts center on the concept of a "mesa", and make use of the following data type:

The assignment requires solving a matrix access problem using only pointers to access the array elements, and introduces the use of struct data types.

A rectangle in the xy-plane, whose sides are parallel to the coordinate axes can be fully specified by giving the coordinates of two opposite corners:

Each line will contain a string ("even" or "odd"), followed by one or more spaces, followed by a nonnegative integer.

Creating a String Data Type in C

Given that much information about two such rectangles, it is possible to determine whether they intersect.

Here is a C function that will print a selected block of bytes from such a memory block, using an array-based view of the necessary logic:

A Capacity: 10 Usage: 4 Data:

CS 1044 Program 6 Summer I dimension ??????

Pointer Accesses to Memory and Bitwise Manipulation

CS 3114 Data Structures and Algorithms DRAFT Project 2: BST Generic

Pointer Accesses to Memory and Bitwise Manipulation

Simple C Dynamic Data Structure

Decision Logic: if, if else, switch, Boolean conditions and variables

Pointer Accesses to Memory and Bitwise Manipulation

CS 1044 Program 2 Spring 2002

Accessing Data in Memory

CS 2604 Minor Project 1 Summer 2000

CS 2604 Minor Project 1 DRAFT Fall 2000

For storage efficiency, longitude and latitude values are often represented in DMS format. For McBryde Hall:

CS 1044 Project 2 Spring 2003

CS 1044 Project 1 Fall 2011

PR quadtree. public class prquadtree< T extends Compare2D<? super T> > {

The Program Specification:

CS 2505 Computer Organization I Test 2. Do not start the test until instructed to do so! printed

Do not start the test until instructed to do so!

struct Properties C struct Types

Invoice Program with Arrays and Structures

CS 142 Style Guide Grading and Details

For questions 4 through 7, select the value assigned to the relevant variable, given the declarations: 3) ) This is not allowed

Programming in C++ 5. Integral data types

CS 2704 Project 1 Spring 2001

Submit your answers to these questions to the Curator under Quizzes as HW08 by the posted due date and time. No late submissions will be accepted.

CS2304 Spring 2014 Project 3

Fundamental Concepts: array of structures, string objects, searching and sorting. Static Inventory Maintenance Program

CS 2505 Computer Organization I Test 1. Do not start the test until instructed to do so!

CS 2505 Computer Organization I

CS 261 Fall Mike Lam, Professor Integer Encodings

Unit 3. Operators. School of Science and Technology INTRODUCTION

CS 2604 Minor Project 3 Movie Recommender System Fall Braveheart Braveheart. The Patriot

CS 3114 Data Structures and Algorithms DRAFT Minor Project 3: PR Quadtree

Arithmetic and Bitwise Operations on Binary Data

CS 2505 Computer Organization I

Binary Adders: Half Adders and Full Adders

Do not start the test until instructed to do so!

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Introduction to Computer Science Midterm 3 Fall, Points

CSC 210, Exam Two Section February 1999

File Navigation and Text Parsing in Java

A complement number system is used to represent positive and negative integers. A complement number system is based on a fixed length representation

Spring 2002 Page 1 of 8 READ THIS NOW!

Arithmetic and Bitwise Operations on Binary Data

ISA 563 : Fundamentals of Systems Programming

CS 2604 Minor Project 3 DRAFT Summer 2000

CS 2704 Project 3 Spring 2000

int32_t Buffer[BUFFSZ] = {-1, -1, -1, 1, -1, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, -1, -1, -1, -1, -1}; int32_t* A = &Buffer[5];

Lecture 3 Tao Wang 1

ICS Instructor: Aleksandar Kuzmanovic TA: Ionut Trestian Recitation 2

Basic Operations jgrasp debugger Writing Programs & Checkstyle

Do not start the test until instructed to do so!

Carnegie Mellon. Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition

a f b e c d Figure 1 Figure 2 Figure 3

Homework 2 - KW26 - using 40 hexadecimal digits

CS 2505 Computer Organization I Test 2. Do not start the test until instructed to do so! printed

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

COS 471A,COS 471B/ELE 375 Midterm

Unit-II Programming and Problem Solving (BE1/4 CSE-2)

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

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

Intro to C and Binary Numbers 8/27/2007

Bits, Bytes and Integers Part 1

Data III & Integers I

Question: Total Points: Score:

READ THIS NOW! Failure to read and follow the instructions below may result in severe penalties. Do not start the test until instructed to do so!

CS 1044 Project 5 Fall 2009

Chapter 3: Operators, Expressions and Type Conversion

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

CSE 131S Introduction to Computer Science Summer SON Exam I

Problem Score Max Score 1 Syntax directed translation & type

Jim Lambers ENERGY 211 / CME 211 Autumn Quarter Programming Project 4

Do not start the test until instructed to do so!

ECE264 Fall 2013 Exam 1, September 24, 2013

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

EDIABAS BEST/2 LANGUAGE DESCRIPTION. VERSION 6b. Electronic Diagnostic Basic System EDIABAS - BEST/2 LANGUAGE DESCRIPTION

CSCI 136 Data Structures & Advanced Programming. Fall 2018 Instructors Bill Lenhart & Bill Jannen

CSCE 206: Structured Programming in C++

Programming Standards: You must conform to good programming/documentation standards. Some specifics:

Writing Program in C Expressions and Control Structures (Selection Statements and Loops)

Part I Part 1 Expressions

DEPARTMENT OF MATHS, MJ COLLEGE

CS 33. Data Representation, Part 1. CS33 Intro to Computer Systems VII 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Number Systems. Binary Numbers. Appendix. Decimal notation represents numbers as powers of 10, for example

CS 111X - Fall Test 1

Transcription:

C Programming Simple Array Processing This assignment consists of two parts. The first part focuses on array read accesses and computational logic. The second part focuses on array read/write access and a bit of integer data representation. Q1. For this question, you will implement the following C function: / Given an array of nonnegative integers, a mesa is a sequence of 5 or more identical values. The length of a mesa is the number of values in the sequence, and the mass of a mesa is the sum of the values in the sequence. bigmesa() determines the mesa of maximum mass in the array that is passed into it. bigmesa() reports its results by setting three extern variables: mesastart, mesaend and mesamass. Pre: A[0:A_Size - 1] are nonnegative integers. Post: mesastart = index of first element of most massive mesa mesaend = index of last element of most massive mesa mesamass = sum of elements in most massive mesa Restrictions: You may not use a second array. / void bigmesa(int A[], int A_Size); Begin by downloading the posted files: driver.c bigmesa.h bigmesa.c sample test driver; modify as you like C header file for compiling with test driver --- do not modify! C source file for implementation of bigmesa() function You can compile these files by using the following command in a Linux shell: gcc o driver std=c99 -Wall driver.c bigmesa.c Using this command with the supplied files will yield an executable named driver. You can execute the testing code by using the following command in a Linux shell:./driver (If you've added the current directory to your Linux path, you can omit the "./" from the command.) Although the given code will compile correctly, the resulting program will not satisfy the requirements of the assignment, since the supplied implementation of the required function doesn't do anything. So, you must correctly complete the implementation of the function. You may, and should, modify the supplied testing code since it's not exactly thorough. But, if your solutions don't compile with the supplied testing code, they won't compile with the real testing code either. It's not likely, but you may need to add include directives to your.c file, as needed for any C Standard Library features you use. You may write secondary "helper" functions if you like; if so, those must be defined and declared within the supplied bigmesa.c file. Such functions should be declared as static. 1

Here are some samples of test data and correct results. The first row shows array indices, the second shows the array contents, and the values that should be set by bigmesa() are shown after that. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 33 28 28 96 96 96 96 96 77 77 77 77 77 77 77 77 77 77 77 77 31 31 31 27 74 56 56 56 51 51 mesastart: 8 mesaend: 19 mesamass: 924 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 91 91 91 91 15 15 15 15 15 15 44 44 44 1 1 1 1 1 1 1 1 1 1 1 1 1 1 mesastart: 4 mesaend: 9 mesamass: 90 Do not consider the test data above to be comprehensive, but it does illustrate some of the logic issues that arise in this assignment. What to Submit You will submit your modified version of the file bigmesa.c to the Curator, via the collection point HW05Q1. This assignment will be graded automatically. You will be allowed up to ten submissions for this assignment, so use them wisely. Test your programs thoroughly before submitting them. Make sure that your programs produce correct results for every logically valid test case you can think of. Do not waste submissions on untested code, or on code that does not compile with the supplied code from the course website. The Curator will assign a score based on runtime testing of your submission; your best score will be counted; the TAs will later verify that your best submission meets the stated restrictions, and assess penalties if not. The Student Guide and other pertinent information, such as the link to the proper submit page, can be found at: http:www.cs.vt.edu/curator/ 2

Q2. For this question, you will implement part of a C library to emulate basic hardware operations on signed 32-bit integer values. You should begin by reviewing your notes on integer data representation. For this question, we will use 32-element arrays of uint8_t values to represent 32-bit signed integers: integer: 84193 binary representation: 0000 0000 0000 0001 0100 1000 1110 0001 BinInt[]: 1 0 0 0 0 1 1 1 0 0 0 1 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Note that we represent the bits of the integer in the array so that the 2 k bit is stored at index k in the array. That simplifies some coding. For this question, we will only emulate three basic operations: addition, negation and subtraction. You will also write a binary to decimal conversion function. You will find a C header file on the course website containing the following function declarations: / Initializes BinInt[] so that the elements correspond to the 2's complement representation of the 32-bit integer Src. Pre: BinInt[] is of dimension 32 Post: For i = 0 to 31, BinInt[i] == 2^i bit of Src / void BI_Create(uint8_t BinInt[], int32_t Src); / Converts a BinInt[] to its decimal representation. Pre: Num[] is of dimension 32 Num[] stores a 2's complement representation integer Ret: The decimal value. / int32_t BI_ToDecimal(const uint8_t Num[]); / Computes sum of 2's complement representations of integer values. Pre: Sum[], Left[] and Right[] are of dimension 32 Left[] and Right[] store 2's complement representations Post: Sum[] == Left[] + Right[], if possible Ret: false if overflow occurs when computing sum of Left[] and Right[]; true otherwise / bool BI_Add(uint8_t Sum[], const uint8_t Left[], const uint8_t Right[]); / Computes difference of 2's complement representations of integer values. Pre: Diff[], Left[] and Right[] are of dimension 32 Left[] and Right[] store 2's complement representations Post: Diff[] == Left[] - Right[], if possible Ret: false if overflow occurs when computing difference of Left[] and Right[]; true otherwise / bool BI_Sub(uint8_t Diff[], const uint8_t Left[], const uint8_t Right[]); 3

/ Computes negation of 2's complement representation of integer value. Pre: Neg[] and Right[] are of dimension 32 Right[] stores a 2's complement representation Post: Neg[] = -Right[] Ret: false if negation cannot be correctly represented; true otherwise / bool BI_Neg(uint8_t Neg[], const uint8_t Right[]); / Prints 2's complement representation, with formatting. Pre: fp is open on an output stream BinInt[] is of dimension 32 and stores a 2's complement representation prefix and suffix are each NULL or point to a C-string Post: the bits represented in BinInt[] have been written, preceded by prefix (if not NULL) and followed by suffix (if not NULL) / void BI_fprintf(FILE fp, const uint8_t BinInt[], char prefix, char suffix); Pay close attention to the given pre- and post-conditions and the specified return values. You will also find a C source file containing trivial bodies for the four functions you need to write. You should download the header and source files and use them as your starting point. There is one important restriction on your implementations: you must not convert a BinInt[] to a C type, like int32_t, or then use the native C arithmetic operations on those values, with the exception of the BI_ToDecimal() function. You may, of course, use the native C arithmetic operations on the bit representations in a BinInt[]. The C source file also contains implementations for the first and last functions whose declarations are shown above; those functions require the use of some bitwise operations we are about to introduce. You do not have to use bitwise operations for the other functions, but you should examine the way they are used in the given functions. What to Submit You will submit your solution in a single.c file to the Curator, via the collection point HW05Q2. You may use any valid Linux file name. This assignment will be graded automatically. You will be allowed up to ten submissions for this assignment, so use them wisely. Test your programs thoroughly before submitting them. Make sure that your programs produce correct results for every logically valid test case you can think of. Do not waste submissions on untested code, or on code that does not compile with the supplied code from the course website. The Curator will assign a score based on runtime testing of your submission; your best score will be counted; the TAs will later verify that your best submission meets the stated restrictions, and assess penalties if not. The Student Guide and other pertinent information, such as the link to the proper submit page, can be found on the course website. 4

Pledge: Each of your program submissions must be pledged to conform to the Honor Code requirements for this course. Specifically, you must include the following pledge statement in the submitted file: On my honor: - I have not discussed the C language code in my program with anyone other than my instructor or the teaching assistants assigned to this course. - I have not used C language code obtained from another student, or any other unauthorized source, either modified or unmodified. - If any C language code or documentation used in my program was obtained from an allowed source, such as a text book or course notes, that has been clearly noted with a proper citation in the comments of my program. <Student Name> Failure to include this pledge in a submission will result in the submission being disallowed during code review. An Example of Simple Binary Addition The simple way to perform addition is to add corresponding bits and propagate carries: carry bits: 1111111111111111111111111111111111111111111111111110011111111110 7321598: 0000000000000000000000000000000000000000011011111011011111111110 + -4332805: 1111111111111111111111111111111111111111101111011110001011111011 ------------------------------------------------------------------------------- 0000000000000000000000000000000000000000001011011001101011111001 You will have to detect the occurrence of overflow. For our purposes, it's sufficient to define overflow as the condition that the mathematically-correct result of an operation cannot be represented correctly using the allowed number of bits (and so, the value that is computed is incorrect). See the course notes for additional information. 5