1. Write a C statement for each of the following. (4 pts) a. Declare an integer
ID: 3806872 • Letter: 1
Question
1. Write a C statement for each of the following. (4 pts) a. Declare an integer called total, 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 answerExplanation / Answer
Solution of 1)
a) int total = 10;
b) int *ptr =&total;
c) *ptr = 20;
d) printf("%d", &total);
e) printf("%d", ptr);
f) int *qtr = &ptr;
g) int *qtr = &result;
h) *qtr = (*qtr) * 2;
Solution of 2)
a) *answer = 'Z';
b) *choice = next;
c) printf("%d", answer);
Solution of 3)
a) True. function alpha can access variable x. As x is global variable.
b) True , function main and all functions has access to any static variable. .
c) True. inside the inner block local copy of y will be considered.
d) False, global variables can be accesses by all functions in file.
e) False, local variable x is accessible inside inner block.
Solution of 4)
int* findMinAndMax(int a[], int length, int *max){
int maxValue = a[0], minValue= a[0];
int maxIdx=0, minIdx= 0;
int idx, *min;
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 = &a[maxIdx]; // assign address of max element to max pointer
printf(" %d",max); // print address of max element
printf(" %d",*max); // print max element using pointer max
min= &a[minIdx]; // assign address of min element to min pointer
printf(" %d",min); // print address of min element
printf(" %d", *min); // print min element using pointer min
return min ;
}
if you have any doubts, you can ask in comment section.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.