Docsity

Exam 2 - 4 Problems on Introduction to Computer Program II | CS 1713, Exams of Computer Science

Toggle sidebar
bg1
CS 1713, Intro. to CS, Exam 2, Tue Feb 17 1998, Page 1
CS 1713, Intro. to Computer Science, Exam 2
(25) 1. Write a simple loop in C that will keep reading pairs of integers as long as they are not both
negative integers. As soon as two negative integers are read, the loop should terminate.
(25) 2. Write a complete C program that will scan in an integer n, and then will print a hollow
triangle of stars of height n, as shown below for n == 10 and n == 5. (Assume that
n > 1. The program needs to work for general n and not just 10 and 5.)
runner% exam2_2 runner% exam2_2
10 5
* *
** **
* * * *
* * * *
* * *****
* * runner%
* *
* *
* *
**********
If you wish, you may use the following function:
void chars(int n, char ch)
{
int i;
for(i = 0; i < n; i++)
printf("%c", ch);
}
Here, chars(5, ’*’) will print 5 stars with no newline.
(25) 3. Consider the following C program:
#include <stdio.h>
int addup(int n);
void main(void)
{
int n, sum;
scanf("%i", &n);
sum = addup(n);
printf("Sum of integers up to %i == %i\n", n, sum);
}
Supply code for the function addup, so that it will add the integers from 1 to n and return
the sum. Thus if n is 5, addup(n) should return 15. (You need a loop inside addup so
that it will work no matter what the value of n is.)
(25) 4. Write a complete C program that will use the random number generator drand48() to
roll a single die over and over again, keeping track of the number of times each of the
number of spots 1 through 6 appears. To keep track of the counts you should use an array
int spots[7];
of counters, where in case a 4 appears, you would increment the counter spots[4]. Have
your program do 1000 rolls. Don’t forget to initialize the counters. The program should
print the results, that is, the counts for each of the six spots and the percentage. Also don’t
forget to initialize the random number generator. Your output might look as follows:
runner% exam2_4
Roll of 1, 177 times, 17.70%
Roll of 2, 181 times, 18.10%
Roll of 3, 156 times, 15.60%
Roll of 4, 168 times, 16.80%
Roll of 5, 157 times, 15.70%
Roll of 6, 161 times, 16.10%

Partial preview of the text

Download Exam 2 - 4 Problems on Introduction to Computer Program II | CS 1713 and more Exams Computer Science in PDF only on Docsity!

CS 1713, Intro. to CS, Exam 2, Tue Feb 17 1998, Page 1

CS 1713, Intro. to Computer Science, Exam 2

(25) 1. Write a simple loop in C that will keep reading pairs of integers as long as they are not both negative integers. As soon as two negative integers are read, the loop should terminate.

(25) 2. Write a complete C program that will scan in an integer n, and then will print a hollow triangle of stars of height n, as shown below for n == 10 and n == 5. (Assume that n > 1. The program needs to work for general n and not just 10 and 5.) runner% exam2_2 runner% exam2_ 10 5





    • runner%

If you wish, you may use the following function: void chars(int n, char ch) { int i; for(i = 0; i < n; i++) printf("%c", ch); } Here, chars(5, ’*’) will print 5 stars with no newline.

(25) 3. Consider the following C program: #include <stdio.h> int addup(int n); void main(void) { int n, sum; scanf("%i", &n); sum = addup(n); printf("Sum of integers up to %i == %i\n", n, sum); } Supply code for the function addup, so that it will add the integers from 1 to n and return the sum. Thus if n is 5, addup(n) should return 15. (You need a loop inside addup so that it will work no matter what the value of n is.)

(25) 4. Write a complete C program that will use the random number generator drand48() to roll a single die over and over again, keeping track of the number of times each of the number of spots 1 through 6 appears. To keep track of the counts you should use an array int spots[7]; of counters, where in case a 4 appears, you would increment the counter spots[4]. Have your program do 1000 rolls. Don’t forget to initialize the counters. The program should print the results, that is, the counts for each of the six spots and the percentage. Also don’t forget to initialize the random number generator. Your output might look as follows: runner% exam2_ Roll of 1, 177 times, 17.70% Roll of 2, 181 times, 18.10% Roll of 3, 156 times, 15.60% Roll of 4, 168 times, 16.80% Roll of 5, 157 times, 15.70% Roll of 6, 161 times, 16.10%