In: Computer Science
Description
CSCI 1151, Fall 2019 Programming Assignment 5 – Structure
Construct an array of a structure to store the information of vehicles. This structure should consist of: an integer for the vehicle identification number (ID); an integer for the mile driven; an integer for the number of gallons used by the vehicle; and a string for the vehicle’s manufacture. Write a C program that get (input) the information of the vehicles from a file, cardb.txt, one structure per vehicle.
Your program should sort the vehicles by their identification numbers (ID) in ascending order. Prompt the user to enter the vehicle ID number and apply binary search for the vehicle. Output the information of the vehicle on the console, if the vehicle is not found then output “Vehicle not found”. Assume the maximum number of vehicles is 100 which means you need to allocate an array of size 100 of the structure you declared. You may use dynamic memory allocation to get exact memory size. There will be an extra 20 points if you use dynamic memory allocation.
Input
File name: cardb.txt
Output
1) You need to have at least two functions, sort() and search(), in your program. You may implement any sorting algorithm for your sort() function to sort the vehicles, you have to implement the binary search algorithm for your search() function for searching vehicles, the search key is the vehicle ID number.
2) To test your sorting, you should output the vehicle information on the console (refer to the sample output example).
3) A while loop continue prompt user for the vehicle ID number and output the information of the searched vehicle on the console. If not find, then output “Vehicle not found”. After receive a negative vehicle ID number from the user, the while loop will be terminated.
Format of the Input File
Up to 100 records, one per vehicle, a new line between two
adjacent records
Four fields (columns) per records (line): vehicle ID number, Miles
Driven, Gallons of Gas used, Vehicle Manufactural. A blank (space)
between adjacent fields. Refer to the attached file cardb.txt
Specifications:
The name of the source code file must be exactly Lab05.c.
Comments at the top with your name, e-mail, date and the course you are taking.
Submit your program to Blackboard.
Read the syllabus for the late policy.
I will test your program as following example: the submitted program name is Lab05.c
gcc –o Lab05 Lab05.c Lab05 cardb.txt
Sample output example:
Following are the information of all vehicles: Vehicle ID # Miles Driven Gallons Used Manufacture ============ ============ ============ ===========
25 1450 62 Ford
36 3240 136 Chrysler
..........
4753 850 0 Tesla
Please enter the vehicle ID number: 25 <Enter>
1,450 Miles Driven 62 Gallons Used Manufactured by Ford
Please enter the vehicle ID number: 80 <Enter> Vehicle not found
Please enter the vehicle ID number: -1 <Enter> Good Bye!
25 1450 62 Ford 36 3240 136 Chrysler 44 1792 76 Audi 52 2360 105 Chevrolet 68 2144 67 BMW 2365 20 2 Ford 2103 105 3 Volkswagon 4753 850 0 Tesla 1499 425 14 Chevrolet 3278 378 21 Ford
I have implemented the complete c program which has following:
Also, i have used dynamic memory allocation for structure array as mentioned in the question.
Note: You may check the screenshot of the code to have better understanding of the indentation.
Here is the my text file, you may add more entries.
C code:
#include<stdio.h>
#include<stdlib.h>
//declare struct
struct vehicle{
int id;
int miles;
int gallons;
char manufacturer[20];
};
//sort function
void selectionSort(struct vehicle arr[], int n)
{
int i, j, min_idx;
struct vehicle temp;
for (i = 0; i < n-1; i++)
{
// Find the minimum element in unsorted array
min_idx = i;
for (j = i+1; j < n; j++)
if (arr[j].id < arr[min_idx].id)
min_idx = j;
// Swap the found minimum element with the first element
temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}
//search function
struct vehicle searchVehicle(struct vehicle arr[], int l, int r,
int x)
{
while (l <= r) {
int m = l + (r - l) / 2;
// Check if x is present at mid
if (arr[m].id == x)
return arr[m];
// If x greater, ignore left half
if (arr[m].id < x)
l = m + 1;
// If x is smaller, ignore right half
else
r = m - 1;
}
// if we reach here, then element was
// not present
struct vehicle v = {-1,-1,-1,""};
return v;
}
//main function
int main()
{
int maxSize = 100,i;
int n=0; //number of records
struct vehicle *details;
//create file object to open the input file
FILE* infile = fopen("cardb.txt","rb"); //read mode
//check if file exists
if(!infile)
{
printf("File does not exists.\n");
return 0;
}
//create array using dynamic memory allocation of size 100
details = (struct vehicle*) malloc (maxSize * sizeof(struct
vehicle));
if(details == NULL)
{
printf("Memory not allocated.\n");
exit(0);
}
char line[100]; //read individual lines from the file
//take entries from file and fill in details array
while(fgets(line,100,infile) != NULL)
{
//temporary variables to copy from the string of file
char idTemp[9];
char milesTemp[9];
char gallonsTemp[9];
char manTemp[20];
int j;
int i=0;
while(isspace(line[i])){ //skip empty spaces
i++;
}
j=0;
while(!isspace(line[i])) //copy id
{
idTemp[j++] = line[i++];
}
idTemp[j] = '\0';
while(isspace(line[i])){ //skip empty spaces
i++;
}
j=0;
while(!isspace(line[i])) //copy miles
{
milesTemp[j++] = line[i++];
}
milesTemp[j] = '\0';
while(isspace(line[i])){ //skip empty spaces
i++;
}
j=0;
while(!isspace(line[i])) //copy gallons
{
gallonsTemp[j++] = line[i++];
}
gallonsTemp[j] = '\0';
while(isspace(line[i])){ //skip empty spaces
i++;
}
j=0;
while(line[i] != '\n') //copy manufacturer name
{
manTemp[j++] = line[i++];
}
manTemp[j] = '\0';
//enter these details in structure details
//first convert string to int then put in structure
sscanf(idTemp, "%d",&details[n].id);
sscanf(milesTemp,"%d",&details[n].miles);
sscanf(gallonsTemp,"%d",&details[n].gallons);
strcpy(details[n].manufacturer,manTemp); //copy string
n++; //increment number of entries
}
//sort the entries
selectionSort(details, n);
//print sorted vehicle details
printf("Sorted vehicles: \n");
i=0;
while(i<n)
{
printf("%d %d %d %s\n",details[i].id,details[i].miles,
details[i].gallons,details[i].manufacturer);
i++;
}
//while loop for user to search vehicles
int x; //id from user
while(1)
{
printf("Please enter the vehicle ID number: ");
scanf("%d",&x); //read input
if(x < 0){ //negative input- break
printf("Good Bye!\n");
break;
}
struct vehicle found = searchVehicle(details,0,n-1,x);
if(found.id == -1) //not found
{
printf("Vehicle not found\n");
}
else //found
{
printf("%d Miles Driven %d Gallons Used Manufactured by
%s\n",
found.miles,found.gallons,found.manufacturer);
}
}
//close the file
fclose(infile);
return 0;
}
Output:
Screenshot of code:
I hope you find the answer helpful :)