Using C, Write a function called init_array. It is passed 3 parameters, as speci
ID: 3750461 • Letter: U
Question
Using C, Write a function called init_array. It is passed 3 parameters, as specified in the prototype below:int init_array(int array[ ], int len, int start_value);
The ith item in array should be initialized to the value start_value * i. Also, the function should return the sum of the values in array after initialization.
Using C, Write a function called init_array. It is passed 3 parameters, as specified in the prototype below:
int init_array(int array[ ], int len, int start_value);
The ith item in array should be initialized to the value start_value * i. Also, the function should return the sum of the values in array after initialization.
int init_array(int array[ ], int len, int start_value);
The ith item in array should be initialized to the value start_value * i. Also, the function should return the sum of the values in array after initialization.
int init_array(int array[ ], int len, int start_value);
The ith item in array should be initialized to the value start_value * i. Also, the function should return the sum of the values in array after initialization.
Explanation / Answer
int init_array(int array[] , int len , int start_value){
//Steps : we will divide the requirements in two parts
// Part 1 : we will run a loop to initilize all array elements to the start_value*i for each ith element in the array
//Part 2 : we will initilize a total_sum variable to 0 and then we will run loop for all elements in array and we will update the sum every time to get total sum.
//Part 3: we will return the total sum.
//Part 1: Start
int i ;
for (i = 0; i< len ; i++ )
{
array[i] = start_value * i;
}
//part 1 : Done
//Part 2 : Start
// Now we will run a loop to get the sum of all values.
int total_sum = 0; //Since initial value of total_sum is 0.
for (i = 0; i< len ; i++)
{
total_sum = total_sum + array[i];
}
//Part 2 : Done
//Part 3: just return the total_sum.
return total_sum;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.