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

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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]);

}

}

}

Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Array
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education