Odd mask of a positive integer number N is also a positive integer

advertisement
1
MIDDLE EAST TECHNICAL UNIVERSITY
Department of Computer Engineering
CEng 230: C Language
2002 Summer
Final - GROUP A
First Name
Question I:
1.
Last Name
ID
120 min.
Section
Grade
Multiple-Choice (5x10=50 pts.; four wrong answers will cancel one correct answer!)
What is the output of the following code fragment?
int f(int *a, int b){
*a = b * 10;
b = *a – 10;
return b;
}
void g(int *a){
*a = f(a, *a);
}
int main(void){
int a = 10;
g(&a);
printf(“%d”, a);
return 0;
}
a) 10
b) 90
2.
c)
d)
100
0
What is the value of strlen(s1) after the following code fragment is executed?
char s1[10] = “ali”;
char s2[] = “veli”;
strcat(s1, s2);
a) 6
b) 7
c)
d)
8
9
3.
a)
48
b)
0
c)
24
d)
12
4.
What is the output of the following code fragment?
a = 0;
if( a = 3 )
for(i=0; i<3; i++, a+=a);
else
do a += a*127 % 2 + 1;
while(a % 4 == 0);
printf(“%d”, a);
What is the output of the following code fragment? (b stands for the blank space)
a = 1245;
str[0]=’1’, str[1]=’2’, str[2]=’4’, str[3]=’5’, str[4]=’\0’;
printf(“%5d_%5s”, a, str);
a)
b)
5.
b1245_1245
1245_b1245
c)
d)
1245_1245
b1245_b1245
Which of the following does not have any errors in it?
a) char a[10], b[10];
c)
b)
d)
……
a = b;
char a[10], b[10];
……
a[0] = b[9];
char a[10], b[10];
……
a[] = b[];
char a[10], b[10];
……
a[10] += b[10];
2
6.
What is the output of the segment below?
for(i=-13,j=13; i++<0 && --j>0;)
k = i*j;
printf(“%d %d %d”,i,j,k);
7.
a)
-13 13 169
b)
000
c)
0 0 -1
d)
101
What is the output of the following code fragment ? (Note: assume that reverse is a user-defined function that
takes the reverse of its argument and returns it through the argument. Ex: reverse of “alideli” is “iledila”)
char s1[SIZE], s2[SIZE], s3[SIZE], s4[SIZE];
int a;
… … …
/*assume that s1 & s2 are assigned valid strings here */
… … …
strcpy(s3, s1);
strcat(s3, s2);
strcpy(s4, s2);
strcat(s4, s1);
reverse(s4);
printf(“%d”, strcmp(s3, s4));
a) A number which depends on the values of s1 and s2
b) Always 0
c)
d)
A positive number
A negative number
a) if(x)
c)
if(x) s=x=x+1; else s=x--;
if(y) s=++y;
b) if(x)
d)
if(y)
if(x)
s=++x;
else s=y++;
else s=x--;
9.
a)
10 100
b)
00
c)
0 10
d)
100 10
8.
Which one is an equivalent of the following segment?
s = x ? y ? ++x : y++ : x--;
if(y) s=x++; else s=y++;
else s=x--;
if(y) s=x=x+1;
else y=(s=y)+1;
else x=(s=x)-1;
What will be the output of the following segment?
void f(int *b, int a)
{ *b = a * a;
a += *b;
}
int main(void)
{ int a, b;
a = 10, b = 0;
f(&a, b);
printf(“%d %d”, a, b);
return 0;
}
10.
Which one of the following is wrong?
a)
There is no string data in C; a string is implemented
just using character arrays/sequences.
A compiler converts a high-level programming
language code into low-level programming language
code.
b)
c)
10 is true but -10 is false.
d)
Every character has a corresponding integer value.
3
Question II:
Error Correction (10 pts.)
The following code includes 10 errors. The program should read a string from the user and convert the number in the string
into an integer number. Ex: The number in “1234” (which is a string) is 1234 (which is an integer). Note that 1234 =
4*1+3*10+2*100+1*1000. Find and correct logical and syntactic errors so that the program performs its job.
#include<stdio.h>
int
for
[]
#include<string.h>
/* str_to_number: returns the integer equivalent of its
string argument */
void str_to_number(char str{}){
integer number, mult = 1, len, i;
=
len = strlen(str);
while(i = len-1, number == 0; i >= 0; i--, mult *= 10)
number += (str[i]-'0')*mult;
return number;
}
/* main: reads the string, passes it to str_to_number
Function and prints the result */
int main(vod) {
void
char str[20]; int number;
print("Enter the number:");
str
scanf("%s", str);
%d
number = str_to_number(&str);
printf("The result of conversion is: %s\n", number);
return;
}
Question III:
return 0;
Trace (10 pts.)
#include <stdio.h>
#include <string.h>
int x=0;
void F () { int x = 1; }
void G () { x = 2;
}
void H (int x) { x = 3; }
void S (){
int i;
char s1[] = "bcdef",
s2[] = "12345", s3[32];
printf("%s %s\n", s1, s2);
for(i=0; i<5; i++){
s3[4-i] = s1[i];
}
s3[5] = '\0';
printf("%s\n", s3);
for(i=0; i<strlen(s1); i++){
s3[2*i] = s1[i];
s3[2*i+1] = s2[i];
}
s3[10] = '\0';
printf("%s\n", s3);
}
void main() {
printf("%d\n", x);
F();
printf("%d\n", x);
x=0; G();
printf("%d\n", x);
x=0; H(x);
printf("%d\n", x);
x=0; {
int x = 4;
printf("%d\n", x);
}
printf("%d\n", x);
S();
}
Answer :
0
0
2
0
4
0
bcdef 12345
fedcb
b1c2d3e4f5
4
Question IV:
Coding (30 pts.; you are expected to use the constructs covered in the class only!)
In this program, you will read as input, the student IDs and final grades of ceng230 class. You will first sort and print
according to student IDs, then sort and print according to grades. You may assume that there are at most 100 students in the
class. The main function is given to you, and you are expected to complete other necessary functions whose headers are given.
See the comments in the code-section for further instructions.
Sample run:
program prints
user enters
user enters
user enters
user enters
user enters
program prints
program prints
program prints
program prints
program prints
program prints
program prints
program prints
program prints
program prints
program prints
program prints
program prints
program prints
Enter student ID and grade (-1 -1 to end) :
1308945 87
1309776 23
1292031 94
1300000 17
-1 -1
4 students.
average is: 55.25
Sorted according to studentIDs in ascending order:
1292031 94
1300000 17
1308945 87
1309776 23
Sorted according to grades in descending order:
1292031 94
1308945 87
1309776 23
1300000 17
Answer (fill in the boxes, only):
#include <stdio.h>
/* (10 pts.) reads the student IDs and grades. Prints the number of students and the average grade. Returns number of students
in class. */
int ReadIDsGrades(int IDs[],int grades[]) {
int i=0, sum=0;
printf("Enter student ID and grade (-1 -1 to end) :\n");
while(1){
scanf("%d %d", &IDs[i], &grades[i]);
if(IDs[i] == -1 && grades[i] == -1)
break;
sum += grades[i];
i++;
}
printf("%d students.\n", i);
printf("average is: %.2f\n", (float)sum/i);
return i;
}
5
/* (5 pts.) Prints the IDs and grades arrays, as shown in the samle run above..*/
void PrintIDsGrades(int IDs[],int grades[], int S) {
int i;
for(i=0; i<S; i++)
printf("%d %d\n", IDs[i], grades[i]);
}
/* (15 pts.) Sorts the arrays according to X. "order" defines the sort-order: -1 for descending, 1 for ascending.
S is the size of the arrays. You may use any algorithm you'd like, but here's the pseudocode for the selection-sort algorithm:
from i=0 to i=S-2 {
Find the location of the minimum (maximum if descending order) number, among the numbers from arr[i] to arr[S-1].
Swap arr[i] & arr[min]
}
*/
void Sort(int X[],int Y[], int S, int order) {
int i,j,t, minmax;
for(i=0; i<S-1; i++){
minmax = i;
for(j=i+1; j<S; j++)
if(order == 1 ? X[j] < X[minmax] : X[j] > X[minmax])
minmax = j;
t = X[i]; X[i] = X[minmax]; X[minmax] = t;
t = Y[i]; Y[i] = Y[minmax]; Y[minmax] = t;
}
}
int main(void) {
int N, IDs[100], grades[100];
N = ReadIDsGrades(IDs, grades);
Sort(IDs, grades, N, 1);
printf("\nSorted according to studentIDs in ascending order:\n");
PrintIDsGrades(IDs, grades, N);
Sort(grades, IDs, N, -1);
printf("\nSorted according to grades in descending order:\n");
PrintIDsGrades(IDs, grades, N);
return 0;
}
Download