COMP1911 23T2 Introduction to Programming

Objectives

In this Lab, you will practise:

Preparation

Before the lab you should re-read the relevant lecture slides and their accompanying examples

Getting Started

Login and run following commands inside a Unix terminal

Create a new directory for this lab called lab09 by typing:

mkdir lab09
Change to this directory by typing:
cd lab09

Introduction

This week we will be reinforcing your understanding of structs and arrays in preparation for your assignment.

For this lab the "slideAndMerge" exercise will be treated like two separate exercises. "slide" and "merge" will each account for 1/3 of lab actvities.

Exercise 1: Structs for Students

We've completed a C program for you studentStruct.c that declares/initialises a string array of names and a corresponding integer array of ages of students. It then increments all the student's ages by 1 and prints out the results

This program works fine, however,

Modify the C program we've written studentStruct.c to contain the following struct and typedef definition

struct studentInfo {
	age *studentAges;
	name *studentNames;
	int size;
};

typedef struct studentInfo StudentInfo;

And then modify the function prototypes to work with this struct data type instead

void incrementAges(StudentInfo si);
void printStudents(StudentInfo si);

You can copy this file into your current directoy by running

$ cp /web/cs1911/current/tlb/09/studentStruct.c .
$ dcc studentStruct.c -o studentStruct
$ ./studentStruct

Student Frank is of age 19
Student Moni is of age 20
Student Jimmy is of age 21
Student Chen is of age 20
Student Navim is of age 19
Student Faraq is of age 20

You can test your program as normal:

 $ 1911 autotest lab09 studentStruct.c

Exercise 2: Sliding and Merging an array

We have completed all but a function in a file slideAndMerge.c which reads from STDIN 10 integers and stores them in an array.

It then calls a function "slideAndMerge" which takes the array and attempts to slide all elements to the leftmost point in the array.

In this array 0 is treated as an "empty" item.

Here are some examples of sliding happening for lists that can't merge

$ dcc slideAndMerge.c -o slideAndMerge
$ ./slideAndMerge
1020304050
1 2 3 4 5 0 0 0 0 0
$ ./slideAndMerge
1230006200
1 2 3 6 2 0 0 0 0 0

If adjacent items are the same then they should merge into one cell. The new value of the cell they're merged into is double the original value. I.E. 2 and 2 would merge into a cell that is 4. For every slide (i.e. single program run) only one merge occurs.

Hint: If you're struggling to figure out how to approach this, your program can follow a 3 step process to make this happen: Slide, merge, slide. For example

1 0 1 0 2 0 2 0 3 0 # original
1 1 2 2 3 0 0 0 0 0 # slide
2 0 4 0 3 0 0 0 0 0 # merge
2 4 3 0 0 0 0 0 0 0 # slide
$ ./slideAndMerge
1010550000
2 10 0 0 0 0 0 0 0 0
$ ./slideAndMerge
1123300011
2 2 6 2 0 0 0 0 0 0

Complete the slideAndMerge function

You can complete it any way you, including writing more functions yourself and having slideAndMerge call those functions.

You can copy this file into your current directoy by running

$ cp /web/cs1911/current/tlb/09/slideAndMerge.c .

You can test your program as normal:

 $ 1911 autotest lab09 slideAndMerge.c

Submission/Assessment

When you are satisfied with your work, ask your tutor to assess it. You are to submit it electronically by typing (run this command in your lab09 directory):
give cs1911 lab09 studentStruct.c slideAndMerge.c
Submit advanced exercises only if you attempt the advanced exercise.

Remember the lab assessment guidelines - if you don't finish the exercises you can finish them in your own time, submit them by 19:59:59 Sunday using give and ask ask tutor to assess them at the start of the following lab.

You can also just run the autotests without submitting by typing

1911 autotest lab09