Explain this c code in details. #include int main() { int max_weight_truck, n; scanf("%d", &max_weight_truck); scanf("%d", &n); int weight_item[n]; int priority[n]; int p_sort[n]; int w_sort[n]; int a[n]; for(int i=0; i0) { s=0; while(s<=max_weight_truck && i< n) { s= s+w_sort[i]; i+=1; } if(s-max_weight_truck>0) { s= s-w_sort[i-1]; i-=1; } if(truck==5) l= i; else if(truck<5) l=i-l; o_item[j]= l; l= i; unused_space[j]= max_weight_truck-s; truck-=1; j+=1; } truck=5; j=i; printf("Oversized items loaded into each truck: "); for(i=0; i< truck; i++) { printf("%d ", o_item[i]); } printf("\n"); l=0; printf("Unused space in each truck: "); for(i=0; i< truck; i++) { //to calculate the no of trucks used if(unused_space[i]==max_weight_truck) l+=1; printf("%d ", unused_space[i]); } printf("\n"); printf("No. of unused truck: %d\n", l); if(l==0 | l==5) { printf("List of unused items:\n"); for(i=j; i
Explain this c code in details.
#include <stdio.h>
int main()
{
int max_weight_truck, n;
scanf("%d", &max_weight_truck);
scanf("%d", &n);
int weight_item[n];
int priority[n];
int p_sort[n];
int w_sort[n];
int a[n];
for(int i=0; i<n; i++)
scanf("%d", &weight_item[i]);
for(int i=0; i<n; i++)
scanf("%d", &priority[i]);
//sorting priority array
for(int i=0; i<n; i++)
p_sort[i]= priority[i];
int temp=0;
for (int i = 0; i < n; i++)
{
for (int j = i+1; j < n; j++)
{
if(p_sort[i] < p_sort[j])
{
temp = p_sort[i];
p_sort[i] = p_sort[j];
p_sort[j] = temp;
}
}
}
for(int i=0; i<n; i++)
a[i]= 0;
for(int i=0; i<n; i++)
{
int index;
for(int j=0; i<n; j++)
{
if(p_sort[i]== priority[j] && a[j]==0)
{
index= j;
break;
}
}
w_sort[i]= weight_item[index];
a[index]=1;
}
int s=0, i=0,j=0,truck=5, l=0;
int o_item[truck], unused_space[truck];
// initializing arrays
for(i=0; i<truck; i++)
{
o_item[i]= 0;
unused_space[i]= 0;
}
i=0;
while(truck>0)
{
s=0;
while(s<=max_weight_truck && i< n)
{
s= s+w_sort[i];
i+=1;
}
if(s-max_weight_truck>0)
{
s= s-w_sort[i-1];
i-=1;
}
if(truck==5)
l= i;
else if(truck<5)
l=i-l;
o_item[j]= l;
l= i;
unused_space[j]= max_weight_truck-s;
truck-=1;
j+=1;
}
truck=5;
j=i;
printf("Oversized items loaded into each truck: ");
for(i=0; i< truck; i++)
{
printf("%d ", o_item[i]);
}
printf("\n");
l=0;
printf("Unused space in each truck: ");
for(i=0; i< truck; i++)
{
//to calculate the no of trucks used
if(unused_space[i]==max_weight_truck)
l+=1;
printf("%d ", unused_space[i]);
}
printf("\n");
printf("No. of unused truck: %d\n", l);
if(l==0 | l==5)
{
printf("List of unused items:\n");
for(i=j; i<n; i++)
{
printf("%d: %d\n", w_sort[i], p_sort[i]);
}
}
}
Step by step
Solved in 2 steps