- Simple Array:
Consider the partial program below. Add
printf statements so that the program
will produce exactly the output shown.
(The numbers must be from the array, and not just in
just inside the string of the printf, so
that different numbers in the array will produce a
different output. Do not try to use a loop here.)
#include <stdio.h>
int main() {
int comb[] = { 36, 24, 12 };
printf("The combination for the safe:\n");
printf("Turn left to %d\n", comb[0]);
printf("Turn right to %d\n", comb[1]);
printf("Turn left to %d, open.\n", comb[2]);
}
Required Output:
The combination for the safe:
Turn left to 36
Turn right to 24
Turn left to 12, open.
- Simple Array:
Fill in the rest of the C program below, so that it will
use a loop to add the square of each
number in the array and then print the sum.
(The program should be complete with all necessary declarations.
You must use a loop.)
#include <stdio.h>
int main() {
int p[] = {2, 3, 5, 7, 11, 13, 17}; /* first 7 prime numbers */
/* Use a loop to add the square of each number in this array. */
/* Then print the sum. */
int sum = 0;
int i;
for (i = 0; i < 7; i++)
sum = sum + p[i]*p[i];
printf("Sum of squares of the first 7 primes: %i\n", sum);
}
Output:
Sum of squares of the first 7 primes: 666
- Reading numbers into an array:
The following program from class reads integers
as "exam scores" and puts them into an array.
Answer the questions below, some of which involve adding
to the code
#include <stdio.h>
int main() {
int score;
int s[8];
int n = 0;
/* other declarations here */
int i;
int sum = 0;
double average;
for (;;) { // infinite loop, terminated by break inside
scanf("%i", &score);
if (score == -1) break;
s[n] = score;
n++;
}
/* rest of the code here */
for (i = 0; i < n; i++) // loop to print scores
printf("%i ", s[i]);
printf("\n");
for (i = 0; i < n; i++) /* loop to calc average */
sum = sum + s[i];
average = (double)sum/n;
printf("Average score: %0.2f\n", average);
}
Output of a run:
80 100 90 -1
80 100 90
Average score: 90.00
- Suppose you want to enter three scores:
80, 100, and 90.
What do you have to type after these three numbers so that
the program will work?
The -1 is needed to terminate the input
for this program. It is not treated as a score.
- When the program is done reading, draw a diagram to
show the values in the array s and give
the value of the variable n .
s[0] = 80; s[1] = 100;
s[2] = 90; rest are uninitialized. n = 3;
- Write a loop at the end of the code above to print the exam scores,
all on one line with a blank between each one. At the
end print a newline. You must use a loop. You will also have
to declare a loop variable.
- Write a second loop after the one above that will
add up the scores that are in the array s,
using an integer variable sum.
Write code to calculate the average of the scores,
using a double variable average. Finally print
the value of the average score. You will have to declare
sum and average.
(It would be possible to add up the scores as they are read,
but are supposed to work with the scores that are in the array.)
- 2-dimensional arrays:
Consider the following program that declares a 2-dimensional
array named x .
#include <stdio.h>
/* prototype for the function rowsum below */
int rowsum(int i, int x[3][4]);
int main() {
int x[3][4] = { { 10, 12, 14, 16},
{ 22, 24, 26, 28},
{ 34, 36, 38, 40} };
int i, j;
/* code for part c below */
x[1][3] = 57;
/* code for part d below */
for (i = 0; i < 3; i++) {
for (j = 0; j < 4; j++)
printf("%2i ", x[i][j]);
printf("\n");
}
/* call to the function rowsum */
printf("Sum of row 2 = %i\n", rowsum(2, x));
}
/* code for the function rowsum below */
int rowsum(int i, int x[3][4]) {
int j, sum = 0;
for (j = 0; j < 4; j++)
sum = sum + x[i][j];
return sum;
}
Output of a run:
10 12 14 16
22 24 26 57
34 36 38 40
Sum of row 2 = 148
- How many rows does the array x have?
What is the length of each row?
There are 3 rows, each of length 4.
- What index number does C give to each row of
the array x ? What index number does C give
to each column of the array x ?
The rows are number 0, 1, and 2;
the columns are numbers 0, 1, 2, and 3.
- Put in code above that will give the element in
row number 1 and in column number 3 the value 57 .
- Give code above that will use a double for loop to print the
values of the array x , each row on a separate
line, with the elements of each row separated by one blank.
- Write a function rowsum with a two formal
parameters: a row number i and the whole array
x itself. This function will add up the numbers in row
number i (ignoring the other rows), and will
return the sum of these numbers as an int .
(The code above shows a call to this function inside the main
function that will find the sum of the numbers in row 2
and will print this sum, which is 148 .)
- Trivia question: Which European country has the
highest population density? [Hint: think weird, think small.]
Points for this problem: if top_pd is a double
giving the value of the highest population density, and
your_pd is a double giving the value of the
population density of the country that you name, then
you will get the following number of points:
(int)(10.0*your_pd/top_pd) .
The answer is Monaco at 16000/km-sq., for (int)(10*1) = 10 points
Next is Gibraltar at 5000/km-sq., for (int)(10*5/16) = 3 points
Next is Vatican City at 2000/km-sq., for (int)(10*2/16) = 1 points
Next is Malta at 1260/km-sq., for (int)(10*0.078) = 0 points
Next is Guernsey (UK) 830/km-sq.
See
here for complete data.