Docsity

Quiz 5 with Questions - Computer Programming | CMSC 106, Quizzes of Computer Science

Toggle sidebar
bg1
Name (printed):
Student ID #:
Section # (or TA’s:
name and time)
CMSC 106 Quiz #5 Fall 2007
1. [20 pnts] Write a function that takes in a single integer and writes the table with that integer on the
diagonal. You may assume the integer coming in is greater than or equal to 2. The program may
use neither the while nor the do-while structure. (hint: it must use the for loop structure). Don’t
just write a printf like printf("3 4 5 \n")).
The output, when a 3 is passed in, is: |The output, when a 5 is passed in, is:
|
345 |56789
234 |45678
123 |34567
|23456
|12345
pf2

Partial preview of the text

Download Quiz 5 with Questions - Computer Programming | CMSC 106 and more Quizzes Computer Science in PDF only on Docsity!

Name (printed):

Student ID #:

Section # (or TA’s: name and time)

CMSC 106 Quiz #5 Fall 2007

  1. [20 pnts] Write a function that takes in a single integer and writes the table with that integer on the diagonal. You may assume the integer coming in is greater than or equal to 2. The program may use neither the while nor the do-while structure. (hint: it must use the for loop structure). Don’t just write a printf like printf("3 4 5 \n")).

The output, when a 3 is passed in, is: |The output, when a 5 is passed in, is: | 3 4 5 | 5 6 7 8 9 2 3 4 | 4 5 6 7 8 1 2 3 | 3 4 5 6 7 | 2 3 4 5 6 | 1 2 3 4 5

  1. [10 pnts] Write the output of the following code. You do not need to worry about spaces - just make sure the correct values are on the correct lines.

#include <stdio.h>

int f1(int i){ int j = 5, k = 0; for (j = 10; j > i; j--){ k+=i; } return k; } int f2(int i){ int j, sum=i; for (j = 1; j < i; j++){ printf("%d: sum = %d\n",j,sum); sum +=i; } return sum; } int main(){ printf("%d\n",f1(3)); printf("%d\n",f2(5)); return 0; }

_________________________________________________________________________

_________________________________________________________________________

_________________________________________________________________________

_________________________________________________________________________

_________________________________________________________________________

_________________________________________________________________________

_________________________________________________________________________

_________________________________________________________________________