0

How do I add numbers into an array from a loop that creates random numbers? I also need to sort these numbers in a certain way. The next number cannot be greater than the previous one. Can someone help me with this? Here is a copy of some of the code I made.

#include <cstdlib>
#include <ctime>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  srand((unsigned) time(0));
  
  int randomNumber;
  int counter = 1;
  int emptyArray[5] = {0};

  while (counter <= 5){
     randomNumber = (rand() % 30) + 40;
     cout << " ------------ ";
     cout << '\n';
     cout << '\n';
     cout << randomNumber << '\n';
     cout << '\n';
     cout << '\n';
     cout << counter << " number of times" << '\n';   
     cout << " --------- ";
     counter++;    
     cout << emptyArray[randomNumber] << "  ";
   }
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 2
    Your question contains two questions in one: 1. How to create an array of random numbers? 2. How to sort an array of numbers? Please focus your question on one issue only. You may want to read this: [Why is “Can someone help me?” not an actual question?](https://meta.stackoverflow.com/q/284236/12149471) – Andreas Wenzel May 06 '22 at 02:56
  • 1
    Make it: `emptyArray[counter++] = randomNumber;` – Adriel Jr May 06 '22 at 02:57
  • 1
    Also, prefer `for` loops (or range loops) for that. And learn to use std::vector. – Adriel Jr May 06 '22 at 02:58
  • You should start your counter from 0 instead of 1 and do `emptyArray[counter] = randomNumber;`. After that you can use any sorting technique to sort your array. – Avinash May 06 '22 at 03:00
  • More modern way to generate random numbers https://stackoverflow.com/questions/288739/generate-random-numbers-uniformly-over-an-entire-range/20136256#20136256 – Sebastian May 06 '22 at 03:07
  • 1
    @AdrielJr using a std::vector for fixed size generates much overhead. The advice should be std::array instead – Klaus May 06 '22 at 07:09
  • `emptyArray[randomNumber]` accesses the array out of bounds. `randomNumber` does not hold valid array indexes. – Remy Lebeau May 06 '22 at 09:34
  • @Klaus I mentioned std::vector because vector is the data structure that adds more value to a developer. Still, you are right, for small static arrays std::array is better since it uses the stack instead of the heap so it is faster. – Adriel Jr May 07 '22 at 01:31
  • @AdrielJr It has nothing to do with stack or heap. You can also allocate a std::array on heap if you like. It also has nothing to do with speed as long you preallocate your vector and don't change the size. But it is simply the wrong type if most features are not used. The "value" for a developer is also the same as it offers all needed interfaces for range based loops, index access and so on. – Klaus May 07 '22 at 07:19
  • @Klaus You are missing the point. The use of std::array is very niche (in fact, someone can easily have crashes with std::array if the array Is big enough while std::vector would work just fine). You can do more things with vector and this is much more important since he is a newbie. The performance of both is about the same (except for creation, which as you said, can be amended with pre-allocation in certain cases). What I mean by adding value, is that for >50% of the problems, vector is the data structure of choice. – Adriel Jr May 08 '22 at 02:30
  • @AdrielJr Should we advice newbies to create useless overhead for convenience? Is access of vector out of bounds not UB? What can you do more with vector as with array if number of elements is known in compile time? So I absolute disagree! – Klaus May 09 '22 at 06:15
  • From now on I will ask one question at a time. But still I haven't found a solution. I have tried emptyArray[counter++] = randomNumber; and emptyArray[counter] = randomNumber;. They still don't work. My goal is to use a loop to create many random numbers in whatever range I want and store them in an array and then display them. But I haven't found a way to do that. Normally the array shouldn't have values when initiated and I don't want to specify how many random numbers will be in the array once the loop is finished. Is there anyone who knows how to do this in C++ or any language? – Amaury Silva May 11 '22 at 11:02

0 Answers0