-1

I'm trying to code an algorithm in c++ to sort random numbers. I've coded a function to generate the random numbers that take as parameters a variable for the number of integers and an array to store the random numbers but when I declare the array there's an error telling me that structured binding declaration cannot have a type long int and that there's an empty structured binding declaration and also that expected initializer before 'Nb_alea' that is the name of the function. I don't understand quite well the meaning of those errors. Could you explain, please! Here is the code :

int main()
{
cout << "Hello world!"<< endl;
long x;
cout<<"Veuillez saisir le nombre d'entiers vous souhaitez trier par ordre décroissant : "<<endl;
cin>>x;
long tab_nb_alea[x];
int y;
cout<<"1-tri rapide, 2-tri fusion, 3-tri par tas"<<endl;
cin>>y;
string chemin_acces;
cout<<"Veuillez rentrer le chemin d'accès du dossier vers lequel vous souhaitez creer et enregistrer le ficher texte : "<<endl;
cin>>chemin_acces;
Nb_alea(x, tab_nb_alea);
}

long[] Nb_alea(long x, long tab_nb_alea[])
{
int max;
max=100;
if(x==0)
{
    cout<<"Probleme, vous devez selectionner un nb superieur a 0!"<<endl;
}else if(x==1)
{
    tab_nb_alea[x]=rand()%max;
    return tab_nb_alea;
}else if(x>1)
{
    srand(time(0));
    tab_nb_alea[x]=rand()%max;
    return Nb_alea(x-1, tab_nb_alea);
    }
}

||=== Build: Release in ProjetC++037_REVEILLAUD_Elie_Juin_2021 (compiler: GNU GCC Compiler) ===| C:\Users\elie\Documents\c\exo_listes_chainee\ProjetC++037_REVEILLAUD_Elie_Juin_2021\main.cpp|23|warning: structured bindings only available with -std=c++17 or -std=gnu++17| C:\Users\elie\Documents\c\exo_listes_chainee\ProjetC++037_REVEILLAUD_Elie_Juin_2021\main.cpp|23|error: structured binding declaration cannot have type 'long int'| C:\Users\elie\Documents\c\exo_listes_chainee\ProjetC++037_REVEILLAUD_Elie_Juin_2021\main.cpp|23|note: type must be cv-qualified 'auto' or reference to cv-qualified 'auto'| C:\Users\elie\Documents\c\exo_listes_chainee\ProjetC++037_REVEILLAUD_Elie_Juin_2021\main.cpp|23|error: empty structured binding declaration| C:\Users\elie\Documents\c\exo_listes_chainee\ProjetC++037_REVEILLAUD_Elie_Juin_2021\main.cpp|23|error: expected initializer before 'Nb_alea'| ||=== Build failed: 3 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|

  • 1
    You cannot return a C-array from a function. – sweenish May 11 '21 at 15:42
  • Please include error message in the question – 463035818_is_not_an_ai May 11 '21 at 15:43
  • As @sweenish already said, you can't return an array from a function due to it's [storage duration](https://en.cppreference.com/w/cpp/language/storage_duration#Storage_duration). The array has automatic storage duration (often refered as stack). To return an array the array must have dynamic storage duration, so it "outlives" a function return – gkhaos May 11 '21 at 15:56
  • 1
    the error mentions [structured bindings](https://en.cppreference.com/w/cpp/language/structured_binding) because the compiler gets a little confused. `long tab_nb_alea[x];` is not standard C++. Use `std::vector` instead – 463035818_is_not_an_ai May 11 '21 at 15:56
  • [Why aren't variable-length arrays part of the C++ standard?](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard) – 463035818_is_not_an_ai May 11 '21 at 15:56
  • no offense but there are some pieces of your code that look like it was written based on guessing how stuff works. Calling `srand(time(0));` each time you want to roll a random number is another one – 463035818_is_not_an_ai May 11 '21 at 15:58

1 Answers1

0

Like my comment says, you cannot return a C-array from a function. You have a ton of options here, and the best choice is to use std::vector. The other choices are std::array and a dynamic C-array.

Here's your code touched up to use a std::vector. I also use <random> and C++ PRNGs. They should be preferred over rand().

#include <iostream>
#include <random>  // CHANGED: Use C++ prngs over rand()
#include <string>
#include <vector>  // CHANGED: Use vector over C-array

// CHANGED: Moved function ahead of main() so code will compile
std::vector<long> Nb_alea(long size) {
  constexpr int max = 100;  // CHANGED: Condense declaration/initialization
  std::vector<long> tab_nb_alea;

  // CHANGED: Declaration of C++ prng and distribution
  static std::mt19937 prng(std::random_device{}());
  static std::uniform_int_distribution<long> range(0, max - 1);

  if (size == 0) {
    std::cout << "Probleme, vous devez selectionner un nb superieur a 0!"
              << std::endl;
  } else {
    // CHANGED: Loop to push back 'size' numbers into vector
    for (int i = 0; i < size; ++i) {
      tab_nb_alea.push_back(range(prng));
    }
  }
  return tab_nb_alea;
}

int main() {
  std::cout << "Hello world!" << std::endl;
  long x;
  std::cout << "Veuillez saisir le nombre d'entiers vous souhaitez trier par "
               "ordre décroissant : "
            << std::endl;
  std::cin >> x;
  // long tab_nb_alea[x];  // CHANGED: Not needed

  /*  CHANGED:: Commented out as it has no bearing on your question
  int y;
  std::cout << "1-tri rapide, 2-tri fusion, 3-tri par tas" << std::endl;
  std::cin >> y;
  std::string chemin_acces;
  std::cout << "Veuillez rentrer le chemin d'accès du dossier vers lequel vous "
               "souhaitez creer et enregistrer le ficher texte : "
            << std::endl;
  std::cin >> chemin_acces;
  */

  auto vec = Nb_alea(x);  // CHANGED: Because function returns vector,
                          // initialize here. Also adjusted function call to
                          // match the changed function

  // ADDED: Print vector to demonstrate it is working properly
  for (auto i : vec) {
    std::cout << i << ' ';
  }
  std::cout << '\n';
}
sweenish
  • 4,793
  • 3
  • 12
  • 23