In: Electrical Engineering
Create a program that stores the information of up to 50 containers loaded on ship. The program should contain a menu to do the following:
A or a to add a container.
R or r to retrieve the information of one container.
T or t to retrieve the information of all containers.
W or w to retrieve the total weight of the loaded containers.
X or x to exit the program
Ask the user for the number of containers to be loaded, create an array of floats with five rows and a number of columns equal to the number of containers provided by the user.
Option A: for this option check if the array has an empty column to enter the information for a container, if not, output a message indicating that array is full. If array is not full, ask the user for the ID number, length in meters, width in meter, height in meters, and weight in Kg of the container and store them in the array.
Option R: Ask the user for the desired ID number to be searched. If found, output the ID number, length in meters, width in meter, height in meters, weight in Kg, and volume in cubic meters of the container each on a line. If not found, display a message to that effect.
Option T: retrieve the info for all containers in the array with a dotted line separating one another. The info retrieved and calculated should be the same as the above step.
Option W: display the total weight of all the containers in the array.
Option X: Exit the program.
code:
#include <stdio.h>
int main() {
//code
int n; //represent the number of containers to be loaded.\n");
printf("Enter the number of containers to be loaded.\n");
scanf("%d",&n);
float arr [5][n];
int i;
for(i=0;i<4;i++) //initialization of array .here -1 represent empty cell.
{
int j;
for( j=0;j<n;j++)
arr[i][j]=-1;
}
printf(" Menu ....\n A or a :to add a container.\n R or r :to retrieve the information of one container.\n T or t :to retrieve the information of all containers.\n W or w :to retrieve the total weight of the loaded containers.\n X or x :to exit the program\n");
char ch;
do
{
fflush(stdin); //use to clear the buffer
printf("\n Select Operation...\n ..........................\n");
scanf("%c",&ch);
if(ch=='A' || ch=='a')
{
int i=0;
for(;i<n;i++)
{
if(arr[0][i]==-1)
break;
}
if(i==n)
printf("Array is full..\n");
else
{
printf("Enter ID number.\n");
scanf("%f",&arr[0][i]);
printf("Length in meters\n");
scanf("%f",&arr[1][i]);
printf("Width in meter . \n ");
scanf("%f",&arr[2][i]);
printf("Height in meters.\n");
scanf("%f",&arr[3][i]);
printf("Weight in Kg \n");
scanf("%f",&arr[4][i]);
}
}
if(ch=='R' || ch=='r')
{
float id;
printf("Enter the ID number to be searched..\n");
scanf("%f",&id);
int i=0;
for(;i<n;i++)
{
if(arr[0][i]==id)
break;
}
if(i==n)
printf("The container which Id number is %f not loaded.\n",id);
else
{
printf("ID number: %f\n length in meters: %f \n width in meter : %f \n height in meters : %f \n weight in Kg : %f \n volume :%f\n",arr[0][i],arr[1][i],arr[2][i],arr[3][i],arr[4][i],arr[1][i]*arr[2][i]*arr[3][i]);
}
}
if(ch=='T' || ch=='t')
{
int i;
for(i=0;i<n;i++)
{
printf(" Containers %d\n ",i+1);
printf("ID number: %f\n length in meters: %f \n width in meter : %f \n height in meters : %f \n weight in Kg : %f \n",arr[0][i],arr[1][i],arr[2][i],arr[3][i],arr[4][i]);
printf(".............................................................................\n");
}
}
if(ch=='W' || ch=='w')
{
float total_weight;
int i;
for( i=0;i<n;i++)
{
if(arr[4][i]!=-1)
total_weight+=arr[4][i];
}
printf("total_weight=%f",total_weight);
}
}while(ch!='X' && ch!='x');
return 0;
}