Sei sulla pagina 1di 8

C Program to Calculate Sum & Average of an Array

1. /*
2. * C program to read N integers into an array A and
3. * a) Find the sum of negative numbers
4. * b) Find the sum of positive numbers
5. * c) Find the average of all numbers
6. * Display the results with suitable headings
7. */
8. #include <stdio.h>
9. #define MAXSIZE 10
10.
11. void main()
12. {
13.
int array[MAXSIZE];
14.
int i, num, negative_sum = 0, positive_sum = 0;
15.
float total = 0.0, average;
16.
17.
printf ("Enter the value of N \n");
18.
scanf("%d", &num);
19.
printf("Enter %d numbers (-ve, +ve and zero) \n", num);
20.
for (i = 0; i < num; i++)
21.
{
22.
scanf("%d", &array[i]);
23.
}
24.
printf("Input array elements \n");
25.
for (i = 0; i < num; i++)
26.
{
27.
printf("%+3d\n", array[i]);
28.
}
29.
/* Summation starts */
30.
for (i = 0; i < num; i++)
31.
{
32.
if (array[i] < 0)
33.
{
34.
negative_sum = negative_sum + array[i];
35.
}
36.
else if (array[i] > 0)
37.
{
38.
positive_sum = positive_sum + array[i];
39.
}
40.
else if (array[i] == 0)
41.
{
42.
;
43.
}
44.
total = total + array[i] ;
45.
}
46.
average = total / num;
47.
printf("\n Sum of all negative numbers = %d\n", negative_sum);
48.
printf("Sum of all positive numbers = %d\n", positive_sum);
49.
printf("\n Average of all input numbers = %.2f\n", average);
50. }

C Program to Sort the Array in an Ascending Order


