in main (1) define a float array of size = 5 and initialize the array; (2) print
ID: 644258 • Letter: I
Question
in main
(1) define a float array of size = 5 and initialize the array;
(2) print the array;
(3) pass 2nd element of the array to function Change by reference (using pointer),
(4) print the modified array
(5) call function Add using the call-by-reference, which means to use pointer, to modify the whole array
(6) print the modified array
function Change
function takes parameter from main
and 1) multiply itself by -1 if the value is smaller than 0,
2) keep itself if it is larger or equal to 0,
return the value
function Add
take the parameters from the main;
add 5 to each element in the array and return the modified array
Explanation / Answer
#include <stdio.h>
void print(float arr[], int size){
int i = 0;
for(i = 0; i < size; ++i){
printf("%.2f ", arr[i]);
}
printf(" ");
}
float change(float *ptr){
if(*ptr < 0){
*ptr *= -1;
}
return *ptr;
}
void add(float arr[], int size){
int i = 0;
for(i = 0; i < size; ++i){
arr[i] += 5;
}
}
int main(){
float arr[5] = {4, -13.6, 12, 0, -5.8};
print(arr, 5);
change(&(arr[1]));
print(arr, 5);
add(arr, 5);
print(arr, 5);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.