You are on page 1of 3

Engineering Programming 100

Practical 7: Arrays and Structures


1 Outcomes
By the end of this practical you will be able to

Generate code for a number of situations that require the use of arrays and structures.

2 Preparation
It is expected that prior to doing to this practical you have done the following:

Access to a C text book


Revised previous lectures
Completed previous practicals

3 Pre-Lab Exercises
1. What do the following statements mean or produce?
(a)

int marks[100];

(b)

int colour[3] = {26, 104, 56};

(c)

float rotation[3][3] = {{25.0, 38.0, 15.0},


{103.0, 77.0, 48.0},
{ 44.0, 95.0, 35.0}};

(d)

float rotation[3][3] = {25.0, 38.0, 15.0,


103.0, 77.0, 48.0,
44.0, 95.0, 35.0};
struct address{
int number;
char street[2][100];
char suburb[100];
int postcode;
char state[100];
char country[100];
};

(5)

struct address employee[500];

2. What does the following code examples produce?

(a)

double x[3][3];
int i,j,k;
k = 0;
for(j=3; j>=1; j--)
{
k++;
for(i=0; i<3; i++)
{
x[j-1][i] = k;
}
}
for(j=0; j< 3; j++)
{
for(i=0; i<3; i++)
printf( %lf\n, x[j][i]);
printf(\n);
}

(b)

typedef float banana;


banana split;
split = 3;
printf(split: %f\n,split);

4 Lab Exercises
1. Take the vector multiplication example from the lecture on Arrays and Structures (see solution and
implement it in a program.
a. Confirm that it works as it should and produces the right answers.
b. Use two different methods to initialise the vectors.
c. Given that the vectors are three elements long, experiment with what happens if you
change the loops so that they run from 0 to 100 instead of from 0 to 3.
2. Implement the matrix transpose example for a 33 matrix from the lecture on Arrays and
Structures and confirm that it works as expected. Choose suitable variable names and values for
the matrix. Print out the matrix and its transpose to confirm the correctness of the results.
3. Implement the code to do a matrix multiplication for a 33 matrix. Instead of doing this from
scratch, revisit your solution to this from practical Fundamentals_2. As a hint, look at the pattern
of how the indices of the arrays vary for the calculations and implement using for loops.

4. Construct a structure that will hold the information describing books in a library such that you can
store the details of 1000 books. Each book entry should contain the following information and you
should choose the appropriate variable type for each piece of information:
a. Book name.
b. Book author.
c. Number of pages.
d. Number of chapters.
e. The ISBN number.
f. Year of publication.
g. Publisher.
Note there is much flexibility in the choice of variable types so choose the ones you think are best.
Use the structure in a program that stores the details of at least two books and prints out the
information in a suitable format. Note to read a string of characters into a program, use the
underscore character (_) instead of a space ( ) to separate words as scanf uses a space to separate
parameters.
5.

Take the coding example for smoothing the image from the lecture Arrays and Structures and
modify the code to invert the image data point by data point (or pixel by pixel) and for each colour
(red, green, blue). Modify the code to create a new program with the code for smoothing replaced
by your code for inverting the image. Use the program GIMP (available in the labs and under
Microsoft Windows) to display the resulting image.
Thats all, make sure you log off!

You might also like