WAP Mathematical Operations on an Array


Levels of difficulty: / perform operation:

This C Program calculates the sum & average of an array. It declares an array and then add the array elements and finds the average of the array.

Here is source code of the C program to calculate the sum & average of an array. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

Program 1 :

#include  <stdio.h>
#define MAXSIZE 10
void main() {
	int array[MAXSIZE];
	int i, num, negative_sum = 0, positive_sum = 0;
	float total = 0.0, average;
	printf ("Enter the value of N \n");
	scanf("%d", &num);
	printf("Enter %d numbers (-ve, +ve and zero) \n", num);
	for (i = 0; i < num; i++) {
		scanf("%d", &array[i]);
	}
	printf("Input array elements \n");
	for (i = 0; i < num; i++) {
		printf("%+3d\n", array[i]);
	}
	/*  Summation starts */
	for (i = 0; i < num; i++) {
		if (array[i] < 0) {
			negative_sum = negative_sum + array[i];
		} else if (array[i] > 0) {
			positive_sum = positive_sum + array[i];
		} else if (array[i] == 0) {
			;
		}
		total = total + array[i] ;
	}
	average = total / num;
	printf("\n Sum of all negative numbers =  %d\n", negative_sum);
	printf("Sum of all positive numbers =  %d\n", positive_sum);
	printf("\n Average of all input numbers =  %.2f\n", average);
}

Output


Enter the value of N
10
Enter 10 numbers (-ve, +ve and zero)
-8
9
-100
-80
90
45
-23
-1
0
16
Input array elements
 -8
 +9
-100
-80
+90
+45
-23
 -1
 +0
+16
 
Sum of all negative numbers =  -212
Sum of all positive numbers =  160
 
Average of all input numbers =  -5.20



C Program to Calculate the Sum of the Array Elements using Pointer

This C Program calculates the sum of array elements using pointer. The program uses the pointer to traverse the array and adds up the element values there.

Here is source code of the C program to calculates the sum of array elements using pointer. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

Program : 2

#include <stdio.h>
#include <malloc.h>
void main() {
	int i, n, sum = 0;
	int *a;
	printf("Enter the size of array A \n");
	scanf("%d", &n);
	a = (int *) malloc(n * sizeof(int));
	printf("Enter Elements of First List \n");
	for (i = 0; i < n; i++) {
		scanf("%d", a + i);
	}
	<
	/*  Compute the sum of all elements in the given array */
	for (i = 0; i < n; i++) {
		sum = sum + *(a + i);
	}
	printf("Sum of all elements in array = %d\n", sum);
}

Output

Enter the size of array A
5
Enter Elements of First List
4
9
10
56
100
Sum of all elements in array = 179



C Program to Calculate Sum of all Elements of an Array using Pointers as Arguments

This C Program calculate sum of all elements of an array using pointers as arguments. The program calls a function to add the addition and passes the array argument as a pointer.

Here is source code of the C program to calculate sum of all elements of an array using pointers as arguments. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

Program 3

#include <stdio.h>
void main() {
	static int array[5] = {
		200, 400, 600, 800, 1000
	}
	;
	int sum;
	int addnum(int *ptr);
	sum = addnum(array);
	printf("Sum of all array elements = %5d\n", sum);
}
int addnum(int *ptr) {
	int index, total = 0;
	for (index = 0; index < 5; index++) {
		total += *(ptr + index);
	}
	return(total);
}

Output

Sum of all array elements =  3000



C Program to Compute the Sum of two One-Dimensional Arrays using Malloc

This C Program computes the sum of two one-dimensional arrays using malloc. The program allocates 2 one-dimentional arrays using malloc() call and then does the addition and stores the result into 3rd array. The 3rd array is also defined using malloc() call.

Here is source code of the C program to compute the sum of two one-dimensional arrays using malloc. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

Program : 4

#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
void main() {
	int i, n;
	int *a, *b, *c;
	printf("How many Elements in each array...\n");
	scanf("%d", &n);
	a = (int *)malloc(n * sizeof(int));
	b = (int *)malloc(n * sizeof(int));
	c = (int *)malloc(n * sizeof(int));
	printf("Enter Elements of First List\n");
	for (i = 0; i < n; i++) {
		scanf("%d", a + i);
	}
	printf("Enter Elements of Second List\n");
	for (i = 0; i < n; i++) {
		scanf("%d", b + i);
	}
	for (i = 0; i < n; i++) {
		*(c + i) = *(a + i) + *(b + i);
	}
	printf("Resultant List is\n");
	for (i = 0; i < n; i++) {
		printf("%d\n", *(c + i));
	}
}

Output

How many Elements in each array...
5
Enter Elements of First List
23
45
67
12
90
Enter Elements of Second List
87
56
90
45
10
Resultant List is
110
101
157
57
100



C Program to Find the Sum of Contiguous Subarray within a 1 – D Array of Numbers which has the Largest Sum

This C Program computes the sum of two one-dimensional arrays using malloc. The program allocates 2 one-dimentional arrays using malloc() call and then does the addition and stores the result into 3rd array. The 3rd array is also defined using malloc() call.

Here is source code of the C program to compute the sum of two one-dimensional arrays using malloc. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

Program : 5

#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
void main() {
	int i, n;
	int *a, *b, *c;
	printf("How many Elements in each array...\n");
	scanf("%d", &n);
	a = (int *)malloc(n * sizeof(int));
	b = (int *)malloc(n * sizeof(int));
	c = (int *)malloc(n * sizeof(int));
	printf("Enter Elements of First List\n");
	for (i = 0; i < n; i++) {
		scanf("%d", a + i);
	}
	printf("Enter Elements of Second List\n");
	for (i = 0; i < n; i++) {
		scanf("%d", b + i);
	}
	for (i = 0; i < n; i++) {
		*(c + i) = *(a + i) + *(b + i);
	}
	printf("Resultant List is\n");
	for (i = 0; i < n; i++) {
		printf("%d\n", *(c + i));
	}
}

Output

How many Elements in each array...
5
Enter Elements of First List
23
45
67
12
90
Enter Elements of Second List
87
56
90
45
10
Resultant List is
110
101
157
57
100



C Program to Find the Sum of Contiguous Subarray within a 1 – D Array of Numbers which has the Largest Sum

This C Program finds the sum of contiguous subarray within a 1 – D array of numbers which has the largest sum.
Here is source code of the C Program to find the sum of contiguous sub array within a 1 – D array of numbers which has the largest sum. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

Program : 6

#include <stdio.h>
#include <stdlib.h>
int maxSubArraySum(int a[], int size, int *begin, int *end) {
	int max_so_far = 0, max_end = 0;
	int i, current_index = 0;
	for (i = 0; i < size; i++) {
		max_end = max_end + a[i];
		if (max_end <= 0) {
			max_end = 0;
			current_index = i + 1;
		} else if (max_so_far < max_end) {
			max_so_far = max_end;
			*begin = current_index;
			*end = i;
		}
	}
	return max_so_far;
}
int main() {
	int arr[] = {
		10, -2, 15, 9, -8, 12, 20, -5
	}
	;
	int start = 0, end = 0;
	int size = sizeof(arr) / sizeof(arr[0]);
	printf(" The max sum is %d", maxSubArraySum(arr, size, &start, &end));
	printf(" The begin and End are %d & %d", start, end);
	getchar();
	return 0;
}

Output

 The max sum is 56 The begin and End are 0 & 6