Find the Largest Two Numbers in a given Array
#include
#include
#define max 4
void main()
{
int array[max], i, largest1, largest2, temp;
clrscr();
printf("Enter %d integer numbers \n", max);
for (i = 0; i < max; i++)
{
scanf("%d", &array[i]);
}
printf("Input interger are \n");
for (i = 0; i < max; i++)
{
printf("%5d", array[i]);
}
printf("\n");
largest1 = array[0];
largest2 = array[1];
if (largest1 < largest2)
{
temp = largest1;
largest1 = largest2;
largest2 = temp;
}
for (i = 2; i < 4; i++)
{
if (array[i] >= largest1)
{
largest2 = largest1;
largest1 = array[i];
}
else if (array[i] > largest2)
{
largest2 = array[i];
}
}
printf("n%d is the first largest \n", largest1);
printf("%d is the second largest \n", largest2);
printf("nAverage of %d and %d = %d \n", largest1, largest2,
(largest1 + largest2) / 2);
getch();
}
Output
Enter 4 integer numbers
80
23
79
58
Input interger are
80 23 79 58
80 is the first largest
79 is the second largest
Average of 80 and 79 = 79
Calculate the Sum of the Array Elements using Pointer
#include
#include
#include
void main()
{
int i, n, sum = 0;
int *a;
clrscr();
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);
}
for (i = 0; i < n; i++)
{
sum = sum + *(a + i);
}
printf("Sum of all elements in array = %d\n", sum);
getch();
}
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
No comments:
Post a Comment