-2

I am new in C++. This is my program, which is generating a random number from 1 to 270:

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

void myFuncB(int par)
{
    std::cout << "unique = " << par; //print the unique number      
}

void myFuncA()
{   
    int parameter = 0;
    std::cout << "Random numbers generated between 1 and 270:" << "\n";
    for(int i = 0; i < 270; i++)
    {
        parameter = rand() % 270;
        std::cout << "parameter=" << parameter << "\n";
        // myFuncB(parameter);
    }    
}

int main() {
    srand(time(0));  // Initialize random number generator.
    myFuncA();
    return 0; 
}

I want myFuncB(int par) to print the unique values only, not repeating any int value.

How can I do this without using a data structure like an array?

marc_s
  • 704,970
  • 168
  • 1,303
  • 1,425
frincit
  • 1
  • 3
  • You should use the data structure named "set". Bitset in your case. – 273K May 15 '22 at 19:57
  • You can't. You must need some container to keep track if a number is already used. The simplest solution would be a set. Without that you don't know the history so you don't know if you already had that number or not. – simre May 15 '22 at 19:57
  • "without using data structure" is not how any reasonable programmer would try to solve this problem. Can you explain why are are averse to using _data that has some structure_? – Drew Dormann May 15 '22 at 20:03
  • Your random number is also _not_ "between 1 and 270". It is in the range of 0 to 269, inclusive, with some numbers occurring more often than others. – Drew Dormann May 15 '22 at 20:06
  • please help me .How i can use `Bitset` for generating the logic of unique numbers? – frincit May 15 '22 at 20:14
  • _I am new in C++_ [here](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) would be a good place to have a browse around. – Paul Sanders May 15 '22 at 21:02
  • `std::bitset<270> mem;` and then `if (!mem.test(parameter)) { mem.set(parameter); std::cout << parameter << std::endl; }` – Goswin von Brederlow May 15 '22 at 22:26
  • @Goswin von Brederlow, can we use `bitset` for `string` not for `int` .... Kindly tell me code string also ? Thanks – frincit May 16 '22 at 22:02
  • For strings you need a set. But you might mean chars, in which case a string would do. – Goswin von Brederlow May 16 '22 at 22:31
  • Kindly write some small the code snippet for `string`? – aaaaaaaaaaaaaaaaaaaaaaaaa May 16 '22 at 22:38

0 Answers0