Ask user to enter 3 numbers a b and c Write a program which

Ask user to enter 3 numbers a, b and c. Write a program, which will print the values of following expressions: a. max(a, a + b, a- c) + max(b, 2b - c, b + 2a) b. max(3, c + 3a, 0) + min(ab - 2, 3c, ac) Define functions max and min, which will accept 3 numbers as arguments and return the max and min of them correspondingly. Use these function in order to calculate the values of the expressions. Ask user to enter a positive integer number n (n must be less than 20). Read n numbers and save them in array. Write 4 functions which accept as a parameter that array and return c. The average of positive numbers stored in the array d. The sum of elements with even indexes e. The maximum of the numbers stored in the array f. The index of the minimum of the numbers stored in the array Print all 4 results in the end.

Solution

Ans:

1)

#include<stdio.h>
int max(int a,int b,int c);
int min(int a,int b,int c);
int main()
{
int a,b,c;
int result1;//to store result of first expression
int result2;//to store result of second expression
printf(\"Enter a,b,c values\");
scanf(\"%d%d%d\",&a,&b,&c);
result1=max(a,a+b,a-c) + max(b,2*b-c,b+2*a);
result2=max(3,c+3*a,0) + min(a*b-2,3*c,a*c);
printf(\"Result of Expression1=%d\ \",result1);
printf(\"Result of Expression2=%d\ \",result2);
return 0;
}
int max(int a,int b,int c)
{
if(a>b && a>c)
return a;
else if(b>c)
return b;
else
return c;
}
int min(int a,int b,int c)
{
if(a<b && a<c)
return a;
else if(b<c)
return b;
else
return c;
}

2)

#include<stdio.h>
float averageOfPositiveNumbers(int a[],int n);
int sumOfElementsWithEvenIndex(int a[],int n);
int findMaximum(int a[],int n);
int findMinimumIndex(int a[],int n);
int main()
{
int a[20],i,n,sum,max,min_index;
float avg_pos;
printf(\"Enter a positive integer(less than 20)\");
scanf(\"%d\",&n);
printf(\"Enter %d elements \",n);
for(i=0;i<n;i++)
scanf(\"%d\",&a[i]);
avg_pos=averageOfPositiveNumbers(a,n);
sum=sumOfElementsWithEvenIndex(a,n);
max=findMaximum(a,n);
min_index=findMinimumIndex(a,n);
printf(\"The Average of Positive Numbers=%f\ \",avg_pos);
printf(\"The Sum of Elements with positive index=%d\ \",sum);
printf(\"The Maximum of the numbers stored in the array=%d\ \",max);
printf(\"The index of minimum of the numbers=%d\",min_index);
return 0;
}
float averageOfPositiveNumbers(int a[],int n)
{
int i,sum=0,pc=0;
float avg;
for(i=0;i<n;i++)
{
if(a[i]>=0)
{
sum=sum+a[i];
pc++;
}
}
avg=(float)sum/pc;
return avg;
}

int sumOfElementsWithEvenIndex(int a[],int n)//index starts from 0
{
int sum=0,i;
for(i=0;i<n;i++)
{
if(i%2==0)
sum=sum+a[i];
}
return sum;
}
int findMaximum(int a[],int n)//to find maximum value
{
int i,max;
max=a[0];
for(i=1;i<n;i++)
{
if(a[i]>max)
max=a[i];
}
return max;
}
int findMinimumIndex(int a[],int n)
{
int i,min,min_index;
min=a[0];
min_index=0;
for(i=1;i<n;i++)
{
if(a[i]<min)
min_index=i;
}
return min_index;
}

 Ask user to enter 3 numbers a, b and c. Write a program, which will print the values of following expressions: a. max(a, a + b, a- c) + max(b, 2b - c, b + 2a)
 Ask user to enter 3 numbers a, b and c. Write a program, which will print the values of following expressions: a. max(a, a + b, a- c) + max(b, 2b - c, b + 2a)
 Ask user to enter 3 numbers a, b and c. Write a program, which will print the values of following expressions: a. max(a, a + b, a- c) + max(b, 2b - c, b + 2a)

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site