3.5 marks
- Ubiem statement
A Farm is represented by the following structure:
struct Farm
{
int hens:
int cows
Is
You are given a function,
int MaxLegs (struct Farm farms[], int
m);
5398
The function accepts an array 'farms' of type 'Farm' consisting of 'm' elements as its argument. A 'Farm' consists of hens which have two legs
and cows which have 4 legs. Implement the function to count the total number of legs for each array element of 'farms' and return the
maximum leg count.
Note:
Computed value lies within integer range.
Return -1 if 'farms' is empty.
Example:
Input:
hens
27
Output:
398
Answers
The C program for the given problem statement is
#include <stdio.h>
#include<stdlib.h>
#include <math.h>
int main()
{
int L,E,x,y;
printf("total number of legs L = \n");
scanf("%d",&L);
printf("total number of eyes E =\n");
scanf("%d",&E);
if(L%2 == 0 & E%2 == 0){
y = (L - E)/2;
x = (L-(4*y))/2;
printf("%d ",y);
printf("%d",x);
}
else {
if(L%2 != 0){
printf("Number of legs should be even number");
}
else {
printf("Number of eyes should be even number");
}
}
return 0;
}
- Different libraries are included at the beginning of the code.
- The variables are declared and the conditions according to the problem statement are implemented.
- If else condition statements are used and at last the result id printed on the screen.
#SPJ2
Answer:
#include <stdio.h>
#include<stdlib.h>
#include <math.h>
int main()
{
int L,E,x,y;
printf("total number of legs L = \n");
scanf("%d",&L);
printf("total number of eyes E =\n");
scanf("%d",&E);
if(L%2 == 0 & E%2 == 0){
y = (L - E)/2;
Explanation:
#include <stdio.h>
#include<stdlib.h>
#include <math.h>
int main()
{
int L,E,x,y;
printf("total number of legs L = \n");
scanf("%d",&L);
printf("total number of eyes E =\n");
scanf("%d",&E);
if(L%2 == 0 & E%2 == 0){
y = (L - E)/2;
x = (L-(4*y))/2;
printf("%d ",y);
printf("%d",x);
}
else {
if(L%2 != 0){
printf("Number of legs should be even number");
}
else {
printf("Number of eyes should be even number");
}
}
return 0;
}
The code starts off by including a number of libraries.
The problem statement's conditions are put into practise, and the variables are stated.
Finally, the result is printed on the screen using if-else condition statements.
#SPJ2