


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



CPS 196 Lab 12 Fall 2007
I. Writing a program fragment with an array loop.
Suppose that you have a program with an array called values declared:
int values[30];
and assume that all 30 elements of the array have been assigned values.
How many elements does the array have?
What is the type of each element?
Write in the space below (by HAND, not using Visual Studio) a program fragment that has a loop that iterates over all the elements of this array and adds one to each value. If you need to use any additional variables, show their declaration. When you are done, check with me or with one of your lab neighbors to see if it is correct.
II. More on using arrays. a) The Fibonacci numbers are a series of numbers defined as follows: the first two Fibonacci numbers are 0 and 1. Each of the remaining numbers is the sum of the two Fibonacci numbers preceding it. Here is a program to compute Fibonacci numbers and store them in an array.
/* fibonacci.c / / This program computes Fibonacci numbers up to 30 / #include <stdio.h> int main() { int i; / loop index variable / int numFibs; / number of Fibonacci numbers requested / int Fibonacci[30]; / array to hold Fibonacci numbers up to 30 / int newFib; / variable to hold the new Fibonacci number */
/* get the input number and check that it is in the specified range */ printf("How many Fibonacci numbers do you want (from 2 up to 30)? "); scanf("%i", &numFibs);
if ((numFibs < 2) || (numFibs > 30)) { printf("Sorry, that number is out of range.\n"); return 0; }
/* assign the first two numbers of the series to the array Fibonacci[0] = 0; Fibonacci[1] = 1;
/* loop over the Fibonacci numbers from 2 on up / for ( i = 2; i < numFibs; i++) { / compute the next Fibonacci term and assign it to the array */ newFib = Fibonacci[i-2] + Fibonacci[i-1]; Fibonacci[i] = newFib; }
/* print the results */ for ( i = 0; i < numFibs; i++) printf("%i\n", Fibonacci[i]);
return(0); }
III. Using double loops in an array program.
Open a new item in your project and put into it the program ratings.c that was discussed in the lecture. You can copy/paste it from the Lecture/Labs page.
Modify this program to display the output in a histogram. Now many histograms are displayed with vertical bars to show the value of the variables, but since we only have line output it will be much easier if we show our histogram in a horizontal direction, that is, sideways on the page, with the number of stars showing the value of the each array element.
For example, if our ratingCounters array had only 5 counters where we now display:
Rating Number of Responses
1 1 2 3 3 2 4 4 5 5
Change the output portion of the program to display instead:
1 | * 2 | *** 3 | ** 4 | **** 5 | *****
Hint: In the output loop, write another inner loop to print each of the stars up to the value of the array element.