Code in (c++) GOAL: Sort the cars (i.e. sort the array of structs) by ascending
ID: 3883794 • Letter: C
Question
Code in (c++)
GOAL: Sort the cars (i.e. sort the array of structs) by ascending price
Sample input file (cars.txt)
Objectives: The two main objectives of this project is to test your ability to (1) create and use structs with arrays, (2) work with pointers, pointer arithmetic, pass by-Value, pass by-Reference, pass by-Address, and (3) design, implement and test a solution to a given problem A review of your knowledge of arrays, iostream, file I/O and C-style strings is also included. Description: For this project, you are to create a program that will assist users who want to rent a car. You are ollowing the same data layout), and you must read in all of the car data from the file and store it given a datafile with 5 different cars file (the file is a priori known to have exactlr 5 entries, each in an array of structs. You must also create a menu with the functionality defined below. Although an example file is provided (Cars.txt), for grading purposes your project will be tested against a different test file that will not be provided to you beforehand. Our test file will be in the same format as the example file. The RentalCar struct will contain the following data members: year, an int (year of production) make, a C-string (char array of 10 maximum size) model, a C-string (char array of 10 maximum size) price, a float (price per day available, a bool 1 = true: 0 = false; try to display true false using the "std: :boolalpha" manipulator like: coutExplanation / Answer
#include<iostream>
#include<fstream>
using namespace std;
void copy (char p[10] , char q[10]){
for (int i = 0; i<10; i++)
p[i] = q[i];
}
struct car{
int year;
char make[10];
char model[10];
float price;
bool available;
};
void sort(struct car data[],int n){
struct car temp;
for (int i = 0; i<n; i++){
for (int j =i+1; j<n; j++){
if (data[i].price > data[j].price){
temp.year = data[i].year;
copy(temp.make,data[i].make);
copy(temp.model,data[i].model);
temp.price = data[i].price;
temp.available = data[i].available;
data[i].year = data[j].year;
copy(data[i].make,data[j].make);
copy(data[i].model,data[j].model);
data[i].price = data[j].price;
data[i].available = data[j].available;
data[j].year = temp.year;
copy(data[j].make,temp.make);
copy(data[j].model,temp.model);
data[j].price = temp.price;
data[j].available = temp.available;
}
}
}
}
void print(struct car data[],int n){
for (int i = 0; i<n; i++){
cout << data[i].year << " " << data[i].make << " " << data[i].model << "," << "$" << data[i].price << "," << "Availability = " ;
if (data[i].available == 0)
cout << "false" << endl;
else
cout << "true" << endl;
}
}
void input(struct car data[]){
char filename[20];
ifstream fin;
char avail[7];
cout << "Enter filename :" << endl;
cin >> filename;
fin.open("Cars.txt");
int count = 0;
while (fin >> data[count].year >> data[count].make >> data[count].model >> data[count].price >> avail){
if (avail[0] == 'f')
data[count].available = false;
else
data[count].available = true;
count++;
}
}
int main(){
struct car data[5];
int choice;
do {
cout << "1.Input filename ";
cout << "2.Print all ";
cout << "3.Sort ";
cout << "4.Quit ";
cout << "Enter your choice: ";
cin >> choice;
switch (choice){
case 1: input(data);
break;
case 2: print(data, 5);
break;
case 3: sort(data,5);
break;
}
}while (choice != 4);
return 0;
}
#include<iostream>
using namespace std;
void myStringCopy(char *destination, const char *source){
int i;
for (i = 0; *(source+i)!=''; i++)
*(destination +i) = *(source + i);
*(destination +i) = '';
}
int myStringCompare(char *destination, const char *source){
int i;
for (i = 0; *(source+i)!='' && *(destination+i)!=''; i++){
if (*(destination +i) > *(source +i))
return 2;
if (*(destination +i) < *(source +i))
return -1;
}
if (*(source+i)!='')
return -1;
if (*(destination+i)!='')
return 2;
return 0;
}
int main(){
char a[10] = "Hello";
char b[10] = "Hello";
char c[10] = "bello";
char d[10] = "nello";
myStringCopy(d,a);
cout << d << endl;;
if (myStringCompare(a,b) == 0)
cout << "They are equal ";
if (myStringCompare(a,c) != 0)
cout << "They are not equal ";
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.