Programming Question

CSCE A201 Midterm #2Name:
This exam is worth a total of 100 points. The exam is open book and open notes. You
can use a calculator or a computer but must do your own work.
I recommend you go through and try to answer all questions directly first and only
type/compile or look up references after attempting all the questions.
Show your work or thought processes. You can insert your answers into this document
and upload your final answers or upload your answers in a separate document(s) that are
compressed into a single zip file.
1) 20 points. Identify the bugs and give a remedy to avoid or fix the bug.
a)
b)
c)
d)
char s[3];
s[0]=’H’;
s[1]=’E’;
s[2]=’Y’;
printf(“%s\n”,s);
int* foo()
{
int p;
p = 100;
return &p;
}
char *p = (char *) malloc(1);
char z;
p = &z;
*p = ‘a’;
char s[100];
scanf(“%s”,s);
if (s == “hi”)
printf(“there”);
2) 10 points. In the following function prototype the function findMin is given an
array of integers and the number of elements in that array:
int findMin(int arr[], int numElements);
Write the function’s implementation so that it returns the smallest value in the array.
3) 6 points. What generally goes inside a .h file?
4) 6 points. Write the function foo so that after the function is called, the value in
variable n is 100.
int n;
foo(&n);
// n should be set to 100 after this call
5) 6 points. Describe an advantage of local variables over global variables.
6) 5 points. Given the array below, after the code runs what value does p point to?
double a[4] = {1.0, 2.0, 3.0, 4.0};
double *p;
p = a;
p = p + 2;
7) 7 points. Give a scenario why you might want to pass a parameter by pointer, even if
the function is only reading from the pointer parameter, not changing any contents of
the pointer.
8) 10 points. The following code sums all the values in an array of length size.
int sum = 0;
for (int i = 0; i < size; i++) sum+=a[i]; Convert the code to a function so that the following function call outputs the sum: printf("The sum is %d.\n", computeSum(a, size)); 9) 5 points. Create a struct named Foo so that the following code will run. Make the data type of x an int and y a double. Foo f; f.x = 6; f.y = 3.1; 10) 5 points. Complete the line of code below so that malloc is used to return back the equivalent of an array of 10 integers. int *p = __________________________________ 11) 5 points. What is a breakpoint and what is it good for? 12) 15 points. a) Here is a definition of a struct: typedef struct Stuff { char name[50]; int value; } Stuff; Write code that creates a variable p1 of the struct with the name “foo1” and value 1, and a variable p2 with the name “foo2” and value 2. You can use the string functions. b) Consider this alternate definition of the struct: typedef struct Stuff { char *name; int id; } Stuff; Write code that creates the same variables as part a, except use malloc to allocate the memory for the strings to hold the name. c) If we want to copy one struct to another using assignment like p1 = p2; would this work to make a copy of p2 into p1 if we use the definition in part a? Describe why or why not. What if we use the definition in part b? Describe why or why not.

Save Time On Research and Writing
Hire a Pro to Write You a 100% Plagiarism-Free Paper.
Get My Paper
Still stressed from student homework?
Get quality assistance from academic writers!

Order your essay today and save 25% with the discount code LAVENDER