array object initializing values

Hello everyone I am trying to declare a array of 10 objects with values attached to them by a random number generator. After declaring a array of 10 accounts how do i make their id numbers 0-9 and generate a balance between 100-10000 and a interest rate of 1.5-4.5. Then after these values are assigned to each object what is the syntax for getting them to use them in further calculations. I do not want the values to reset until after the program terminates.

 
Here's the basics, you should be able to do more processing from here:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

#include <random>
#include <iostream>

// An account has an id, a balance, and an interest rate
class Account {
  public:
      
    Account() {}
    
    Account(int id, int balance, double interest_rate)
    : id(id), balance(balance), interest_rate(interest_rate)
    {}
    
    int    id;
    int    balance;
    double interest_rate;
};

int main()
{
    // from <random>, search each class name to know more
    // http://www.cplusplus.com/reference/random/
    std::default_random_engine generator;
    std::uniform_int_distribution<int> random_balance(100, 10000);
    std::uniform_real_distribution<double> random_interest_rate(1.5, 4.5);
    
    const int N = 10;
    Account accounts[N];
    
    for (int i = 0; i < N; i++)
    {
        // set id, and random values for balance and interest rates
        int id = i;
        int balance = random_balance(generator);
        int interest_rate = random_interest_rate(generator);
        
        // set account
        accounts[i] = Account(id, balance, interest_rate);
    }
    
    // do further processing.
    
    for (int i = 0; i < N; i++)
    {
        std::cout << accounts[i].balance << std::endl;
    }
}

closed account (48T7M4Gy)
@OP Please stick to a single thread. So far you have 3 threads on essentially the same topic. Multiple posting wastes peoples time in losing context, duplication of volunteer effort and is not good for you. Please be courteous and pick just one thread now and close off the others with a green tick to show they are complete and that there is no need to revisit.

http://www.cplusplus.com/forum/beginner/213274/
Topic archived. No new replies allowed.