M
M
Mag1str2020-06-03 19:01:16
C++ / C#
Mag1str, 2020-06-03 19:01:16

Structure arrays?

Task:

Known data on the cost of each of the 15 car models and their type (passenger or truck). Find the average cost of cars.

Here is what I have written so far, I don’t know how to calculate further
#include <iostream>
#include <string.h>

using namespace std;

struct Auto
{
  char marka[15];
  int tup, price;
};

void Auto_desh(struct Auto *m, int n);

int main()
{
  const int n=2;
  Auto M[n];

  string marka [15]{
  "BMW", "Audi"};
  
  string tup [15]{
  "Vaz", "Leh"};
  
  string price [15]{
  "1000", "500"};
  
  cout<<"E taki auto\t";
  for (int i = 0; i < n; i++) {
  cout<<marka[i]<<"\t"<<tup[i]<<"\t"<<price[i]<<endl;
  }
return 0;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
edward_freedom, 2020-06-03
@Mag1str

#include "stdafx.h"
#include <iostream>
#include "list"
#include <algorithm>
#include <sstream>
#include <string>
using namespace std;

class Car {
public:
  enum Type
  {
    Passenger,
    Cargo
  };
  int Price;
  string Model;
  Type TypeAuto;
  

  Car(int price, string model, Type type) {
    this->Model = model;
    this->Price = price;
    this->TypeAuto = type;
  }
  string ToString() {

    std::stringstream result;
    result << "Model: " << this->Model <<
      ", Price: " << this->Price <<
      ", Type: " << this->TypeAuto;
    return result.str();
  }
};

class FilterCars {
public:
  list<Car> Cars;
  int AveragePrice;
  FilterCars(list<Car> cars, int averagePrice) {
    this->Cars = cars;
    this->AveragePrice = averagePrice;
  }
};

void ShowCars(list<Car> cars) {
  for (auto  car : cars) {
    std::cout << car.ToString() << endl;;
  }
}

FilterCars AveragePriceByType(list<Car> cars, Car::Type type) {
  list<Car> selectCars;
  std::copy_if(cars.begin(), cars.end(), std::back_inserter(selectCars), [type](Car car) {
    return car.TypeAuto == type;
  });
  auto prices = 0;
  for (auto car : selectCars) {
    prices += car.Price;
  }
  auto average = prices / selectCars.size();
  return FilterCars(selectCars, average);
}



int main()
{
  setlocale(LC_ALL, "russian");

  auto cars = {
    Car(40000, "Tesla", Car::Type::Passenger),
    Car(34000, "Chevrolet", Car::Type::Passenger),
    Car(324000, "Mercedes ", Car::Type::Passenger),
    Car(114000, "Volvo ", Car::Type::Cargo),
    Car(222000, "BMW ", Car::Type::Cargo),
  };

  

  auto cargo = AveragePriceByType(cars, Car::Type::Cargo);
  auto passenger = AveragePriceByType(cars, Car::Type::Passenger);
  
  cout << "Вывод грузовых автомобилей: " << endl;
  ShowCars(cargo.Cars);
  cout << "Средняя цена грузовых автомобилей: " << cargo.AveragePrice << endl << endl;

  cout << "Вывод легковых автомобилей: " << endl;
  ShowCars(passenger.Cars);
  cout << "Средняя цена легковых автомобилей: " << passenger.AveragePrice << endl << endl;

  system("pause");


    return 0;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question