Download CMPE 150

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Line (geometry) wikipedia , lookup

Collatz conjecture wikipedia , lookup

Addition wikipedia , lookup

Location arithmetic wikipedia , lookup

Positional notation wikipedia , lookup

Approximations of π wikipedia , lookup

Proofs of Fermat's little theorem wikipedia , lookup

Elementary mathematics wikipedia , lookup

Transcript
NAME:
CMPE 150, 152 C MIDTERM 1
22/03/2002
1
2
3
4
5
6
TOTAL
Name :
Department:
Signature :
Notes: a) NO QUESTIONS!!!
b) Do not use any nonstandard constructs.
c) Use only features covered in class.
d) Write your name on all sheets!
QUESTIONS
1) (25 points) Write a program that takes an integer consists of three digits (i.e. in [100,999])
from the user and prints the number of different digits on the screen. If the user enters invalid
numbers (not in [100,999]), you will display an error message and exit the program.
Enter a number: 10
It is not a three-digit integer...
Enter a number: 164
The different number of digits is 3...
Enter a number: 464
The different number of digits is 2...
Enter a number: 888
The different number of digits is 1...
#include <stdio.h>
int main(){
int n, first, second, third;
printf(“Enter a number: ”);
scanf(“%d”,&n);
if (n < 100 || n > 999){
printf(“It is not a three-digit number”);
return 0;
}
first = n / 100;
NAME:
second = (n % 100) / 10;
third = n % 10;
if (first == second && first == third)
printf(“The different number is 1\n”);
else if (first == second || first == third || second == third)
printf(“The different number is 2\n”);
else
printf(“The different number is 3\n”);
return 0;
}
2) (25 points) Write a program that takes an integer x and a sequence of integers from the user
and counts the number of integers divisible by x. Sequence of integers will end with a
negative value (You will not count the negative integer).
Sample
Enter an integer: 5
Enter the sequence (negative value indicates the end):
2 3 8 35 24 35 2 10 5 -10
The counter is 4...
int main(){
int n, counter = 0, temp;
printf(“Enter an integer: ”);
scanf(“%d”,&n);
printf(“
Enter the sequence (negative value indicates the end): ”);
scanf(“%d”,&temp);
while (temp >= 0){
if (temp % n == 0)
counter++;
scanf(“%d”,&n);
}
printf(“The counter is %d...\n”,counter);
return 0;
}
3) (15 points) Suppose you are working in a software firm as a programmer. Your project leader
gave you the following code that had been written before and wanted you to correct the code,
correct the code and state your reason. Note that the program may be working correctly but
still there are errors.
#include <stdio.h>
int main(){
unsigned int totalPeople;
int noPeople, totalRoom = 0;
double avgRoom;
char answer;
NAME:
totalPeople = -1;
/* 1) totalPeople is defined as an unsigned integer and negative value is assigned
to it, define it as signed integer, int totalPeople;*/
printf("Enter the number of the rooms to be reserved:");
scanf("%f",&noPeople);
/* 2) conversion specifier does not match with integer, correct it as
scanf("%d",&noPeople);*/
switch(noPeople){
case 2: printf("Do you want single or double bed? (S/D)");
scanf("%c",&answer);
totalPeople += 2; totalRoom++;
break;
case 3: totalPeople += 3; totalRoom++;
/* 3) break is forgotten, it causes the program to continue with default case */
default: printf("It is an invalid choice");
break;
case 4: printf("There is also suit for four people");
totalPeople += 4;
break;
totalRoom++; /* 4) this statement has no effect since it is after break*/
}
avgRoom = totalPeople / totalRoom;
/* 5) if totalRoom == 0, it gives overflow */
/* 6) this division gives int value, but avgRoom should be a decimal number*/
/* if (totalRoom != 0) avgRoom = (float)totalPeople / totalRoom;*/
if (avgRoom == 0.0)
/* 7) we should avoid to compare the equality of two decimal numbers */
/* if abs(avgRoom) < 0.0000000000000001*/
printf("All rooms are empty...\n");
return 0;
}
4) (15 points) Fill in the blanks as stated in comment blocks. It is not allowed to define extra
variables.
a) /* you should include the stdio library here */
#include <stdio.h>
b) /* define the name of days (Sunday, Monday, Tuesday....) as integer values from 1 to 7 by
using define or an enumerated type */
enum days{Sunday = 1, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday};
c) int main(){
int day;
printf("Enter the number of the day:");
scanf("%d",&day);
NAME:
/* control whether the given day is a weekday or weekend by using switch */
/* if you use the values for the days, you must use your definitions in (b)*/
switch(day){
case Sunday: case Saturday:
printf(“It is a weekday”);
break;
case Monday: case Tuesday: case Wednesday: case Thursday: case Friday:
printf(“It is a weekend”);
break;
default:
printf(“Invalid day number”);
break;
}
return 0;
}
5) (10 points) What is the result of each assignment?
int a = 3, b = 4;
double c = 1.0;
char d = 'F', e = '7';
c = a / 10;
d = d + '9' - e;
d++;
c = (double)(a / b);
c = a++;
c = 0.0
d = ‘H’
d = ‘I’
c = 0.0
c = 3.0 (a = 4)
6) (10 points)
Register A and B have some contents. Write a program to switch their contents.
D0
D1
D2 ...
A
B
D
C
L0
L1
L2
...
instruction set:
LOAD Lx, A
LOAD Lx, B
STORE Lx, C
STORE Lx, D
ADD
COMP
CON Lx, Dy
GOTO label
it has no solution, you can not switch the contents of the registers by using the above
instructions