Homework 1 - First Steps

In this homework we set up the class environment, and practice some C programming. There are many things to do in this homework, and some things can take a lot of time to download or install, so make sure you start early.

Install VMWare and 32 bit Ubuntu

All homeworks in this class will be done in C under Linux, and homeworks will be tested and graded on a Linux machine. To make sure your program runs the same on Tim's machine as it does on yours, you need to run the latest version of Ubuntu on a 32-bit x86 PC. To make sure everyone has the same environment, we will use a virtual machine under VMWare.

VMWare can be downloaded for free here. You'll need to register, and use your UIC email account as your pre-registered account identifier.

You can download the lastest version of Ubuntu from here

then install the development tools with this command on the ubuntu command line:

sudo apt-get install build-essential

TURN-IN REQUIREMENT: When the installation is complete take a snapshot from the host machine. Save the image as hw1i1.png, to be turned in together with the rest of this homework.

Check out your turn-in directory using subversion

Every registered student has their own directory in the course subversion repository. To check out a copy of your directory, use

svn co svn://bits.cs.uic.edu/cs385s11/students/YOURID --username YOURID

then copy the template for homework 1 to your submission directory, and change to this directory to start working on the programming assignment:

cd YOURID

svn cp svn://bits.cs.uic.edu/cs385s11/notes/homeworks/hw1 .

cd hw1

Here are additional instructions on using svn. When you are done with the assignment, all required files should be committed to the repository.

If you have added any files (such as the screenshot), you need to first add this for svn tracking thus:

svn add THEFILE

files that aren't added with an explicit "add" are not managed by svn, and will not be "turned in". Then, to commit a version, do

svn commit -m "An short but informative description of this revision"

Note that you can commit any number of revisions, only the most recent one before the submission deadline will be used for grading. To check the status of your files, use

svn status

the letter in the left column indicates whether a file is being managed, and if so what it's current status is (updated in the repository, updated on your current machine, just added, etc).

TURN-IN REQUIREMENT: When you're done with the homework, make sure you did it right by following the instructions in the last section below.

C Program: processing student records

The there will be several files already in the hw1 directory when you check out from svn. The header file hw1.h contains prototypes for the functions you will implement and should not be edited. The hw1.c file is where you will provide the implementations for each required function. For each part below, a separate file (hw1i2a through d.c) contains the main() function that calls your code.

All of the questions will operate on the Student type defined in hw1.h. The type definition looks like this:

typedef struct {
   int identifier;
   char grade;
   char name[30];
} Student;

NOTE: For this homework only, DO NOT use any built-in functions like: strlen, memcpy, strcpy, sprintf, sscanf etc. except where noted.

a. Writing to the Screen Using printf

Implement the function printStudent(Student s). Using printf, write out the entire structure out byte by byte in hexadecimal format. Next, write to the screen each field of the Student structure in the format described below.

Example usage:

Student st = {1, 'A', "Dennis Ritchie"};
printStudent(st);

example output from hw1i2a:

01 00 00 00 41 44 65 6e 6e 69 73 20 52 69 74 63 68 69 65 00 00 00 00 00 00 00 00 00 ...
ID: 1
Name: Dennis Ritchie
Grade: A

b. Rename a student

Implement the function =renameStudent(Student s, char* newName). Replace the name in the Student record, and return the record.

Example hw1i2b output:

Original record:

01 00 00 00 41 44 65 6e 6e 69 73 20 52 69 74 63 68 69 65 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
ID: 1
Name: Dennis Ritchie
Grade: A

Renaming to "Hu Li"

01 00 00 00 41 48 75 20 4c 69 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
ID: 1
Name: Hu Li
Grade: A

Renaming to "Balakrishnan Radhakrishnan"

01 00 00 00 41 42 61 6c 61 6b 72 69 73 68 6e 61 6e 20 52 61 64 68 61 6b 72 69 73 68 6e 61 6e 00 00 00 00 00
ID: 1
Name: Balakrishnan Radhakrishnan
Grade: A


HINT: Every C string is (and must be) terminated by a zero byte.

c. Print a compact description of a student record

Implement the function printStudentShort(Student s), to print a shorter description of the student. You can expect all student names to be structured have the format " ".

Example output from hw1i2c:

1. D. Ritchie: A
2. T. Merrifield: B
3. J. Eriksson: C

d. Sort student records by grade

Implement the function sortStudentRecords(Student *students, int count), which sorts the count records in the students array by grade, with A first, and F last.

HINT: The list won't be very long, so a simple bubble sort will do just fine.

Know the size of things

For this assignment, turn in a text file called hw1i3.txt. For each of the following types list the size of the type (in bytes), and the size of a pointer to that type.

char
char*
unsigned char
short
int
unsigned int
struct coordinate {int x,y; };
struct pair { struct coordinate from, to; };
union { int a; short b; char c; }
enum { ONE=1; TWO=2; THREE=3; }

For example, your answer should contain the line:

char 1 4
because the size of a char is 1 byte, and the size of a pointer to a char, on a 32-bit machine, is 4 bytes.

HINT: You can have gcc help you with this homework. The C operator sizeof(x) returns the size of a variable or type passed to it. For example, sizeof(signed char)==1. Be sure you understand why a certain type has a certain size though.

TURN-IN REQUIREMENT: For this assignment, write your answers in a text file called hw1i3.txt.

A peek into the inner bowels of C

Given the following program.

int i=257;
int *ip=&i;

main() {
 char c = (char)i;
 char *cp = (char*)ip;
}

what is the assembly code corresponding to the line:

 char c = (char)i;

what is the assembly code corresponding to the line:

 char *cp = (char*)ip;

There is a difference in the opcodes used for these two assignments. What is the difference, and how does this difference affect the resulting value in the destination variable?

HINT: use gcc -S like we did in class.

TURN-IN REQUIREMENT: For this assignment, supply your answers in a text file called hw1i4.txt.

To check that your turn-in is properly turned-in

"cd" to an empty directory, and re-check-out your homework submission:

svn co svn://bits.cs.uic.edu/cs385s11/students/YOURID/hw1

once you're satisfied that all your submission files are in the hw1 directory, you can delete this directory: it is forever preserved in the svn.

Topic revision: r18 - 2012-01-07 - 15:23:20 - Main.jakob
CS385.Homework1 moved from CS385.HomeworkOne on 2011-01-10 - 20:15 by Main.jakob
 
Copyright 2016 The Board of Trustees
of the University of Illinois.webmaster@cs.uic.edu
WISEST
Helping Women Faculty Advance
Funded by NSF