1. /*
2. * C program to accept N numbers and arrange them in an ascending order
3. */
4. #include <stdio.h>
5.
6. void main()
7. {
8.
int i, j, a, n, number[30];
9.
10.
printf("Enter the value of N \n");
11.
scanf("%d", &n);
12.
printf("Enter the numbers \n");
13.
for (i = 0; i < n; ++i)
14.
scanf("%d", &number[i]);
15.
for (i = 0; i < n; ++i)
16.
{
17.
for (j = i + 1; j < n; ++j)
18.
{
19.
if (number[i] > number[j])
20.
{
21.
a = number[i];
22.
number[i] = number[j];
23.
number[j] = a;
24.
}
25.
}
26.
}
27.
printf("The numbers arranged in ascending order are given below
\n");
28.
for (i = 0; i < n; ++i)
29.
printf("%d\n", number[i]);
30. }

C Program to Sort the Array in Descending Order


1. /*
2. * C program to accept a set of numbers and arrange them
3. * in a descending order
4. */
5. #include <stdio.h>
6.
7. void main ()
8. {
9.
int number[30];
10.
int i, j, a, n;
11.
12.
printf("Enter the value of N\n");
13.
scanf("%d", &n);
14.
printf("Enter the numbers \n");
15.
for (i = 0; i < n; ++i)
16.
scanf("%d", &number[i]);
17.
/* sorting begins ... */
18.
for (i = 0; i < n; ++i)
19.
{
20.
for (j = i + 1; j < n; ++j)
21.
{
22.
if (number[i] < number[j])
23.
{
24.
a = number[i];
25.
number[i] = number[j];
26.
number[j] = a;
27.
}
28.
}
29.
}
30.
printf("The numbers arranged in descending order are given
below\n");
31.
for (i = 0; i < n; ++i)
32.
{
33.
printf("%d\n", number[i]);
34.
}
35. }

C Program to Find the Largest Two Numbers in a given Array


1. /*
2. * C program to read in four integer numbers into an array and find the
3. * average of largest two of the given numbers without sorting the
array.
4. * The program should output the given four numbers and the average.
5. */
6. #include <stdio.h>
7. #define MAX 4
8.
9. void main()
10. {
11.
int array[MAX], i, largest1, largest2, temp;
12.
13.
printf("Enter %d integer numbers \n", MAX);
14.
for (i = 0; i < MAX; i++)
15.
{
16.
scanf("%d", &array[i]);
17.
}
18.
19.
printf("Input interger are \n");
20.
for (i = 0; i < MAX; i++)
21.
{
22.
printf("%5d", array[i]);
23.
}
24.
printf("\n");
25.
/* assume first element of array is the first larges t*/
26.
largest1 = array[0];
27.
/* assume first element of array is the second largest */
28.
largest2 = array[1];
29.
if (largest1 < largest2)
30.
{
31.
temp = largest1;
32.
largest1 = largest2;
33.
largest2 = temp;
34.
}
35.
for (i = 2; i < 4; i++)
36.
{
37.
if (array[i] >= largest1)
38.
{
39.
largest2 = largest1;
40.
largest1 = array[i];
41.
}
42.
else if (array[i] > largest2)
43.
{
44.
largest2 = array[i];
45.
}
46.
}
47.
printf("n%d is the first largest \n", largest1);
48.
printf("%d is the second largest \n", largest2);
49.
printf("nAverage of %d and %d = %d \n", largest1, largest2,
50. (largest1 + largest2) / 2);
51. }

C Program to Cyclically Permute the Elements of an Array


This C Program cyclically permutes the elements of an array. This program first accepts an
array. Assume there are 4 elements in an array. It takes 2 element as a first elment in an array and
so on till the last element of the given array. Now here first element of an array becomes last
element in an array during cyclical permutation.
Here is source code of the C program to cyclically permutes the elements of an array. The C
program is successfully compiled and run on a Linux system. The program output is also shown
below.
1. /*
2. * C program to cyclically permute the elements of an array A.
3. * i.e. the content of A1 become that of A2. And A2 contains
4. * that of A3 & so on as An contains A1
5. */
6. #include <stdio.h>
7.
8. void main ()
9. {
10.
int i, n, number[30];
11.
printf("Enter the value of the n = ");
12.
scanf("%d", &n);
13.
printf("Enter the numbers\n");
14.
for (i = 0; i < n; ++i)
15.
{
16.
scanf("%d", &number[i]);
17.
}
18.
number[n] = number[0];
19.
for (i = 0; i < n; ++i)
20.
{
21.
number[i] = number[i + 1];
22.
}
23.
printf("Cyclically permuted numbers are given below \n");
24.
for (i = 0; i < n; ++i)
25.
printf("%d\n", number[i]);
26. }

C Program to Find the two Elements such that their Sum is Closest to Zero
This C Program checks two elements such that their sum is closest to zero.
1. /*
2. * C Program to Find the two Elements such that their Sum is Closest to
Zero
3. */
4. # include <stdio.h>
5. # include <stdlib.h>
6. # include <math.h>
7.
8. void minabsvaluepair(int array[], int array_size)
9. {
10.
int count = 0;
11.
int l, r, min_sum, sum, min_l, min_r;
12.
13.
/* Array should have at least two elements*/
14.
if (array_size < 2)
15.
{
16.
printf("Invalid Input");
17.
return;
18.
}
19.
20.
/* Initialization of values */
21.
min_l = 0;
22.
min_r = 1;
23.
min_sum = array[0] + array[1];
24.
for (l = 0; l < array_size - 1; l++)
25.
{
26.
for (r = l + 1; r < array_size; r++)
27.
{
28.
sum = array[l] + array[r];
29.
if (abs(min_sum) > abs(sum))
30.
{
31.
min_sum = sum;
32.
min_l = l;
33.
min_r = r;
34.
}
35.
}
36.
}
37.
printf(" The two elements whose sum is minimum are %d and %d",
array[min_l], array[min_r]);
38. }
39.
40. int main()
41. {
42.
int array[] = {42, 15, -25, 30, -10, 35};
43.
minabsvaluepair(array, 6);
44.
getchar();
45.
return 0;
46. }

C Program to Find the Largest Number in an Array


This C Program finds the largest number in an array
Here is source code of the C Program to find the largest number in an array. The C program is
successfully compiled and run on a Linux system. The program output is also shown below.
1. /*
2. * C Program to Find the Largest Number in an Array
3. */
4. #include <stdio.h>
5.
6. int main()
7. {
8.
int array[50], size, i, largest;
9.
printf("\n Enter the size of the array: ");
10.
scanf("%d", &size);
11.
printf("\n Enter %d elements of the array: ", size);
12.
for (i = 0; i < size; i++)
13.
scanf("%d", &array[i]);
14.
largest = array[0];
15.
for (i = 1; i < size; i++)
16.
{
17.
if (largest < array[i])
18.
largest = array[i];
19.
}
20.
printf("\n largest element present in the given array is : %d",
largest);
21.
return 0;
22. }

C Program to Print all the Repeated Numbers with Frequency in an Array


This C Program print all the repeated numbers with frequency in an array.
Here is source code of the C Program to print all the repeated numbers with frequency in an
array. The C program is successfully compiled and run on a Linux system. The program output is
also shown below.
1. /*
2. * C Program to Print all the Repeated Numbers with Frequency in an
Array
3. */
4. #include <stdio.h>
5. #include <malloc.h>
6.
7. void duplicate(int array[], int num)
8. {
9.
int *count = (int *)calloc(sizeof(int), (num - 2));
10.
int i;
11.
12.
printf("duplicate elements present in the given array are ");
13.
for (i = 0; i < num; i++)
14.
{
15.
if (count[array[i]] == 1)
16.
printf(" %d ", array[i]);
17.
else
18.
count[array[i]]++;
19.
}
20. }
21.
22. int main()
23. {
24.
int array[] = {5, 10, 10, 2, 1, 4, 2};
25.
int array_freq = sizeof(array) / sizeof(array[0]);
26.
duplicate(array, array_freq);
27.
getchar();
28.
return 0;
29. }

Potrebbero piacerti anche