Docsity

Quiz Solutions for CMSC 106, Fall 2007, 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. [18 pts.] Write a function which would be used with the following main so that the character passed
as the first parameter is printed the number of times indicated by the second parameter. If the
integer passed is non-positive, the function should do nothing.
#include <stdio.h>
void writechars(char c, int i){
int cnt;
if (i > 0){
for(cnt = 0; cnt < i; cnt++){
printf("%c", c);
}
printf("\n");
}
return;
}
int main(){
writechars(’a’,4); //output would be aaaa
writechars(’b’,3); //output would be bbb
writechars(’x’,6); //output would be xxxxxx
return 0;
}
(over )
pf2

Partial preview of the text

Download Quiz Solutions for CMSC 106, Fall 2007 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. [18 pts.] Write a function which would be used with the following main so that the character passed as the first parameter is printed the number of times indicated by the second parameter. If the integer passed is non-positive, the function should do nothing.

#include <stdio.h>

void writechars(char c, int i){ int cnt; if (i > 0){ for(cnt = 0; cnt < i; cnt++){ printf("%c", c); } printf("\n"); } return; }

int main(){ writechars(’a’,4); //output would be aaaa writechars(’b’,3); //output would be bbb writechars(’x’,6); //output would be xxxxxx return 0; }

(over )

  1. [11 pts.] Write a function that takes in no parameters but prompts for and reads integers (one at a time) from the user until a value between 1 and 9 (inclusive) is given. The single digit integer is then returned to the caller of the program.

It is called like this: int retvalue; retvalue = getOneDigit();

It looks like this when run: Give a single integer: 12 Error - Give a single integer: - Error - Give a single integer: - Error - Give a single Integer: 4

int getVal(){ int i; do{ printf("Give a single Intgeer:"); scanf("%d", &i); if(i > 9 || i < 1){ printf("Error - "); } }while(i > 9 || i < 1); return i; }

  1. [11 pts.] Tell the output of the following C code:

#include <stdio.h> void f1(int a, int b){ int x = 99; a = x - b++; printf("f1: %d, %d, %d\n",a,b,x); return; } int f2(){ int x = 5, y = 9; return x * y; } int main(){ int x = 1, y = 2, z = 3; f1(x,y); x = f2(); printf("main: %d, %d, and %d\n",x,y,z); }

f1: 97, 3, 99 main: 45, 2, and 3