Skip over navigation

Contents

Review of Arrays

Review Test

Table of Contents

How to Cite This SparkNote

The following function:


int foo (mystery_t arr[])
{
	int j = 0;

	while (arr[j] != '\0') {
		j++;
	}
	return j;
}

Assume that an int is 4 times the number of bytes of a char. If int_p is a pointer to an array of integers and char_p is a pointer to an array of chars, and the addresses of both are equal to each other, then what does char_p + 4 equal?

Assume that an int is 2 times the number of bytes of a char. If int_p is a pointer to an array of integers and char_p is a pointer to an array of chars, and the addresses of both are equal to each other, then what does char_p + 6 equal?

What does the following function do assuming n1 is the length of arr1 and n2 is the length of arr2?


int foo (int arr1[], int n1, int arr2[], int n2) 
{
	int j, k;
	for (j = 0, k = 0; 
	     j < n1 && k < n2; 
	     j, k, arr1, arr2) {
		if (arr1 != arr2) return 0;
	}
	return 1;
}

True or False: It is possible to have an array in a program where the size of the array is specified exactly by the user at run-time.

True of False: Assuming a char is one byte in size and an int is four bytes in size, then the string "thespark" requires the same amount of memory as two ints.

What does the following function do, assuming that n is the size of the array arr?


void int(int arr[], int n)
{
	int j, k;
	for (j = 0; j < n / 2; j++) {
		k = arr[j];
		arr[j] = arr[n - j - 1];
		arr[n - j - 1] = k;
	}
}

True or False: Passing an array using the syntax arr[] creates a local copy of the array whereas *arr creates a pointer to the array.

True of False: A two-dimensional array does not necessarily require more memory than a one-dimensional one with the same number of cells of the same data type.

What does the following function do:


int foo (int arr[], int n) 
{
	if (n == 0) return 0;
	if (n == 1) return arr[0];
	return MAX(foo(arr, n / 2), foo(&arr[n/2], n - n/2));
}

What does the following function do:


int foo(int arr[], int n) 
{
	int j, k=0;
	for (j = 0; j < n; j++) {
		if (arr[j] > k) k = arr[j];
	}
	return j;
}

arr is an 5 by 6 array of ints while arr_p is a pointer to an int. If arr and arr_p point to the same memory address, then what does arr[3][4] equal?

In a function definition, having a pointer is equivalent to having an array.

When specifying a multi-dimensional array argument in function definition, you must always include the number of rows.

It would not be possible to add a new chunk of memory onto an existing two-dimensional array such that it increased the number of columns while keeping the same things in every row.

It would not be possible to add a new chunk of memory onto an existing two-dimensional array such that it increased the number of rows while keeping the same things in every column.

Given the following code:


mys[0] = 1;
mys[1] = 1;
for (i = 2; i < 100; i++) {
	mys[i] = mys[i - 1] + mys[i - 2];
}
What does mys[99] equal?

Do the following two code chunks do the same thing in memory?


char arr[][3] = {
		{'a', 'b', '\0'},
		{'c', 'd', '\0'}
	     };
and

char* arr[] = {"ab", "cd"};

Assuming ints are 2 characters and arr is declared:


char arr[] = {(char) 1, (char) 2, (char) 1, (char) 2, (char) 1}
What does the integer n equal if:

n = (int) &arr[1];

What is the difference between the following two chunks of code?


char arr1[] = {'a', 'b', 'c'};
and

char *arr2 = "abc";

What does this return?


int getInd(int *arr, int index);

void main() {
    int array[5];
    int i;
    for(i = 0; i < 5; i++) {
       array[i] = i + 2;
    }
    printf("%d\\n",getInd(array,4));
}

int getInd(int *arr, int index) {
    return arr[index];
}

How long was the array 'array' in the above question?

What does this code print?


void main() {
    int array[5];
    int i;
    for(i = 0; i < 5; i++) {
       array[i] = i + 2;
    }
    printf("%d\n",array[array[2]]);
}

What does this code print?


void main() {
    int array[5];
    int i;
    for(i = 0; i < 5; i++) {
       array[i] = i + 2;
    }
    printf("%d\n",array[array[array[2]]]);
}

What does this code print?


void main() {
    int array[5][6];
    int i, j;
    for(i = 0; i < 5; i) {
       for(j = 0; j < 6; j) {
          array[i][j] = i * j;
       }
    }
    printf("%d\n",array[2][3]);
}

What does this output?


void main() {
    char *string = "harvard";
    string[2] = 'h';
    string[5] = 'h';
    printf("%s\n",string);
}

What about this?


