Question 11
Memory & Data Representation in C
(a) Explain the types of values that each of the following C data types store, your answer must explain the advantage provided by each type: char, un- signed int, long int, float, double. [5 marks]
(b) Explain how C represents strings. [2 marks]
(c) Write C code that reads in two strings of up to 10 characters and prints out the longer of the two strings.
For example, the output of a run of the program might look like:
Enter word 1: hello
Enter word 2: universe
The longer word is universe
(d) The following C function takes two doubles: min and max. The function should update the passed values, placing the smallest of the two values in min and the largest in max. It does not currently work correctly.
void order(double min, double max) {
if (max < min) {
double temp = min;
min = max;
max = temp;
}
}
i. What will the following code output using the version of the order func- tion given above?
int main(void) {
double num1 = 6;
double num2 = 5;
order(num1, num2);
printf("small %lf, large %lf \n", num1, num2);
return 0;
}
ii. Write the correct C code for the function so that it behaves as expected. [5 marks]
iii. Correct the call in main to the order function. [2 marks]
(e) Write a C function, identity, that takes two arguments: rows, cols and creates a new rows x cols identity matrix. An identity matrix is a matrix with ones on the main diagonal and zeros elsewhere. [13 marks]
[Total for Question 11: 33 marks ]