1. The following requirements are for a program that maintains boats docked in a
ID: 3781714 • Letter: 1
Question
1. The following requirements are for a program that maintains boats docked in a harbor.
a. Define a structure to store the following data on boats docked in the harbor: owner’s last name, telephone number, boat license number, both length, boat name, and dock number where the boat is currently docked.
b. Write a function that called createBoat() that creates a boat by requesting the data from the user and returns it back.
c. Write a function called displayBoat() that takes a boat and displays its details.
d. Define an array of maximum of 500 boats that can represent the boats on that harbor.
e. Write a function called insertBoat() that inserts a new boat to your harbor.
f. Write a function that searchBoat() that searches a boat in the harbor by given its license number. It returns true if it is at the harbor.
Need in C(stdio.h and stdlib.h)
Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
int count=0;
//strcture of boat
struct Boat
{
char lname[20];
int tele;
int license;
int length;
char boatname[20];
int dockno;
}boats[3];//array of boat structure
//it will accept each details of boat from user
struct Boat createBoat()
{
printf("For Boat no %d ",count);
char lname[20];
struct Boat ob;
printf("Enter last name of owner ");
scanf("%s",ob.lname);
printf("Enter Telephone of owner ");
scanf("%d",&ob.tele);
printf("Enter license of owner ");
scanf("%d",&ob.license);
printf("Enter length of Boat ");
scanf("%d",&ob.length);
printf("Enter name of Boat ");
scanf("%s",ob.boatname);
printf("Enter Dock number of Boat ");
scanf("%d",&ob.dockno);
return ob;
}
//Display details of boat
void displayBoat(struct Boat ob)
{
printf("last name of owner ");
printf("%s",ob.lname);
printf(" Telephone of owner ");
printf("%d",ob.tele);
printf(" license of owner ");
printf("%d",ob.license);
printf(" length of Boat ");
printf("%d",ob.length);
printf(" name of Boat ");
printf("%s",ob.boatname);
printf(" Dock number of Boat ");
printf("%d",ob.dockno);
}
//function to insert boat
void insertBoat(struct Boat t)
{
boats[count++]=t;
}
//search boat in array with license number
void searchBoat(int license)
{int i;
for ( i = 0; i < count; ++i)
{
if(license==boats[i].license)
{
displayBoat(boats[i]);
return;
}
}
printf("Boat is not found ");
}
int main(int argc, char const *argv[])
{
int i;
//get details and add boat
for(i=0;i<3;i++)
{
struct Boat temp;
temp=createBoat();
insertBoat(temp);
}
//print boat details
for(i=0;i<3;i++)
{
printf("For Boat no %d ",i);
displayBoat(boats[i]);
}
printf("Enter license number to search ");
int l;
scanf("%d",&l);
searchBoat(l);
return 0;
}
================================================================
Output:
akshay@akshay-Inspiron-3537:~/Chegg$ gcc boat.c
akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out
For Boat no 0
Enter last name of owner
Abhang
Enter Telephone of owner
123
Enter license of owner
321
Enter length of Boat
212
Enter name of Boat
Ranger
Enter Dock number of Boat
1
For Boat no 1
Enter last name of owner
Abhang
Enter Telephone of owner
456
Enter license of owner
654
Enter length of Boat
546
Enter name of Boat
Karter
Enter Dock number of Boat
2
For Boat no 2
Enter last name of owner
Shinde
Enter Telephone of owner
789
Enter license of owner
987
Enter length of Boat
879
Enter name of Boat
Pollo
Enter Dock number of Boat
3
For Boat no 0
last name of owner
Abhang
Telephone of owner
123
license of owner
321
length of Boat
212
name of Boat
Ranger
Dock number of Boat
1For Boat no 1
last name of owner
Abhang
Telephone of owner
456
license of owner
654
length of Boat
546
name of Boat
Karter
Dock number of Boat
2For Boat no 2
last name of owner
Shinde
Telephone of owner
789
license of owner
987
length of Boat
879
name of Boat
Pollo
Dock number of Boat
3Enter license number to search
987
last name of owner
Shinde
Telephone of owner
789
license of owner
987
length of Boat
879
name of Boat
Pollo
Dock number of Boat
3
=====================================================
For demo purpose i have taken 3 boats.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.