Problem Description :
The function accepts two positive integers ‘r’ and ‘unit’
and a positive integer array ‘arr’ of size ‘n’ as its argument ‘r’ represents
the number of rats present in an area, ‘unit’ is the amount of food each rat
consumes and each ith element of array ‘arr’ represents the amount of food
present in ‘i+1’ house number, where 0 <= i
Note:
Return -1 if the array is null
Return 0 if the total amount of food from all houses is not
sufficient for all the rats.
Computed values lie within the integer range.
Example:
Input:
r: 7
unit: 2
n: 8
arr: 2 8 3 5 7 4 1 2
Output:
4
Explanation:
Total amount of food required for all rats = r * unit
= 7 * 2 = 14.
The amount of food in 1st houses = 2+8+3+5 = 18. Since,
amount of food in 1st 4 houses is sufficient for all the rats. Thus, output is
4.
C- Program for the above problem description is given below.
#include<stdio.h>
#define S 9999
int main()
{
int r, u, a[S],s=0,req=0,i,n;
printf("Enter how many number of houses are\n");
scanf("%d",&n);
printf("enter the food for each house\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
printf("the food available in all the houses are\n");
for(i=1;i<=n;i++)
printf("%d\t",a[i]);
printf("\nEnter the unit value of food for one rat\n");
scanf("%d",&u);
printf("Enter the total number of rats\n");
scanf("%d",&r);
printf("calculating the required food for %d\t rats\n", r);
req= u * r;
printf("the food required for %d\t rats is %d\t ", r,req);
printf("\n\n calculating how many house food is enough for %d\t rats", r);
printf("\n\n");
for(i=1;i<=n;i++)
{
s += a[i];
if(s >= req)
{
printf("\n the amount of food in first %d\t houses are enough for %d\t rats:", i, r);
printf("\n\t the output is %d\n", i);
return 1;
}
}
printf("\n food is not sufficiently available for %d\t rats in %d\t houses",r, (i-1));
}