Practice with Arrays
Solutions
Assignment 1
Write a program to read five values into an array and print them out in reverse order (Use two independent for loops).
R:
int i = 0;
int values [5];
for (i=0; i<5;i++)
{
printf ("Please enter an integer: ");
scanf ("%d", &values[i]);
}
printf ("Result: ");
for (i=4; i >= 0;i--)
printf ("%d ", values[i]);
printf ("\n");
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;
R:
0 1 2 3 4 5 6 7 8 9
0 0 1 1 2 2 3 3 4 4
b)
for (k = 0; k < little; k++)
number[k] = k * k;
for (k = little; k < medium; k++)
number[k] = number[k - 5];
R:
0 1 2 3 4 5 6 7 8 9
0 1 4 9 16 25 1 4 9 16
c)
number[0] = 1;
k = 1;
do
{
number[k] = 2 * number[k -1];
k++;
}
while (k < medium);
R:
0 1 2 3 4 5 6 7 8 9
1 2 4 8 16 32 64 128 256 512
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]
R:
A. 0
B. 150
C. ???
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]);
R:
0
2
0
3
0
4
0
5
0
6
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
R:
2) 7 8 13 44 26 23 98 57
3) 7 8 13 23 26 44 98 57
4) 7 8 13 23 26 44 57 98
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
Which sorting algorithm is being used? Explain your answer.
R:
Insertion algorithm. If selection had been used, after 2 passes, the array would have looked like this:
3 32 66 47 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
Which sorting algorithm is being used? Explain your answer.
R:
Bubble sort algorithm. The two largest numbers were passed to the last two positions.
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
R:
1. L = 0; M = 3; H = 7
2. L = 4; M = 5; H = 7
3. L = 6; M = 6; H = 7
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");
______;
}
R:
for (cnt1 = 0; cnt1 < 22; cnt++)
for (cnt2 = cnt1+1; cnt2 < 23; cnt2++)
break
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]);
R:
a) Lakers
b) Nets
c) ers
d) ERROR (this is a character, not a string)
e) L
f) ERROR (this is a string, not a character)
g) k