Sample Final Problems - V22.0002

Note: this is not an actual final. These are the kind of problems you will be expected to solve on the final.

• Write a program that reads integers from the keyboard ending in -9. The -9 acts as a sentinel and is not used in the further processing of the data. All the numbers, except the -9, will be stored into an array. The program should then read a number N from the keyboard, which will indicate the position in the array of one of the number (call this number target) with the first number entered having position 1, etc. The program will then print out how much each number in the array differs from target.

• Sample run:

```   Enter numbers (ending in -9): 3 6 4 7 6 19 -9
Enter position of target    : 2
Differences                 : -3 0 -2 1 0 13```
Notes:
• You have to make sure that the number of integers read is not bigger than the array's size.
• Remember that arrays in C start from index 0.

```   #include <stdio.h>

#define ARRAY_SIZE 100

int main()
{
int value = 0, count = 0, target = 0, index, array[ARRAY_SIZE];

printf("Enter numbers (ending in -9): ");

do
{
scanf("%d", &value);
if( value != -9 )
{
array[count] = value;
count++;
}
}while( (count < ARRAY_SIZE) && (value != -9) );

printf("Enter position of target    : ");
scanf("%d", &target);
printf("Differences                 : ");

for(index = 0; index < count; index++)
{
printf("%d ", array[index] - array[target-1]);
}
printf("\n");

return 0;
}```

• Write a program that reads a group of characters ending with a '.'. The characters will be stored into an array called inp. The program will then process the array inp and place the first character that follows each blank in successive elements of an array called out. The program will finally print the array out with one character per line.

• Sample run:

```   Enter characters (ending in '.'): This is a sample run for the final.
i
a
s
r
f
t
f```
Notes:
• You have to make sure that the number of characters read is not bigger than the array's size.
• Remember that arrays in C start from index 0.

```   #include <stdio.h>

#define ARRAY_SIZE 100

int main()
{
int count = 0, index = 0, inpIndex = 0, outIndex = 0;
char value, inp[ARRAY_SIZE], out[ARRAY_SIZE];

printf("Enter characters (ending in '.'): ");

do
{
scanf("%c", &value);
if( value != '.' )
{
inp[count] = value;
count++;
}
}while( (count < ARRAY_SIZE) && (value != '.') );

for(inpIndex = 0; inpIndex < count; inpIndex++)
{
if( inp[inpIndex] == ' ' )
{
out[outIndex] = inp[inpIndex+1];
outIndex++;
}
}

for(index = 0; index < outIndex; index++)
{
printf("%c\n", out[index]);
}

return 0;
}```

• Write a function called CellFiller that takes an array of integers (call it cells) as its parameter and then:
1. Reads in a positive integer, k.
2. If the kth element of cells is set to -1, you will store k in that location.
3. If the kth element of cells is not set to -1, you will store k in the next available location of cells (i.e. the next available location that is set to -1).

So, if cells contains the following values: cells[3] = 3, cells[4] = 4, cells[5] = -1, cells[6] = 6 and cells[7] = -1. If the value read is 7 it will be stored in cells[7]; if the value read is 3, it will be stored in cells[5].

You can assume that cells is infinitely large.

```   void CellFiller(int cells[])
{
int k, index;
scanf("%d", &k);
index = k;
while( cells[index] != -1 )
{
index++;
}
cells[index] = k;
}```

• You're helping the dean with a program that categorizes students depending on the grade they received in a given class. Your job is to write a function that takes two parameters: the first is a string with the Id number of the student, the second is a single character with the letter grade the student received (the class only awards A, B, C, D and F). The function will print out the ID number and the word "honors" if the student received an A, the word "pass" if the student received a B or a C, the word "remedial" if the student received a D and the word "fail" if the student received an F.

•

Sample run, with ID = 012-34-6789 and grade 'A':

`   012-34-6789 honors`
Sample run, with ID = 012-34-6789 and grade 'B':
`   012-34-6789 pass`
Notes:
• The function does not read anything from the keyboard.
• What will be the appropriate return type for the function ?

