Это то, что я сделаю, чтобы найти самые низкие операционные затраты:
#include <string>
#include <iostream>
#include <stdio.h>
using namespace std;
int NUM_BOATS = 5;
struct Boat {
int mpg;
int maintenanceCost;
int purchaseCost;
string name;
int operatingCost; // total of your calculation
};
void caculation(Boat& boat, int yearsService){
int totalMiles = 15000 * yearsService;
int gasTotal = totalMiles / boat.mpg;
int maintenanceTotal = boat.maintenanceCost * yearsService;
boat.operatingCost = boat.purchaseCost + gasTotal + maintenanceTotal;
}
void print(Boat& boat){
cout << "mpg: " << boat.mpg << endl;
cout << "maintenanceCost: " << boat.maintenanceCost << endl;
cout << "purchaseCost: " << boat.purchaseCost << endl;
cout << "name: " << boat.name << endl;
cout << "operatingCost: " << boat.operatingCost << endl;
}
int main()
{
Boat BoatData[5] = { {30, 100, 5000, "Boat a", 0},
{20, 110, 5500, "Boat b", 0},
{35, 120, 4000, "Boat c", 0},
{10, 300, 1000, "Boat d", 0},
{40, 200, 3000, "Boat e", 0},
} ;
//User input
int costGas;
cout << "Please enter the first Boats cost of gas: "<<endl;
cin >> costGas;
cout << "The cost of gas you entered is " << costGas << endl;
int yearsService;
cout << "Please enter the number of years in service: " << endl;
cin >> yearsService;
cout << "The number of years in service you entered is " << yearsService << endl;
int iMin = 0;
int minCost = 99000; // a really high number
for(int i=0; i<NUM_BOATS; ++i){
print(BoatData[i]);
caculation(BoatData[i], yearsService); // calculate all your operatingCost
if (BoatData[i].operatingCost < minCost){ // and check if is the lowest operatingCost
iMin = i; // if it is, save that index
}
}
cout << "Your lowest operating cost is " << BoatData[iMin].operatingCost << " with the boat " << BoatData[iMin].name << endl;
return 0;
}
Но исходя из вашего кода и ваших комментариев, ваш подход заключается в сортировке массива, так что это то, что вы должны реализовать вВаш код:
#include <string>
#include <iostream>
#include <stdio.h>
using namespace std;
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
void swapName(string& x, string& y)
{
string temp(x);
x = y;
y = temp;
}
void bubbleSort(int arr[], string boatName[], int n)
{
int i, j;
for (i = 0; i < n-1; i++)
for (j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1])
{
swap(&arr[j], &arr[j+1]); // if you have to swap the data
swapName(boatName[j], boatName[j+1]); // swap also the names
}
}
void printArray(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
}
int main()
{
//Declarations
int numBoats = 5;
int boatData[5];
string boatName[5] = {"a", "gg", "tt", "hh", "jj"};//
int costGas;
int i;
int j;
int boatArray[5][3] = {{30, 100, 5000},
{20, 200, 10000},
{30, 200, 2000},
{25, 150, 12000},
{30, 50, 8000}};
//User input
cout << "Please enter the first boats cost of gas: "<<endl;
cin >> costGas;
cout << "The cost of gas you entered is " << costGas << endl;
int yearsService;
cout << "Please enter the number of years in service: " << endl;
cin >> yearsService;
cout << "The number of years in service you entered is " << yearsService << endl;
//Calculations
for (i = 0; i < numBoats; i++) {
int mpg = boatArray[i][1];
int maintenanceCost = boatArray[i][2];
int purchaseCost = boatArray[i][3];
int totalMiles = 15000 * yearsService;
int gasTotal = totalMiles / mpg;
int maintenanceTotal = maintenanceCost * yearsService;
int operatingCost = purchaseCost + gasTotal + maintenanceTotal;
boatData[i] = operatingCost;
}
//Print operating costs
cout<<"The operating costs of each boat is: "<<endl;
printArray(boatData, numBoats);
//Bubblesort
bubbleSort(boatData, boatName, numBoats); // pass also the name's array
printArray(boatData, numBoats);
cout<<"Your lowest operating cost is "<<boatData[0]<< " with the boat " << boatName[0] << endl; // print the name
return 0;
}