HW #9: Variable Scope and Pointers Due date: Saturday April 1st, 11:59 pm throug
ID: 3809376 • Letter: H
Question
HW #9: Variable Scope and Pointers Due date: Saturday April 1st, 11:59 pm through Handin (https://secure.cse.msu.edu/handin) 1. Write a C statement for each of the following. (4 pts) a. Declare an integer called tota initialize it to 10 b. Declare a pointer to an int called ptr and set it to point to tota c. Set the value of total to 20 using ptr d. Print the memory address of total using total e. Print the memory address of total using ptr f. Create a new pointer qtr and let it point to total using ptr g. Make qtr point to integer result (assume already result declared) h. Double the value of result using qtr 2. Write a statement to 1.5 pts) a. Assign character 'Z' to the variable pointed to by char pointer answer b. Change the value of variable pointed to by char pointer choice to next character c. Print the value of char pointer answer 3. Consider the following program with only the variable declarations shown. For each statement, specify if it is true or false. (2.5 pts) int x; void alpha(int a) int b; void beta() static int k; void gamma (int x) int main int x, y, z int y, z;Explanation / Answer
1)
a) int total=10;
b) int *ptr=&total;
c) *ptr=20;
d) printf("%d ",&total);
e) printf("%d ",&ptr);
f) int *qtr=&total;
g) int result=2;
qtr=&result;
h) result=2*(*qtr);
___________
2)
char *answer='Z';
char *choice=answer+1;
printf("%c",answer);
3)
a) true
b) false
c) true
d) false
e) false
____________
4)
int * findMinAndmax(int a[],int length,int *max)
{
int idx;
int maxValue=a[0],minValue=a[0];
int maxidx=0,minidx=0;
for(idx=0;idx<length;idx++)
{
if(a[idx]>maxValue)
{
maxValue=a[idx];
maxidx=idx;
}
if(a[idx]<minValue)
{
minValue=a[idx];
minidx=idx;
}
}
*max=maxValue;
int *min=&minValue;
return *min;
}
_____________
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.