E
E
Evgeny Yakushov2018-03-09 16:36:16
C++ / C#
Evgeny Yakushov, 2018-03-09 16:36:16

How to pass variables to a function and return them?

how to pass an array and other variables to a function and return them, and then output them through main. Required through the function

#include "stdafx.h" 
#include "stdio.h" 
#include "iostream"

using namespace std;

int enter(int number_rocket[4], int amount, int min_fuel, int min_E, int min_mass, int max_umass, int max_speed) {

  int fuel, E, mass, umass, speed;

  cout << "\tЗапускаем идеальную ракету Falcon 9\n\nВведите кол-во ракет:\t";

  cin >> amount;

  while ( amount < 1) {
    cout << "\n\tКол-во ракет не может быть меньше 1!\n\nВведите повторно кол-во ракет:\t";
    cin >> amount;
  }

  cout << "\n\tХарактеристика ракеты 1\n"
    << "\nВведите кол-во необходимого топилва для взлета:\t";
  cin >> fuel;
  cout << "\nВведите кол-во энергии затраченной на взлет:\t";
  cin >> E;
  cout << "\nВведите массу ракеты:\t";
  cin >> mass;
  cout << "\nВведите полезную массу:\t";
  cin >> umass;
  cout << "\nВведите максимальную скорость ракеты:\t";
  cin >> speed;

  min_fuel = fuel;
  number_rocket[0] = 0;

  min_E = E;
  number_rocket[1] = 0;

  min_mass = mass;
  number_rocket[2] = 0;

  max_umass = umass;
  number_rocket[3] = 0;

  max_speed = speed;
  number_rocket[4] = 0;

  for (int i = 1; i < amount; i++) {
    cout << "\n\tХарактеристика ракеты " << i + 1 << "\n"
      << "\nВведите кол-во необходимого топилва для взлета:\t";
    cin >> fuel;
    cout << "\nВведите кол-во энергии затраченной на взлет:\t";
    cin >> E;
    cout << "\nВведите массу ракеты:\t";
    cin >> mass;
    cout << "\nВведите полезную массу:\t";
    cin >> umass;
    cout << "\nВведите максимальную скорость ракеты:\t";
    cin >> speed;

    if (fuel < min_fuel) {
      min_fuel = fuel;
      number_rocket[0] = i;
    }
    if (E < min_E) {
      min_E = E;
      number_rocket[1] = i;
    }
    if (mass < min_mass) {
      min_mass = mass;
      number_rocket[2] = i;
    }
    if (umass > max_umass) {
      max_umass = umass;
      number_rocket[3] = i;
    }
    if (speed > max_speed) {
      max_speed = speed;
      number_rocket[4] = i;
    }
  }

}

int main(){

  setlocale(LC_ALL, "rus");

  int number_rocket[4];
  int amount, min_fuel = 0, min_E = 0, min_mass = 0, max_umass = 0, max_speed = 0;

  enter(number_rocket, amount, min_fuel, min_E, min_mass, max_umass, max_speed);

  cout << "\n\n\tИдеальная ракета будет с характеристиками:"
    << "\nМинимально затраченное топливо:\t" << min_fuel << "\tу ракеты #:" << number_rocket[0] + 1
    << "\nМинимальная энергия для взлета:\t" << min_E << "\tу ракеты #:" << number_rocket[1] + 1
    << "\nМинимальная масса ракеты:\t" << min_mass << "\tу ракеты #:" << number_rocket[2] + 1
    << "\nМаксимальная полезная масса:\t" << max_umass << "\tу ракеты #:" << number_rocket[3] + 1
    << "\nМаксимальная скорость ракеты:\t" << max_speed << "\tу ракеты #:" << number_rocket[4] + 1 << endl;

  system("pause");
  return 0;
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
T
tugo, 2018-03-09
@tugo

Place these variables in a struct.

int number_rocket[4];
  int amount, min_fuel = 0, min_E = 0, min_mass = 0, max_umass = 0, max_speed = 0;

Pass structure to function by reference

C
CityCat4, 2018-03-09
@CityCat4

It is possible, in principle, to pass five pointers, but this is crooked and cumbersome. In such cases, they do, as tugo said - a structure, pass a pointer to the structure and do what is necessary with it in the function

I
Ivan Klimenko, 2018-03-09
@yeswell

Read about passing arguments to functions. In short: it happens by value and by pointer (reference). In C language it is possible only by value. But the value can be a pointer to a variable. In C++, you can also follow the link.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question