CS 246 Lab 5
Spring 2017
Pointers
Finish the pointer exercises from class if you did not already do so. Check your answers to problems 3 and 4 by writing short programs.
One-dimensional array as pointer
Write a function
void meansum(int n, int* p, double* mean, int* sum)
that computes the mean (average) and sum of n
int
s, starting at memory location p
. Do not use any brackets in writing this function. Place the mean in *mean
and the sum in *sum
. Here is a main
that can test this function:
int main()
{
int xs[] = { 2, 8, 3, 4, 1 };
double m;
int s;
meansum(5, xs, &m, &s);
printf("Mean: %lf\n", m);
printf("Sum: %d\n", s);
return 0;
}
Note how this main
declares an array xs
and passes it into a function argument that is a pointer.
Two-dimensional array as pointer
Write a function
bool fifteen_winner(int* board)
that takes the memory address of the first element in the Fifteen Puzzle’s board and checks whether or not it has the required form of
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 0
Unlike in hw03, your function accepts the puzzle as an int*
. As described in our book, p. 170, two-dimensional arrays are stored in row-major order, which means that the rows are stored in memory one after the other. So, if we have
int a[2][2] = { {1, 2}, {3, 4} };
then z
refers to a region of memory containing 1
, 2
, 3
, and then 4
. The memory layout is precisely the same as if we had said
int b[4] = {1, 2, 3, 4};
These declarations are different because the types are different – a
needs two indices while b
needs only 1. But the memory is the same. We can use this fact about memory layout to write fifteen_winner
, which should actually be simpler than the version you wrote on the homework assignment.
Here is a main
that can test your work:
int main()
{
int solved[4][4] = { { 1, 2, 3, 4},
{ 5, 6, 7, 8},
{ 9, 10, 11, 12},
{13, 14, 15, 0} };
if(fifteen_winner((int*)solved))
{
printf("solved\n");
}
else
{
printf("not solved\n");
}
return 0;
}
This should print solved
. Then, if you change the array, it should print not solved
.
Note the cast (the (int*)
). Casts are described in our book starting p. 147. They tell the compiler that you want to treat one type (in our case, a two-dimensional array) as another (an int*
). A cast is not necessary in the 1-dimensional case because C will automatically convert from a one-dimensional array to a pointer; it complains, however, when you try this with a two-dimensional array. (Why? Because pointer arithmetic on a one-dimensional array works the same as it does on a pointer. Pointer arithmetic on a two-dimensional array works differently. Try it out!)
Git
Follow the tutorial at https://git-scm.com/docs/gittutorial, but read the following notes first:
A project.tar.gz is available for you to work with.
The tutorial tells you to use tar xzf project.tar.gz
. This does not work on powerpuff, and may not work on other systems. Instead, use tar xf project.tar.gz
. No z
.
You will need an SSH key to work with GitHub. Follow the instructions to “Register an SSH key with GitHub” here.
The classwork repo is a great place for you to experiment with git’s collaboration features. When you get to the collaboration section, use ssh://git@github.com/bmc-cs246/classwork.git
instead of /home/alice/project
when cloning.
Work with your partner in this section: one of you is Alice, and one is Bob.