GNG 1106 Fall 2014 Final Exam (English)
GNG 1106 Fall 2014 Final Exam (English)
GNG 1106 Fall 2014 Final Exam (English)
Closed-book
180 minutes
The paper is out of 100 points, but you can get up to 108 points. That is, there are additional 8 bonus
points.
Question 5 The following code segments all intend to use variable x to specify a string hello. Verify if
they are correct.
1. char x [256]=" hello ";
2. char x [256];
x =" hello ";
3. char * x =" hello ";
4. char * x ;
x =" hello ";
5. char x [256]={ h , e , l , l , o };
Question 6 What does the following program print?
# include < stdio .h >
int getIt ()
{
static int a =2;
static int b =1;
int out ;
out = a + b ;
a=b;
b = out ;
return out ;
}
int main ()
{
printf ("% d \ n " , getIt ());
printf ("% d \ n " , getIt ()+ getIt ());
printf ("% d \ n " , getIt ()+ getIt ()/4);
return 0;
}
Question 7 Determine if the program below is correct. If it is correct, explain what it prints; otherwise,
explain why it is wrong.
# include < stdio .h >
int main ()
{
int i ;
int a [5]={1 , 2 , 3 , 4 , 5};
int b [5]={6 , 7 , 8 , 9 , 10};
b=a;
for ( i =0; i <5; i ++)
printf ("% d \ n " , b [ i ]);
return 0;
}
4
Question 8 Determine if the program below is correct. If it is correct, explain what it prints; otherwise,
explain why it is wrong.
# include < stdio .h >
int main ()
{
int i ;
int a [5]={1 , 2 , 3 , 4 , 5};
int b [5]={6 , 7 , 8 , 9 , 10};
* b =* a ;
for ( i =0; i <5; i ++)
printf ("% d \ n " , b [ i ]);
return 0;
}
Question 9 Determine if the program below is correct. If it is correct, explain what it does (by providing
example input to the program as well as its corresponding output); otherwise, explain why it is wrong.
# include < stdio .h >
int * getThreeNumbers ()
{
int i ;
int a [3];
int * p ;
for ( i =0; i <3; i ++)
{
printf (" enter an integer \ n ");
scanf ("% d " , a + i );
}
p=a;
return p ;
}
int main ()
{
int * q ;
int i ;
q = getThreeNumbers ();
printf (" the number you entered are :\ n ");
for ( i =0; i <3; i ++)
printf ("% d \ n " , q [ i ]);
return 0;
}
Question 13 Just like decimal numbers are represented using 10 different symbols (i.e., {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}),
hexadecimal numbers are represented using 16 different symbols. These 16 symbols are
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f }.
The numerical (decimal) values of the 16 symbols are given in the table below.
Numerical Value
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Hexadecimal Symbol
0
1
2
3
4
5
6
7
8
9
a
b
c
d
e
f
For any positive integer K, an integer number x between 0 and 16K 1 can be converted to a K-symbol
hexadecimal representation. We explain this conversion rule by taking the example of K = 4 (and hence x
is between 0 and 65535 = 164 1).
1. Express x as x = y3 163 + y2 162 + y1 16 + y0 , where y0 , y1 , y2 , y3 are all numbers in {0, 1, . . . , 15}.
Notice that for every integer x between 0 and 65535, such an expression is unique.
2. Convert the ordered tuple (y3 , y2 , y1 , y0 ) symbol-by-symbol to hexadecimal symbols using the above table.
Example 1: x = 15678. Express x as x = 3 163 + 13 162 + 3 16 + 14. That is, y3 = 3, y2 = 13, y1 =
3, y0 = 14. The (y3 , y2 , y1 , y0 ) tuple is then (3, 13, 3, 14). Converting this tuple to hexadecimal symbols
gives us string 3d3e
Example 2: x = 3258. Express x as x = 0 163 + 12 162 + 11 16 + 10. That is, y3 = 0, y2 = 12, y1 =
11, y0 = 10. The (y3 , y2 , y1 , y0 ) tuple is then (0, 12, 11, 10). Converting this tuple to hexadecimal symbols
gives us string 0cba
Clearly the above rule extends to arbitrary positive integer K. That is, to convert an integer x between 0
and 16K 1 to a K-symbol representation, we only need to express x as
x = yK1 16K1 + yK2 16K2 + . . . + y2 162 + y1 161 + y0 .
Then we convert the tuple (yK1 , yK2 , . . . , y2 , y1 , y0 ) to hexadecimal symbols according to the above table.
The following program is meant to do the following: it asks the user to enter a non-negative integer x
and a positive integer K; if x is between 0 and 16K 1, it prints the K-symbol hexadecimal representation
of the integer, otherwise it prints message x is out of range. You are required to complete the program
by designing and implementing the function printHex called in the main function.