1. Write a function increment_value with the following prototype: void increment
ID: 3697346 • Letter: 1
Question
1. Write a function increment_value with the following prototype:
void increment_value (int *value);
When passed the address of a variable, the function will increment the value stored by that variable by 1.
2. Write a function sum_array with the following prototype:
int sum_array (const int a, int n);
The function takes in an array a of size n, and returns the sum of the values in the array. Constraint: the function should use pointer arithmetic instead of array subscripting to traverse the array. You can use the following code as a starting point, then convert the function to pointer arithmetic (eliminate the variable i and the uses of the [] operator):
int sum_array (const int a, int n)
{
int i, sum = 0;
for (i = 0; i < n; i++)
sum += a[i];
return sum;
}
2. Write a program that uses the above functions as follows:
• The program declares an array of size 10 and initializes its values to 1, 2, 3, .., 10.
• First, the program should call the sum_array function and print out the sum of the values of the array.
• Second, the program uses the increment_value function to increment the values of each element in the array
• Third, the program should call the sum_array function again and print out the sum of the values of the array.
Explanation / Answer
1. Write a function increment_value with the following prototype:
void increment_value (int *value);
When passed the address of a variable, the function will increment the value stored by that
variable by 1.
void increment_value(int *value)
{
(*value)++;
}
2. Write a function sum_array with the following prototype:
int sum_array (const int a, int n);
The function takes in an array a of size n, and returns the sum of the values in the array.
Constraint: the function should use pointer arithmetic instead of array subscripting to traverse
the array. You can use the following code as a starting point, then convert the function to pointer
arithmetic (eliminate the variable i and the uses of the [] operator):
int sum_array (const int a, int n)
{
int i, sum = 0;
for (i = 0; i < n; i++)
sum += *(a + i);
return sum;
}
2. Write a program that uses the above functions as follows:
The program declares an array of size 10 and initializes its values to 1, 2, 3, .., 10.
First, the program should call the sum_array function and print out the sum of the
values of the array.
Second, the program uses the increment_value function to increment the values of
each element in the array
Third, the program should call the sum_array function again and print out the sum of
the values of the array.
//2. Write a program that uses the above functions as follows:
//The program declares an array of size 10 and initializes its values to 1, 2, 3, .., 10.
int Array[10];
for(int i = 0; i < 10; i++)
Array[i] = i+1;
//First, the program should call the sum_array function and print out the sum of the
//values of the array.
printf("The sum of elements is: %d ", sum_array(Array, 10));
//Second, the program uses the increment_value function to increment the values of
//each element in the array
for(int i = 0; i < 10; i++)
increment_value(Array + i);
//Third, the program should call the sum_array function again and print out the sum of
//the values of the array.
printf("The sum of elements is: %d ", sum_array(Array, 10));
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.