In C 1 Write a program to create an array of size 5 Initiali
In C
1- Write a program to create an array of size 5. Initialize the array to 7, 9, 10, 15 and 2.Display all the values of this array using a loop.
2- Find the lowest value of the array in the above problem and display it.
3- Find the average of these values.
4- Find the difference between the average and lowest value.
5- How many odd numbers and how many even numbers do we have in this array?
6- Find the average of odd numbers and the average of even numbers.
7- Write a program to read 5 double values to an array of size 5 of type double. Display the values.
8- Write a function to sort arrays of the size passed to the function.
9- Write a function that merge two sorted arrays. The function should accept three arrays. Two ofthe arrays are the sorted arrays of the same size. The third array will have the result of themerged arrays. The merged array will have the sorted values of both arrays.
Solution
1-Write a program to create an array of size 5. Initialize the array to 7, 9, 10, 15 and 2.Display all the values of this array using a loop.
1-ANSWER
// Program to create an array
#include <stdio.h>
int main()
{
int arr[10], i, n;
printf(\"Enter n: \");
scanf(\"%d\", &n);
for(i=0; i<n; ++i)
{
printf(\"Enter number%d: \",i+1);
scanf(\"%d\", &arr[i]);
sum += arr[i];
}
return 0;
}
OUTPUT:
Enter n: 5
Enter number1:7
Enter number2: 9
Enter number3: 10
Enter number4: 15
Enter number5: 2
2- Find the lowest value of the array in the above problem and display it.
2- ANSWER
// Program to find the lowest value of the array in the above problem and display it.
#include <stdio.h>
int main()
{
int arr[10], i, n lowest, location=1;
printf(\"Enter n: \");
scanf(\"%d\", &n);
for(i=0; i<n; ++i)
{
printf(\"Enter number%d: \",i+1);
scanf(\"%d\", &arr[i]);
sum += arr[i];
}
lowest = arr[0];
for ( i = 1 ; i < n ; i++ )
{
if ( arr[i] < lowest )
{
lowest = arr[i];
location = i+1;
}
}
printf(\"Lowest element is present at location %d and it\'s value is %d.\ \", location, lowest);
return 0;
}
Enter n: 5
Enter number1:7
Enter number2: 9
Enter number3: 10
Enter number4: 15
Enter number5: 2
Lowest element is present at location 5 and it\'s value is 2.
3 - Find the average of these values.
3 - ANSWER
// Program to Find the average of an array
#include <stdio.h>
int main()
{
int arr[10], i, n, sum = 0;
float average;
printf(\"Enter n: \");
scanf(\"%d\", &n);
for(i=0; i<n; ++i)
{
printf(\"Enter number%d: \",i+1);
scanf(\"%d\", &arr[i]);
sum += arr[i];
}
average = sum/n;
printf(\"Average is = %f\", average);
return 0;
}
OUTPUT:
Enter n: 5
Enter number1:7
Enter number2: 9
Enter number3: 10
Enter number4: 15
Enter number5: 2
Average is= 8.6
4- Find the difference between the average and lowest value.
4 - ANSWER
// Program to Find the difference between the average and lowest value
#include <stdio.h>
int main()
{
int arr[10], i, n, sum = 0,lowest;
float average, difference;
printf(\"Enter n: \");
scanf(\"%d\", &n);
lowest = arr[0];
for(i=0; i<n; ++i)
{
printf(\"Enter number%d: \",i+1);
scanf(\"%d\", &arr[i]);
sum += arr[i];
}
average = sum/n;
printf(\"Average is = %f\", average);
for ( i = 1 ; i < n ; i++ )
{
if ( arr[i] < lowest )
{
lowest = arr[i];
}
}
printf(\"Lowest element is %d\ \", lowest);
difference= average-lowest;
printf(\"Difference between average and lowest element is %d\ \", difference);
return 0;
}
OUTPUT:
Enter n: 5
Enter number1:7
Enter number2: 9
Enter number3: 10
Enter number4: 15
Enter number5: 2
Average is = 8.6
Lowest element is 2
Difference between aveage and lowest element is 6.6




