The following are a set of questions to help you prepare for Exam 1. I recommend trying out these problems with paper and pencil. Remember that any material from the first four weeks is eligible, whether or not it appears in a problem below.
Topics these questions cover:
Consider the following snippet of C code. The function numDigits takes a non-negative integer as a parameter and returns the number of digits that number contains. For example:
#include <stdio.h>
#include <assert.h>
/* function numDigits
* preconditions:
*
*
* postconditions:
*
*
*/
int numDigits (int n){
// insert an assert statement below
// insert function details below
}//numDigits
int main(void){
// an example of calling numDigits
num = 10347;
int dig;
dig = numDigits(num); // I expect dig = 5
printf("num = %d has %d digits \n", num, dig);
return 0;
}//main
Fill in the documentation for preconditions, postconditions, and one assertion corresponding to your precondition.
Write the function numDigits which takes in a positive integer n as a parameter and returns the number of digits of n. Your function does not need to consider the case where n is not a positive integer. Hint: For a positive number, we can determine the number of digits by repeatedly dividing by 10.
Come up with a list of test cases to assess the accuracy of your solution. Describe whether your tests could be described as black box or white box and why.
Write a series of Linux commands to complete the following set of tasks:
Imagine that the below box is your terminal window on a Linux shell. Each appearance of the $ symbol indicates a command prompt. Hence, 5 appearences of $ for each of the 5 commands you’ve been asked to write.
shaw some-directory$
/home/user/some-directory
shaw some-directory$
shaw some-directory$
shaw ~$
csc151 csc161 test.c
shaw ~$
Consider the following C code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <stdio.h>
void fun1(int a[], int b){
for(int i = 0; i< b; i++){
a[i] = 0;
}
}
int fun2(int a, int b){
int squar = a*a;
return 0;
}
int fun3(int a, int b){
fun2(a,b);
b = fun2(b,a);
return b;
}
int main(void){
int a,b;
a = 1;
b = 7;
int c[5] = {2,4,6,8,10};
b = fun3(a,b);
fun1(c,5);
}
Write out a stack diagram for when the program counter is 17
Write out a stack diagram for when the program counter is 27
Write out a stack diagram for when the program counter is 28
Consider the following snippet of C code.
#include <stdio.h>
#include <stdbool.h>
/* A program that will read in an array of characters from the
* user, and then determine if every element of the array is the
* character equivalent to the index.
* For example:
* [0,1,2,3]
* [0]
* [0, 1]
* [0, 1, 2, 3, 4, 5, 6, 7, 8]
*/
int main(void){
int size;
bool validArray;
// Ask the user for an integer,
// save it in size,
// and create an array of characters of that size
// Ask the user to input the requested number of characters
// Then read them in
// Check whether the array is of the correct format
// Print out the result
if(validArray){
printf("The array was of the correct format");
}
else{
printf("The array was not of the correct format");
}
return 0;
}
Fill in the part where you prompt the user for an integer, and then read it in, and then create an array
Fill in the part where you prompt the user to enter characters, and read them in and save them to your array
Fill in the part where you determine whether the array is in the correct format. You can use the boolean validArray to keep track of your answer.
This question is about Integers and Floating Point numbers in C. In all cases, assume the IEEE standards for floating points that we learned in class. Also assume we have 8 bits of storage for integers.
How is 10 versus 10.0 stored in the computer?
Convert the decimal (base 10) 63.5 into binary (base 2).
Convert the binary (base 2) number 00111001 into decimal (base 10).
Convert the binary number from part c into it’s negative using two’s complement.