Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Introduction This week, let's do a lab that's (1) smaller than before, and (2) gives you some pieces you can use for this week's assignment.

Introduction

This week, let's do a lab that's (1) smaller than before, and (2) gives you some pieces you can use for this week's assignment.

Background Information

Remember the 2D cellular automata from a few weeks ago? That time, we used a simple 2D nested array for the matrix, declared like this:

int board[20][20]; 

This time, we're going to use malloc() and pointers to create the array because doing it that way makes it easy to use functions with our arrays. This results in simpler code, more reliable and less of it to write.

General Principle:

If you find yourself writing the same code over and over, turn it into a function and only write it once!

Let's get started

Firstly, recall that we can declare a simple 1-dimensional array (of char, for example) like this:

const int how_many = 25; char A[how_many];

As you saw in class earlier this week, we can do exactly the same thing using malloc() like this:

const int how_many = 25; char *A = malloc(how_many * sizeof(char));

Notice the asterisk on A, meaning A is not a char, but a pointer to char. That means A can't hold a character, but it can hold the memory address of a character that is somewhere else in the computer's memory.

Asterisks are used confusingly to indicate multiplication in one place and "this is a pointer" in another place, sorry; blame it on the C language designers not having a large enough keyboard in the year 1969 for them to make the symbols all distinct.

Secondly, recall that we could declare a two-dimensional array like this:

const int rows = 15; const int cols = 20; char board[rows][cols];

We can get the same effect with malloc() like this:

const int rows = 15; const int cols = 20; char **board = malloc(rows * sizeof(char *)); for (int i=0; i board[i] = malloc (sizeof(char) * cols); }

General Principle:

Note the double asterisks in the definition abovetwo dimensions, two asterisks on the main pointer! [Also note that the sizeof() is applied to a pointer to the variable type, i.e., char *, not just char.]

As we saw in class this week, arrays made this way can be used exactly the same way as any other array:

for (int i=0; i for (int j=0; j board[i][j] = 'X';  } }

General Principle:

Arrays made with malloc() are indexed the same way as any other array, with square brackets, like this: some_array[42][13];

The only other thing to remember is that every time you use malloc(), it's important to call free() on that memory when it's no longer neededto give it back to the operating system so other programs can use it. To free the 2-D array that we made with malloc(), call free() on it in the opposite order, like this:

for (int i=0; i free (board[i]);  board[i] = NULL; } free(board); board = NULL;

It's good C practice always to assign a pointer variable the special value NULL when it's not pointing at something.

General Principle:

If you're writing a function that takes a 2-D array as a parameter, and that function DOES NOT need to change the array, then the function parameters will have two asterisks, like this:

void print_array (bool ** A, int rows, int cols) { // ... }

And you'll call the function like this:

print_array (my_array);

But if the function DOES need to modify the array, then the function parameters will have an extra asterisk, like this:

void swap_arrays (bool *** A, bool *** B) { // ... }

And you'll call the swap() function with an ampersand, like this:

swap_arrays (&this_array, &that_array);

The extra asterisk in the function definition, together with an ampersand in the function use, let the function "grab hold" of a thing and modify it.

Code You Need to Write Today:

Please write code to do the following:

0. Create a 2-D array of type bool with 10 rows and 12 columns using malloc(). Initialize every element to true.

1. Write a function to print a 2-D array of bool. The function prototype should look like this:

void print_2D_array_of_bool (bool ** A, int rows, int cols);

It should produce output something like this:

T F F F F F F T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T F T T T T T T T T T F T T T T T T T F T T F T T T T T F T T F T T T T T T T T T T T F T T T T T T T T T T T F T T F T T T T T T T F

Your function should print T if the array position (x,y) is true, F if it's false.

2. Create another 2-D array of type bool the same size as the previous one. Initialize every element of thisarray to false.

3. Write a function to swap the values of two arrays. The function prototype should look like this:

void swap_2D_arrays_of_bool (bool *** X, bool *** Y);

Note: three asterisks in the function parameters this time, andamazinglythe function doesn't need to knowhow many rows and columns are in there, because it's only swapping pointers. Remember the function you saw in class to swap two integers using pointers:

void swap (int * x, int * y) {  int temp = *x;  *x = *y;  *y = temp;  return; }

Hint: for two-dimensional arrays, you need three asterisks in the function parameters.

Another hint: the temporary pointer in your function should have one less asterisk on it than the function parameters.

In class, we called the swap() function with ampersands on both arguments, like this:

int first = 10; int second = 20; swap (&first, &second);

Hint: do it the same way with arrays.

4. Test your code by printing the first array, printing the second array, then calling swap_2D_arrays_of_bool() and printing both arrays again.

What To Turn In:

Submit your .c file through Canvas

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Building The Data Warehouse

Authors: W. H. Inmon

4th Edition

0764599445, 978-0764599446

More Books

Students also viewed these Databases questions

Question

Write a Python program to check an input number is prime or not.

Answered: 1 week ago

Question

Write a program to check an input year is leap or not.

Answered: 1 week ago