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

For problem 7) Use for-loop to calculate sum Write a C code to perform the follo

ID: 3799154 • Letter: F

Question

For problem 7) Use for-loop to calculate sum

Write a C code to perform the following tasks: In main: Define an array of SIZE = 7 and initialize them with 3, 5, 7, 9, 11, 13, 15 Call function sum with the array and size as parameters Print each element of the array Print the sum In function sum: Use for-loop to calculate sum Write a C code to perform the following tasks: In main: Define an array of SIZE = 7 and initialize them with 3, 5, 7, 9, 11, 13, 15, Call function sum with the array and size, both passed in pointer representations Print each element of the array Print the sum In function sum: Take the parameters in pointer representations,

Explanation / Answer

Problem 6)

#include <stdio.h>
void sum(int [],int); // function prototype
int main()
{
    int size = 7;// size of the array
    int array[] = {3,5,7,9,11,13,15}; // initializing the array
    sum(array,size);// calling the function sum
    return 0;
}
void sum( int a[], int n) // function sum definition
{
    int sum = 0,i;// variable decleration
    for(i=0;i<n;i++) // for -loop
    {
        sum += a[i]; // adding each elelemt to sum
        printf("%d ",a[i]); // printing each elelemt
    }
    printf("Sum = %d ",sum); // printing the sum
  
}


OUTPUT

$./a.out

3                                                                                                                                                                                                                                             

5                                                                                                                                                                                                                                             

7                                                                                                                                                                                                                                             

9                                                                                                                                                                                                                                             

11                                                                                                                                                                                                                                            

13                                                                                                                                                                                                                                            

15                                                                                                                                                                                                                                            

Sum = 63

Problem 7)

#include <stdio.h>
void sum(int *,int *); // function prototype with pointer
int main()
{
    int size = 7;// size of the array
    int array[] = {3,5,7,9,11,13,15}; // initializing the array
    sum(array,&size);// calling the function sum
    return 0;
}
void sum( int *a, int *n) // function sum definition
{
    int sum = 0,i;// variable decleration
    for(i=0;i<*n;i++) // for -loop
    {
        sum += *(a+i); // adding each elelemt to sum
        printf("%d ",*(a+i)); // printing each elelemt
    }
    printf("Sum = %d ",sum); // printing the sum
  
}

OUTPUT

$ ./a.out                                                                                                                                                                                                                                

3                                                                                                                                                                                                                                             

5                                                                                                                                                                                                                                             

7                                                                                                                                                                                                                                             

9                                                                                                                                                                                                                                             

11                                                                                                                                                                                                                                            

13                                                                                                                                                                                                                                            

15                                                                                                                                                                                                                                            

Sum = 63