



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The solutions to the midterm exam for the cgs-3460 fall 2008 class taught by manuel e. Bermudez. Problem-solving steps for various c programming questions, covering topics such as expressions, arrays, control structures, functions, and unix commands.
Typology: Exams
1 / 6
This page cannot be seen from the preview
Don't miss anything!




Name: _____________________________________
UFID:__________________
Problem 1 (20 points). Answer the following questions, briefly.
(a) What is the value of the following expression in C?
('a' < 'b')? 1 : 2;
1
(b) Is the following a valid initialization of the array arr? If yes, indicate the value of each element in arr. Otherwise indicate why this is invalid.
int arr[3] = {2, 3};
(c) Write a printf statement to print “Hello” (including the double quotes), not just Hello.
printf(“\”Hello\””);
(d) Is the control expression in the following statement valid in C? Explain why or why not. if(a = 1 && b = 2) a=3;
It is invalid since logical operators have higher precedence i.e (a = 1 && b = 2) is equivalent to (a = (1 && b) = 2); which in turn is (a = 1 = 2); compiler wouldn’t compile the program due to an invalid lhs value.
(e) What is the value of the control expression in the following statement?
if((25 && 30) && -1) a=3;
true, since all non-zero values evaluate to true i.e.: ((true && true) && true)
(f) Placing a new-line character at the end of a scanf format string is usually a bad idea. Why? scanf("%d\n", &i);
scanf will continue reading until it encounters a non-whitespace character.
Problem 2 (30 points). Write the exact output generated by the following C program. Indicate any spaces and new lines clearly.
#include <stdio.h>
void triangle(char c){ int i,j; for(i = 0; i<4; i++){ for(j=4-i;j>0;j--){ printf("%c",c); } printf("\n"); } }
char xOrY(int number){
if(number %2 == 0){ return 'X'; }
return 'Y'; }
int main(){ triangle('*'); triangle(xOrY(99)); triangle(xOrY(100)); return 0;
}
Output:
**
YYYY YYY YY Y XXXX XXX XX X**
Problem 3 (30 points). Find errors (either syntax errors, or errors that will cause bad run- time behavior) in the following C program. Indicate the necessary correction for each error.
#include
if(k) printf("Hello world");
for(int i=0, j=10;i<5&&j>0;i++,j--){
printf("%d--%d",i,j); }
do { { printf("%d\n",n); n-- n--; } }while(n>0) while(n>0);
return 0 return 0;
}
Line 11: for(int i=0, j=10;i<5&&j>0;i++,j--){
There is a problem in this expression because i cannot be declared inside a for statement and j has not been declared previously. But, we have not deducted any marks for this problem. The main idea of this for statement was to check if the students are familiar with multiple variable initialization and increment in a for statement separated by commas. The correct expression should be:
int i,j; for(i=0, j=10;i<5&&j>0;i++,j--)