- 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 };
}
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. */
}
- 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 */
for (;;) { // infinite loop, terminated by break inside
scanf("%i", &score);
if (score == -1) break;
s[n] = score;
n++;
}
/* rest of the code here */
}
- 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?
- 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 .
- 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 main() {
int x[3][4] = { { 10, 12, 14, 16},
{ 22, 24, 26, 28},
{ 34, 36, 38, 40} };
/* code for part c below */
/* code for part d below */
/* call to the function rowsum */
printf("Sum of row 2 = %i\n", rowsum(2, x));
}
/* code for the function rowsum below */
- How many rows does the array x have?
What is the length of each row?
- 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 ?
- 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) .