## Practice with Arrays

Complete these exercises to test your understanding of arrays.

### Assignments

##### Assignment 1

Write a program to read five values into an array and print them out in reverse order (Use two independent for loops).

##### Assignment 2

Assume the following declarations are made:

const int little = 6, medium = 10;int k, number[medium];

For each of the following, show what the array looks like after the execution of the code:

a)

for (k = 0; k < medium; k++)number[k] = k / 2;

b)

for (k = 0; k < little; k++)number[k] = k * k;for (k = little; k < medium; k++)number[k] = number[k - 5];

c)

number[0] = 1;k = 1;do{number[k] = 2 * number[k -1];k++;}while (k < medium);
##### Assignment 3

Given the statement:

int profit[6] = {150, 300, 500};

What is the value of n, when:

a) n = profit[3]
b) n = profit[0]
c) n = profit[6]

##### Assignment 4

What would be printed by the following program segment:

 int list[10] = {0}; int k; for (k = 0; k < 5; k++) list[2 * k + 1] = k + 2; for (k = 0; k < 10; k++) printf ("%4d\n", list[k]);

##### Assignment 5

An array contains the elements shown below. The first two elements have been sorted using a selection sort. What would be the value of the elements in the array after three more passes of the selection sort algorithm?

1) 7 8 26 44 13 23 98 57

##### Assignment 6

We have the following array:

47 3 66 32 56 92

After two passes of a sorting algorithm, the array has been rearranged as shown below:

3 47 66 32 56 92

##### Assignment 7

We have the following array:

80 15 21 72 3 33

After two passes of a sorting algorithm, the array has been rearranged as shown below:

15 21 3 33 72 80

##### Assignment 8

An array contains the elements shown below. Using the binary search algorithm, trace the steps followed to find 88. At each loop, including the last, show the contents of low, middle, and high.

8 13 17 26 44 56 88 97

##### Assignment 9

The program below searches through an unsorted array for any to numbers that match and stops the search if a match is found. Fill in the missing numbers, variable names, or statements in the space provided:

 int numbers[23], cnt1, cnt2, cnt3, testNo; int match = 0; for (cnt1 = 0; cnt1 < ______; cnt++) { testNo = numbers[cnt1]; for (cnt2 = ______; cnt2 < ______; cnt2++) { if (testNo == numbers[______]) { match = 1; break; } if (match == 1) { printf ("A match has been found!\n"); ______; }

##### Assignment 10

Based on the following array, what will be the output in each declaration?

char teams[ ] = {'L','a','k','e','r','s','\0','N','e','t','s','\0'};

a) printf(" %s",teams);
b) printf("%s",teams+7);
c) printf("%s",(teams+3));
d) printf("%s",teams[0]);
e) printf("%c",(teams+0)[0]);
f) printf("%c",(teams+5));
g) printf("%c",(teams-200)[202]);

Source: Kishori Mundargi, https://www.peoi.org/Courses/Coursesen/cprog/frame1.html