Write a C code to perform the following tasks In main Define

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,

Solution

// C code
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <math.h>

// sum function using pointers to add elements of array
int sumArray(int *array, int *SIZE)
{
   int i;
   int sum = 0;

   for (i = 0; i < *SIZE; ++i)
   {
       sum = sum + *(array+i);
   }

   return sum;
}

int main()
{
   int SIZE = 7;

   int array[] = {3,5,7,9,11,13,15};

   // ddeclare and initialize pointer
   int *ptr;
   ptr = array;

   // pass array pointer and size of array to function
   int sum = sumArray(ptr,&SIZE);

   printf(\"Array: \");
   int i;
   for (i = 0; i < SIZE; ++i)
   {
       printf(\"%d \",array[i]);
   }
   printf(\"\ \");

   printf(\"Sum of array: %d\ \",sum);

   return 0;
}

/*
output:

Array: 3 5 7 9 11 13 15
Sum of array: 63
*/

 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

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site