void main() {
    char *string = "harvard";
    string[2] = '\0';
    printf("%s\n",string);
}

What does this output?


void main() {
    int arr[4][4];
    int *ind = &arr[0][0];
    int i, j;
    for(i = 0; i < 4; i) {
       for(j = 0; j < 4; j) {
          arr[i][j] = i * j;
       }
    }
    printf("%d\n",ind[11]);
}

What about here?


void main() {
    int arr[4][4];
    int *ind = &arr[0][0];
    int i, j;
    for(i = 0; i < 4; i) {
       for(j = 0; j < 4; j) {
          arr[i][j] = i * j;
       }
    }
    printf("%d\n",ind[12]);
}

What will this program do?


void main () {
	int arr[] = {0, 1, 2};
	int j, *p;

	p = &arr[0];
	for (; p < arr + 3 * sizeof(int); p += sizeof(int) {
		print ("%d", *p);
	}
}

Given:


int arr[] = {0, 2, 4}
If ( ((int *)&arr[0]) + q == (int *)&arr[2] ), what does q equal?

What does this code do?


int mys[128];
str = "sparks";
for (c = 0; c < strlen(str); c) {
	mys[str[c]];
}

True of False: If you have a string constant in your program, such as str = "spark", then you are using dynamic memory allocation because the space for "spark" is allocated at run time.

In order for an array to be globally modified in a function, the zeroeth element of the array must be cast to a pointer and passed in as opposed to passing the array variable itself.

What will the following do:


char * str = "stark";

str[1] = 'p';

What does the following code do?


count = 0;
for (j = 1; j < 80; j) {
	if (sample[j - 1] * sample[j] < 0) count;
}

What does the following code do?


for (s = str; s[0] != '\0'; s) {
	(*s);
}

True of False: Because an array is just a pointer, declaring an array of any length sets aside the same amount of memory as declaring a pointer.

True of False: It is not possible to make a two dimensional array with different row lengths by only making one array (each row in the array must be made separately as its own array).

Study the code below. It will output a string of 1's and 0's. How many 1's will it output?


void main() {
    int bulbs[100];
    int i, j, k;
    for(i = 0; i < 100; i) { bulbs[i] = 1; }
    for(i = 2; i < 100; i) {
       for(j = (i - 1); j < 100; j ) {
          if(bulbs[j] == 0) { bulbs[j] = 1; }
          else { bulbs[j] = 0; }
       }
    }
    for(i = 0; i < 100; i) { printf("%d",bulbs[i]); }
    printf("\n");
}

What does the following print?


j = 0;
for (; j <= MAX; j++) {
	print ("%d", arr[j]);
}

What does the following do?


int mys(mys_t *p, mys_t c) 
{
	if ((*p) == c) return 0;

	return 1 + mys(p+1, c);
}

True of False: Because arrays can be modified in functions, if you do pointer arithmetic on an array passed in, this will advance the array in the calling function.

In terms of size, the following two dimensional and one dimensional arrays are equivalent (both have space for 50 integers):


int arr2d[5][10];
int arr1d[5*10];
What location in arr1d is equivalent to arr2d[3][7]?

If an long int is 8 bytes, a single long int can be used for a 64 position bitset. The goal of the following function is to return an array that can be used as a bitset for the number of positions that need to be filled in. What should replace the xxx in the function?


long int *new_bitset(int num_positions)
{
	int factor = num_positions / xxx;
	if (num_positions % xxx != 0) factor++;
    return malloc(sizeof(long int) * factor);
}

The expression


*(&a[2] + 1)
is equivalent to:

There is no string type in C. Instead, null-terminated arrays of characters are used as strings. The following function is designed to test whether the string passed in is a palindrome, returning 1 if it is, 0 if it isn't. What should the for condition be?


int is_palindrome(char *str)
{
    int i, j;
    int length = strlen(str);

    if(str == NULL)
        return 0;

    for(xxx)
        if(str[i] != str[j])
            return 0;

    return 1;
}

The following function is causing a memory violation. What line could not be responsible?


1   char *my_strdup(char *str)
2   {
3       int i;
4       char *str2 = malloc(strlen(str));
5       int length = strlen(str);
6       for(i = 0; i <= length; i++)
7           str2[i] = str[i];
8       str2[i] = '\0';
9       return str2;
10  }

Does the following function correctly sort an array of integers?


void sort(int *start, int *end) {
    int *marker, *n, temp;

    if(start >= end)
        return;

    n = start;

    for(marker = start; marker <= end; marker++) {
        if(*marker < *n)
            n = marker;
    }

    temp = *start;
    *start = *n;
    *n = temp;

    sort(start + 1, end);
}

Follow Us