0

So here's my problem i'm writing a c++ code to basically randomly generate X amount of numbers between [0-100] and then create a 2D array with all the numbers found sorted by smallest->biggest and print out each number found and how many times they are found.

this is the code i've written but there is a problem with it whenever i print the array with the numbers and how many times each one is found no matter how many times one number is found it's the same for all of them how can i fix that

#include <random>
#include <functional> 
#include <iostream> 

using namespace std;

std::default_random_engine generator;
std::uniform_int_distribution<int> data_element_distribution(0, 100);
auto random_element = std::bind(data_element_distribution, generator);

int main()
{
    int size, i;
    int different_numbers;
    cout<<"Enter the size of the Linear List:";
    cin>>size;

    int LinearList[size], TempList[size];

    for (i=0; i < size; i++)
    {
        int data_element = random_element();    //Filling the Linear List with random numbers 

        LinearList[i]=data_element;
        TempList[i]=LinearList[i];
    }
    int j, temp;
    
    for(j=size;j>1;j--)
    {
        for ( i=1; i<j; i++)
        {
            if(TempList[i]<TempList[i-1])
            {
                temp=TempList[i];                   //Sorting numbers
                TempList[i]=TempList[i-1];
                TempList[i-1]=temp;
            }
        }
    }
    different_numbers=1;
    int numbers[size];
    int *Histogram;
    Histogram=new int[different_numbers,1];

    for (i=0;i<size-1;i++)
    {
        if(TempList[i]!=TempList[i+1])
        {                                               //Finding the Size of the Histogram
            numbers[different_numbers]=TempList[i];     //Counting how many Times each Number is found
            Histogram[different_numbers,1]=1;    
            different_numbers++;
        }   
        else
        {
            Histogram[different_numbers,1]++;
        }   
    }
 
    for(i=1;i<different_numbers;i++)
    {
        Histogram[i,0]=numbers[i];                      //Printing numbers and Times found
        cout<<Histogram[i,0];
        cout<<" Is found "<<Histogram[i,1]<<" times."<<endl;

    }

    return 0;
}

edit: thank you guys for your comments and help i'll give it a try you're all life savers Xd

  • First thing you should do is change all the C arrays to C++ std:vector. Second your `Histogram` usage is out-of-bounds. – Goswin von Brederlow May 12 '22 at 12:48
  • 3
    Note that `int a[n]` is not standard c++ unless `n` is known compile time – Lala5th May 12 '22 at 12:49
  • 2
    `int size;` and then `int LinearList[size], TempList[size];` -- This is not valid C++. Dynamic arrays are done in C++ by using `std::vector`. – PaulMcKenzie May 12 '22 at 12:49
  • 4
    Also `new int[different_numbers,1]` does not do what you think it does. – Lala5th May 12 '22 at 12:50
  • 3
    Looks like you're learning C++ from an "online competition" website instead of using [good C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). A good C++ book would never show declaring arrays this way. – PaulMcKenzie May 12 '22 at 12:52
  • 1
    *randomly generate X amount of numbers between [0-100] and then create a 2D array with all the numbers found sorted by smallest->biggest and print out each number found and how many times they are found.* -- BTW, `std::map Histogram;` is a better choice, instead of all the work you're doing now. A simple one or two line loop to populate the map, and you have all the information right there inside the map. You don't need `int*` or any of the `new int[]` shenanigans going on. – PaulMcKenzie May 12 '22 at 12:56
  • 1
    [See this sample program](https://godbolt.org/z/5f35T8sxs). – PaulMcKenzie May 12 '22 at 13:05
  • thanks for the help guys. also PaulMcKenzie you just straight up did my exercise in like 20 mins when i've spent about 20hours ;( – Tetoros21Bill May 12 '22 at 13:38
  • @Tetoros21Bill -- I was afraid to post that as an answer, because 1) The `map` looks nothing like the code you are using now, and maybe you want to figure out why the code you wrote doesn't work correctly, and 2) Didn't want to potentially waste time giving a solution you're not allowed to use (for whatever reason). It's 2) that wastes time, since we have to put time to craft an answer, only to be rejected as something that "can't be used because my teacher says so", or some similar reasoning. – PaulMcKenzie May 12 '22 at 13:45
  • dont worry, the hard part of C++ is to avoid the infinite number of ways to write difficult and complicated code. It needs time to make lots of mistakes and then know how to not make them, eventually C++ can be simple and nice ;) C-arrays and using manual `new` are among the difficult parts btw – 463035818_is_not_an_ai May 12 '22 at 13:54

0 Answers0