P059.c (1345B)
1 /* Write a program to find the sum of array elements using following fuctions 2 int array_sum(int a[], int n); 3 void get_data(int a[], int n); 4 void dispaly(int a[], int n); 5 */ 6 7 // This code has not been compiled. 8 // If you find any issues, please create a new issue on GitHub regarding them. 9 // Go to this link to create a new issue: https://github.com/notamitgamer/bsc/issues 10 11 #include <stdio.h> 12 13 int array_sum(int[], int); 14 void get_data(int[], int); 15 void display(int[], int); 16 17 int main() 18 { 19 int size, arr[20]; 20 printf("How many element do you want to add (Max: 20): "); 21 scanf("%d", &size); 22 if (size < 1 && size > 20) 23 { 24 printf("\nMax Element count is 20."); 25 return 1; 26 } 27 get_data(arr, size); 28 display(arr, size); 29 printf("\nSum of the elements is: %d", array_sum(arr, size)); 30 return 0; 31 } 32 33 void get_data(int a[], int n) 34 { 35 int i; 36 for (i = 0; i < n; i++) 37 { 38 printf("Enter element for position %d: ", i); 39 scanf("%d", &a[i]); 40 } 41 } 42 43 void display(int a[], int n) 44 { 45 int i; 46 printf("\nArray: ["); 47 for (i = 0; i < n; i++) 48 { 49 printf("%d", a[i]); 50 if (i != n - 1) 51 printf(", "); 52 } 53 printf("]\n"); 54 } 55 56 int array_sum(int a[], int n) 57 { 58 int i, sum = 0; 59 for (i = 0; i < n; i++) 60 sum += a[i]; 61 return sum; 62 }