1Write a program that creates an integer array of size 5 rea
1-Write a program that creates an integer array of size 5, reads the values from the user and stores them in the array. After you store the elements in the array, find the sum, min, max and average values and display them.
2-Write a program that defines an array of 5 integers and initializes it using user’s input. Ask the user to enter the target. Find the indices of all the elements greater than the target entered by the user.
3-Write a program to prints the sum of all elements with an even index and all elements with an odd index of the following integer array. int c[10] ={5,6,4,9,11,14,8,2,3,1} .Use for loop and if statement.
4-Write a function that takes an array of integer values, array size and an integer parameter called item as parameters. The function return number of times item appears in the array. Test the function with a suitable program.
5-Write function concat that takes two source arrays of characters and a destination array of characters. The function should concatenate two source arrays and store in the destination array.
Solution
1)
#include <stdio.h>
int main()
{
int a[5],i,max,min,sum=0;
float avg;
printf(\"please enter 5 integer values\ \");
for(i=0;i<5;i++)
{
scanf(\"%d\",a+i);
}
max=a[0];
min=a[0];
for(i=0;i<5;i++)
{
sum+=a[i];
if(a[i]>max)
{
max=a[i];
}
if(a[i]<min)
{
min=a[i];
}
}
avg=sum/5;
printf(\"sum=%d\ avg=%f\ max=%d\ min=%d\",sum,avg,max,min);
return 0;
}
/*
output:
please enter 5 integer values
1 2 3 4 5
sum=15
avg=3.000000
max=5
min=1ubuntu@anavark:~/cheggcode$ ./p
please enter 5 integer values
-5 1 3 7 -100
sum=-94
avg=-18.000000
max=7
*/
------------------------------------------------
2)
#include <stdio.h>
int main()
{
int a[5],i,target;
printf(\"please enter 5integer values\ \");
for(i=0;i<5;i++)
{
scanf(\"%d\",a+i);
}
printf(\"please enter target value\ \");
scanf(\"%d\",&target);
printf(\"indices number of integer values which is greater then target value=%d\ \",target);
for(i=0;i<5;i++)
{
if(a[i]>target)
printf(\"%d\\t\",i);
}
return 0;
}
/*
output:
please enter 5integer values
1 2 3 4 5
please enter target value
3
indices number of integer values which is greater then target value=3
3 4
*/
---------------------------------------------------------
3)
#include <stdio.h>
int main()
{
int c[10] ={5,6,4,9,11,14,8,2,3,1};
int i;
int oddSum=0,evenSum=0;
for(i=0;i<10;i++)
{
if(i%2==0)
evenSum+=c[i];
else
oddSum+=c[i];
}
printf(\"oddSum=%d\ evenSum=%d\ \",oddSum,evenSum);
return 0;
}
/*
output:
oddSum=32
evenSum=31
*/

