Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using C 1. Write a function increment_value with the following prototype: void i

ID: 3697441 • Letter: U

Question

Using C

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++)

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.

Algorithm design process

1. Main program:

sum += a[i];

2. Develop the increment_value function

3. Develop the sum_array function

a. Declare and initialize variables (array of size 10, sum of values).

b. Call the sum_array function and print out the return value

c. Call the increment_value function in a loop, to increment all the

array values. Key issue: how do we pass the array elements to the

function, which requires pointers to integers? Need to pass the address of

each element.

d. Call the sum_array function and print out the return value

a. Use pointers to make sure the value is updated

a. Details are provided above

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));

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote