101Final
101Final
00011101.11100001
2) Here is a decimal number that has a decimal point. Please write this number as a binary number using a
binary point. (I do not want the IEEE version.) Your answer should have exactly 8 bits to the left of the binary
point and 8 bits to the right of the binary point.
29.341
3) Suppose you are given 10 bit positions. How many different numbers can you represent if you only have two
symbols ( 0 and a 1 ) to fill each position?
____________________
5) A call to the scanf function is an expression. It reports out a value. Please tell me what is returned by a call to
the scanf function. Please tell me both typical possibilities. I will get you started.
6) What is the value of the following expression. A decription of the value will do.
7) The following is an expression. It reports out a value. That value has type. What is the type of the value
reported out by the following expression?
9) I give you 9 bits. What is the decimal equivalent of the smallest number that can be represented using a twos
complement strategy?
10) Assume that a struct is passed as an argument to a function foo. Is a copy of the whole struct made and
pushed onto the run-time stack or is a copy of the address of the struct passed?
For example,
foo ( x ) ;
11) I have taught you a specific way to phrase the type of an object when it is presented in a declarator. I call
the technique the Right Left Walk Method. Use the right left walk method precisely to tell me X’s type in the
declarator given below:
double * (*X) ( int a ) ;
12) Does the declarator in question 11 above reserve space? Yes or No? ________________
13) Types convey two fundamental pieces of information. What are they?
14) How many arguments are being passed to the function foo below?
foo ( (1, 2, 3, 4, 5 ) );
16) The following declarator does three fundamental things. What are they?
int x ;
a) ______________________________________________________________________
b) _______________________________________________________________________
c) _______________________________________________________________________
17) The following call to malloc will do two fundamental things. What are they?
malloc ( 10 ) ;
a) ________________________________________________________________________
b) ________________________________________________________________________
18) In particular, the following call will NOT do two fundamental things. What are they?
malloc ( 10 ) ;
a) ________________________________________________________________________
b) ________________________________________________________________________
19) The purpose of the atoi function is to convert a string number into its twos complement integer
representation. What function would you use to do the opposite? That is, to convert a two’s complement
number into its string represenation? Say for example, you had the decimal value 143 stored in a variable
named x. You also had a definition of a character array S. How would you convert the integer 143 into a string
representation? DO NOT USE a call to the non-standard itoa . Do not write your own function.
int x = 143 ;
char S[100] ;
________________________________________________________
20) Are you ok with this? Is it ok? Or is it wrong? If wrong, please explain.
char s[50] ;
21) Assume there are no compile errors in the following function definition. Does the function look ok to you?
If ok, say ok, otherwise tell me what trap the programmer could be falling into?
int i ;
return ( A ) ;
}
x -> next ;
int A[3][4][5] ;
A[1][3]
25) Assume that a machine uses 2 bytes to represent an integer and 4 bytes to represent a pointer. How many
bytes of space are reserved by the following declarator?
26) Assume that a machine uses 2 bytes to represent an integer and 4 bytes to represent a pointer. How many
bytes of space are reserved by the following declarator?
27) Is this ok? Or is this wrong? If wrong, please explain. You may assume that strcpy has been declared.
char *s = “hello” ;
char *t ;
strcpy( t, s );
int A[3] = { 1, 2, 3 } ;
int B[3] ;
B=A;
30) Suppose you want to pass a two-dimensional array x to a function named foo. Assume the declarator for x is
int x[5][10]. Is the following call to function foo ok? Explain.
foo ( x[][10] )
31) Assume that a machine uses 1 byte to represent a character and 4 bytes to represent a pointer. How many
bytes of space are reserved by the following declarator?
___________________________________________________________________________________
33) The definition of the C programming language does not include the type referred to as a string. To us, what
is the definition of a string in C. I will help get you started.
A string is ….
34) The function getc , short for get character, is a standard c function. As found in appendix D, the declaration
only for getc is
int getc ( FILE *file_pointer) ;
If the purpose of the function is to get a character why isn’t the declaration written as
35) It is important that you understand how objects in memory are represented. Here is a definition declarator
with an initializer. The machine the program will run on uses two bytes to store a twos complement integer.
int x = - 43 ;
Show me x’s internal representation. That it, show me the 1s and 0s.
36) In C, what is the purpose of the . operator?
38) When writing a C program the * symbol can be used three different ways. One of those ways is within a
declarator. When you see the * symbol in a declarator what phrase should come to you mind?
____________________________________________________________
39) When writing a C program the * symbol, used within an executable statement, has two different meanings.
One of the meanings is “multiplication operator”. For example, 5 * 8 . What is the other meaning? Explain.
40) Here is a program fragment. Does it look good to you? Or does it look bad to you? If it looks like that there
could be trouble, please explain.
char *s ;
s[0] = ‘B’ ;
s[1] = ‘I’ ;
s[2] = ‘L’ ;
s[3] = ‘L’ ;
s[4] = ‘\0’ ;
Name: _________________________________ 91.101 Computing I Final
Fall 2007 Part II – Short Answer, Templates, and Code Fragments
Take the next 5 weeks to improve your scholarship, to increase your confidence, and to prepare for the
academic challenges that lie ahead of you. Your current career path is to become a terrific scholar. You can.
int n, sum = 0 ;
while ( ){
sum = sum + n ;
2) Show me that you know how to define a file pointer and that you know how to open a file whose name has
been given on the command line as argv[1]. You should show this to me by completing the code below. You
should include anything you need to. You should not include anything you do not need to include. The program
should read in one integer value from the file and then print this value out. You should close the file.
return 0 ;
}
3) Please complete the following function called JOIN. The purpose of the function join is to return an array
that contains all of the integers in the array X followed by the integers in array Y.
int A[4] = { 1, 2 , 3, 4 } ;
return 0 ;
}
5) Can you call and write a function called swap? Show me by completing this code. I do not want to see any
arrays or usage of the array index operator.
swap ( ) ;
int x = 6 , y = 4 ;
swap ( ) ;
return 0 ;
swap ( ) {
}
6) A program’s command line contains a number of integer values. For example, when it is launched it could
look like: % a.out 5 8 12 44 6 4 511 23 . Or it could be, for example: % a.out 34 5 277 .
Complete the following program that will read in these command line arguments and output their sum.
#include <stdio.h>
int main ( int argc, char *argv[] ) {
return 0 ;
}
7) Here are three declarators. Study them and then doodle a clear picture of what space being reserved looks
like. Use pointer arrows where obvious. This is a terrific problem. If you can do this you’ve got it. Have fun.
char **b[] = { a + 3 , a + 2 , a + 1 , a } ;
char ***c = b
8) Using the declarators in problem 7 above, what is printed by the following. Again, do not waste time if you
do not get it. I wanted to give a question that could challenge some of the stronger students.
int size = 23 ;
int a = 4, b = 6, c = 14 ;
int i ;
A[2][1] = 511 ;
return 23 ;
int B[4] ;
int n = 32 ;
n = n + roger ( B , 4 )
return 0 ;
}
Problem 9
Name Addr Content
4032
Code Area
4036
4040
4044
4048
Global and Static Data Area
4052
4056
4060
4064
4068
4072
4076
4080
Heap
4084
4088
4092
4000
4096
4004
4100
4008
4104
4012
4108
4016
4112
4020
4116
4024
4120
4028
Run Time Stack
10) Found below is a small program. The program does not do anything useful. On the next page is a blank
memory template for this problem. Fill out the memory template using the conventions we used in class up to
the point of the arrow. Be clear. Be complete. Show me that you know how memory is changed and updated. If
I have made a typo, then fix the problem and then complete the problem. Do not bail on the problem if a typo
exists.
int i = 4 ;
count = count + 1 ;
if ( n >= 15 ) return ( 5 ) ;
return ( mystery ( n * 2 ) + 7 ) ;
int i = 1 ;
int a ;
a = mystery ( i ) ;
return 0 ;
}
Problem 10
4032
4040
4044
4052
4056
4060
4064
4068
4072
4076
4080
Heap
4084
4088
4000
4092
4004
4096
4008
4100
4012
4104
4016
4108
4020
4112
4024
4116
4028
4120
There have been some easy questions and there have been some hard ones too.
Do your best.
1) Write a complete C program that will prompt the user to enter an integer value, say n, between 1 and 50.
Your program should read in the value. Then your program will print out a single line containing n asterisks.
The asterisks is the * symbol.
2) Twin primes are two prime numbers that differ by 2. For example, 3 and 5 are twin primes, so are 101 and
103. Write a program that prints all twin primes less than 3000.
You do not need to write the definition of the IsPrime function. You may assume its declaration is found in
Isprime.h. The declaration is
and the function will return a 1 if the number x is prime and 0 if the number x is not prime. You may call this
function from your main routine. DO NOT WRITE the IsPrime function. You may call it.
3) Write a complete C program that will compute the inner product of two vectors. Each vector contains exactly
8 floating point values. Your program will enter these values in from the standard input. Once these values have
been read in, your program should call a function that will compute the inner product of these two vectors. The
function should return the value back to main. The inner product of vector U = (5, 1, 6, 2 ) and vector V = ( 1,
2, 3, 4 ) is 33 because
5*1=5
1*2=2
6 * 3 = 18
2*4=8
and 5 + 2 + 18 + 8 is 33.
4) This is not a coding question, but more or less a check on a coding question that I asked you to do. What
is the persistence of each of the following numbers:
417 _____________
1000 ___________
6 _____________
715 ____________
That is, the first three terms are each 1 and then each subsequent term is the sum of the previous three terms.
Write a complete C program that inputs an integer value from the command line as argv[1], say n. Then it will
print out the nth term in the ORDER 3 fibonacci sequence. Your program must call a RECURSIVE function to
do the job. Your program should print out the value of the nth term. The sequence starts with the first term (1).
The sequence does not start with the zeroth (0). The first term is 1. The second term is 1.
(I wish I could give you more coding problems to do, but it is already too much grading.)