Homework Assignment 2
Homework Assignment 2
1. (25 points total) On slide 8 of lecture Week3_Class1, there’s code to convert Celsius (oC) to Fahrenheit
(oF) temperatures based on Figure 5.8 from the textbook. The code below converts from oF to oC.
a) (20 pts) Please fill in the printf below and circles/rows for the table by tracing program’s for loop.
Trace for loop execution fahrenheit celsius oop printf
Complete trace of for (enter line number) condition output
loop by completing
1 #include <stdio.h> the table and filling in
11(1) fahrenheit
2 empty circles with the
F I 42.0
#define 42.0 line # for each step
4 #define M T 2 True
5 #define ST 10
12(2) fahrenheit F IMIT 42 42.0 32
7
8
int main() {
float fahrenheit,
42 5.6
celsius; 1
(5) prin ( .1f F .1f C , 42.0 F
10 17
fahrenheit, celsius)
42 5.6 5. C
11 for (fahrenheit = ;
12 fahrenheit >= M T;
1 fahrenheit -= ST ) 32
14
15
{
celsius = (fahrenheit 2) 5.0 .0;
32 True 32>=32
1
17
printf("%.1f = %.1f \n",
___________, ___________);
15(4) c (f 32) 5.0 / 9.0 32 0.0
fahrenheit Celsius
18 }
1 32 0 32F=0 C
20 } end main
13(3) fahrenheit FST P 22.0
Please
Please fill in
complete
these arguments 22 False (22<32)
N
b) (5 points) Would (fahrenheit - 32) * 5 / 9 and 5 / 9 * (fahrenheit - 32) be equivalent? (Y or N): _____
No, because 5 / 9 performs integer division (resulting in 0), whereas (fahrenheit - 32) * 5 /
Explain (note fahrenheit is float, 5 is int, 5.0 is double): _______________________________________
9 correctly computes the conversion when at least one operand is a float or double.
2. (25 points total) Write a program that returns the smallest, largest, and running total (aka rolling total,
aka cumulative sum) array given quarterly transactions #define QTRS 4. This cumulative sum (cusum) at
each index is equal to the sum of all previous indexes plus itself, e.g. cusum of [4,3,2,1] is [4, 7, 9, 10].
1 OF 4
Student Name: ________________________________ Student ID: _______________________
c) (10 points) Write the function definition for void function qtrMinMaxRunningTotal. Try to answer
first without a compiler but then please check your answers by running the code!
const int qtr_trans[QTRS]
void qtrMinMaxRunningTotal(________________________, int *min_trans
_____________________,
int *max_trans int cusum[QTRS]
________________________, _____________________) {
d) (5 points) What will be the print output from main of the program in 2b on the Mason platform?
min=78, max=222, and total=525
3. (20 points total) The scope of a name is the region in a program where a particular meaning of a name
is visible or can be referenced. If an identifier is referenced outside of its scope, an undeclared symbol
syntax error will result. Scope of constant macros (e.g. #define I _ 80) begin at their definition and
continue to the end of the source file. A function name is visible from its declaration/prototype to the
end of the source file except within functions that have local variables of the same name (bad practice).
a) (10 points) What will be the print output values of the program on Mason platform? Test run code!
#include <stdio.h>
void pass yValue(int copy);
int main() {
int x = 2025;
pass yValue(x);
2025
printf("After pass yValue: x = %d\n", x); // Ans: _______________________________
if (x == 202 );
printf("Will this print? if();?\n"); // Answer (Y or N): ______ Y
return 0; f yes, why? otice ; after if();
} Hint: search empty statement in
void pass yValue(int x) { This only modifies the local x
Explain: ___________________________
x = 202 ; inside the function, not the x in
____________________________________
} ____________________________________
main.
b) (5 points) Does the code above in question 3a) compile on Mason platform?
No
Answer (Yes or No): _________. If not, what are the errors? ___________________________________
Does not compile due to incorrect function naming (pass yValue vs. pass_yValue),
an unnecessary semicolon after if (x == 202);, and typos in the return statement.
2 OF 4
Student Name: ________________________________ Student ID: _______________________
c) (5 points) The part of a program where an identifier can be referenced is called the ________ of the
identifier.
O driver
O scope
O stub
4. (15 points total) elow is example code reading strings line by line from a file and storing them in a
string array, which is a 2D array because each string is a char array. Please answer the following
questions to a) assign each line read from file to an element in the string array and b) use strcmp to
search for a target word. Week 4 class video may help. Question 4c has programming mistakes that may
lead to segmentation fault run time errors or unexpected results.
int log_no = 0;
char firewall_logs[ O _ ][ _ ], line[ _ ], status;
inp = fopen( AM , "r");
fclose(inp);
O i) strcpy(firewall_logs[log_no++], line);
O ii) firewall_logs[log_no++] = line;
O ither i) or ii) work
b) (5 points) If strings char target_word[STR_SIZE] and char log_token[STR_SIZE] are
equal, what value does strcmp(target_word, log_token) return? (5 points)
O i) 1
O ii) 0
O iii) 1
c) (5 points) Which of the following may cause a Segmentation fault (core dumped) run-time error?
O i) prin (“ s”, firewall_logs[0][5]) (i.e. passing char non pointer for s to prin )
O ii) firewall_logs[log_no++] //where log_no O _ , i.e. read more lines than dimension size
O iii) oth i) and ii) may cause a segmentation fault run time error
3 OF 4
Student Name: ________________________________ Student ID: _______________________
5. (15 points total) Strings in C are char arrays. When declaring a string, we usually use #define STR_SIZ ,
for example, to specify the length of the char array (aka string). Therefore, while indexing the char array,
we must be careful to stay within the bounds or we may receive a run time error or incorrect results.
a) (10 points) What will be the print output values of the program on Mason platform? Please write
“Invalid” if the printf statement may result in a run-time error or would print incorrect results.
b) (5 points) What include statement is missing based on the syntax compilation error below causing a
string.h
“warning: implicit declaration of function ‘strlen…” when compiling?. Answer: #include <__________>
. (10 points). A structure (aka struct) in C is a self defined data type that includes multiple components.
We will cover C structures more in weeks and 7. What will be the print output values of the program
on Mason pla orm mason.gmu.edu?
#include <stdio.h>
#include <stdlib.h>
#define STR_S Z 80 max number of characters for our string
typedef struct {
char name[STR_S Z ];
double price; in USD
} product_t;
int main() {
product_t calculator = {"T -84+", 14 . };
printf(" ame: % s\n", calculator.name); T-84+
// 6) Ans: Name: __________________
$14.99
printf(" rice: $%.2lf\n", calculator.price); // 6) Ans: Price: _________________
return ( X T_SU SS);
}
4 OF 4