#include <assert.h> // Assign the value of 5 to the integer variable pointed //
ID: 3870291 • Letter: #
Question
#include <assert.h> // Assign the value of 5 to the integer variable pointed // to by ptr. // // This should only take one line of code! void set_to_five(int *ptr) { // TODO: Your code here. assert(0); } // Return the sum of all elements in the given array. The array // "arr" has "n" elements. // // Note that before, we used pointers to reference single variables // in memory, but now we are using them to refer to many elements. // The "arr" pointer points to the first element in the array in memory. // Because pointers themselves don't carry any length information // (like array objects do in Java), we generally have to pass in a // length parameter to functions which use pointers to reference // arrays. int array_sum(int *arr, int n) { // TODO: Your code here. assert(0); return -1; } #include <assert.h> // Assign the value of 5 to the integer variable pointed // to by ptr. // // This should only take one line of code! void set_to_five(int *ptr) { // TODO: Your code here. assert(0); } // Return the sum of all elements in the given array. The array // "arr" has "n" elements. // // Note that before, we used pointers to reference single variables // in memory, but now we are using them to refer to many elements. // The "arr" pointer points to the first element in the array in memory. // Because pointers themselves don't carry any length information // (like array objects do in Java), we generally have to pass in a // length parameter to functions which use pointers to reference // arrays. int array_sum(int *arr, int n) { // TODO: Your code here. assert(0); return -1; }Explanation / Answer
#include <stdio.h>
// Assign the value of 5 to the integer variable pointed
// to by ptr.
//
// This should only take one line of code!
void set_to_five(int *ptr) {
*ptr = 5;
// TODO: Your code here.
}
// Return the sum of all elements in the given array. The array
// "arr" has "n" elements.
//
// Note that before, we used pointers to reference single variables
// in memory, but now we are using them to refer to many elements.
// The "arr" pointer points to the first element in the array in memory.
// Because pointers themselves don't carry any length information
// (like array objects do in Java), we generally have to pass in a
// length parameter to functions which use pointers to reference
// arrays.
int array_sum(int *arr, int n) {
int sum = 0;
for(int i=0;i<n;i++){
// *(arr+i) is to get value of element from (arr = base address) + ith address
sum += *(arr+i);
}
return sum;
}
int main() {
// this is Driver method. i.e this function is to test the above methods
// here, i created 'int i' as 0
int i = 0;
printf("Value of i before calling set_to_five() is %d",i);
// pass 'address of i' to 'set_to_five()' method so
set_to_five(&i);
// print the value of i
printf(" Value of i after calling set_to_five() is %d",i);
// below is to test 'array_sum()' method
// create arr of int with 5 elements
int arr[] = {1,2,3,4,5};
// to find length of array we can use below code
int len = sizeof(arr) / sizeof(int);
// print sum of array which we passed to 'array_sum'
printf(" Sum of arr = %d",array_sum(arr,len));
return 0;
}
Hope this answer helps you. if in case of od any doubt feel free to comment here.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.