```void Rank(char id[], char grade)
{
printf("%s ", id);

{
case 'A':
printf("honors\n");
break;

case 'B':
case 'C':
printf("pass\n");
break;

case 'D':
printf("remedial\n");
break;

case 'F':
printf("fail\n");
break;

default:
break;
} /* end switch grade */

} /* end Rank function */```

• Write a program that reads a series of four-digit positive integers from the keyboard ending with the sentinel -9. The integers will be stored into an array using as index the last two digits of each integer. Thus the number 1023 would be stored in location 23, the number 1141 will be stored in location 41, etc. Assume that the last two digits of each integer are unique, that is, there is only one integer that ends with the same last two digits.

•

Note: How big do you have to make the array ?

```int main()
{
int iInput = 0, iIndex = 0, iValues[100] = { 0 };

printf("Enter numbers (ending in -9): ");

do
{
scanf("%d", &iInput);
if( iInput != -9 )
{
/* get the last two digits for the index of array */
iIndex = iInput % 100 ;

/* put user's input in that index of array */
iValues[ iIndex ] = iInput;

} /* end if iInput != */

} while( iInput != -9 );

return 0;
} /* end main */```

• Write a program that reads integer test scores. The input will be terminated by a sentinel of your choice (don't use the same given in the sample run below). The program will then output the minimum score, the maximum and the average with two decimals of precision. Write the program without using arrays.

•

Sample run:

```   Enter scores (ending in <your sentinel>): 82 76 99 67 78 83 91 92 <your sentinel>
Min: 67 Max: 99 Average: 83.50```
Notes:
• What will be a good choice for a sentinel in this case ?
• What is a good initialization value for the minimum and the maximum ?

```   #include <stdio.h>

#define SENTINEL -999

int main()
{
int min, max, total, score, count = 0;

printf("Enter scores (ending in %d): ", SENTINEL);

/* Read first to initialize */

scanf("%d", &score);
if( score != SENTINEL )
{
min = score;
max = score;
total = score;
count = 1;
}

while( score != SENTINEL )
{
scanf("%d", &score);
if( score != SENTINEL )
{
total += score;
if( score < min )
{
min = score;
}
else
{
if( score > max )
{
max = score;
}
}
count++;
}
}

if( count > 0 )
{
printf("Min: %d Max: %d Average: %.2f\n", min, max, (float )total / count);
}

return 0;
}

```
• A geometric progression is a sequence of numbers such that each number bears a constant ratio, called the common ratio, to the previous number. If a[1] is the first term, and r is the common ratio, the the nth term would be given by the formula:
• `   a[n] = a[1] * r^(n-1)`
In other words, if the first term is equal to 4 and the common ratio is equal to 2 you would have the following series of numbers:
```   n  a[n]
--------
1    4
2    8
3   16
4   32
5   64
6  128
......```
You can notice that you start from the first number and simply keep multiplying by the common ratio.

You have to write a program that produces a table of geometric progressions after reading the following information from the keyboard:

1. The starting first element
2. The ending first element
3. The common ratio
4. The number of entries to be displayed for each series.
Sample run:
```   Starting first element: 2
Ending first element  : 5
Common ratio          : 2
Number of entries     : 6

2       4       8       16      32      64
3       6       12      24      48      96
4       8       16      32      64      128
5       10      20      40      80      160```
Notes:
• You should use TABS ("\t") to make the output pretty.
• How many loops do you need ? Any nested loop ?

```   #include <stdio.h>

int main()
{
int first, last, ratio, number, series, entry, value;

printf("Starting first element: ");
scanf("%d", &first);
printf("Ending first element  : ");
scanf("%d", &last);
printf("Common ratio          : ");
scanf("%d", &ratio);
printf("Number of entries     : ");
scanf("%d", &number);

for(series = first; series <= last; series++)
{
value = series;
for(entry = 0; entry < number; entry++)
{
printf("%d\t", value);
value *= ratio;
}
printf("\n");
}

return 0;
}```