1

I'm a new learner of C++. I want to get a CSV file like this:

   |  S0001    |   S0002   | S0003  | S0004  |  ...
0  | 10.289461 | 17.012874 |        |
1  | 11.491483 | 13.053712 |        |
2  | 10.404887 | 12.190057 |        | 
3  | 10.502540 | 16.363996 | ...    |  ...
4  | 11.102104 | 12.795502 |        | 
5  | 13.205706 | 13.707030 |        |
6  | 10.544555 | 12.173467 |        | 
7  | 10.380928 | 12.578932 |        | 
8  | 10.962240 | 12.615608 |        | 
9  | 10.690547 | 17.678212 |        | 
10 | 12.416197 | 13.769609 |        |
...    ...         ...         ...     ...  

The first column is a series of integers. The cell values are generated from a log-normal distribution.

Here is my code:

#include <iostream>
#include <vector>
#include <random> 
#include <fstream>
#include <stream>

//get first row: S0001, S0002, S0003, etc
std::string GetNextNumber(int lastNum)
{
    std::stringstream ss;
    ss << "S";
    ss << std::setfill('0') << std::setw(4) << lastNum;
    return ss.str();
}
int main()
{
    std::string ukey;
    int ticktime;
    double bid;
    double len = 1000;
    
    std::default_random_engine generator;
    std::lognormal_distribution<double> distribution(0.0,1.0);
    
    std::ofstream outFile;
    outFile.open("Data_bid.csv");
    
    for(int j=0; j<50; ++j){ 
        ukey = GetNextNumber(j+1);
        outFile<<","<<ukey;
    }
    outFile<<std::endl;
    for(int i=0; i<len; ++i){       
        ticktime = i;
        bid = distribution(generator)+10;
        outFile<<ticktime<<","<<bid<<std::endl;
    }   
    outFile.close();

    return 0;
} 

Right now, the code can only give me a result like this: enter image description here

I don't know how to append values for the rest of the columns. Can you help me out or provide some other methods to write the csv?

Cyan
  • 319
  • 2
  • 8
  • Right now you have a loop for each line that writes a time, and then a single value followed by a newline. If you want multiple values, I suggest you write out the time on its own, then have an internal loop that writes out a comma, and a value for each of the data columns. Then after the loop, write a newline. – paddy Sep 23 '21 at 02:52
  • 2
    *"I don't know how to append values"* -- append to what? Think about what your code does, and think about where something would need to be appended. If you keep adding this sort of detail to your question, you might find the answer before anyone gets to reply. (At the very least, add to your code something to generate values for the next few columns, like you did with `bid`.) – JaMiT Sep 23 '21 at 02:59
  • @paddy the actual numbers of `ukey` and `ticktime` are really large. There are 5000 ukey and 234000 ticktime. I don't quite understand what you mean. Could you show me how to do it a little? – Cyan Sep 23 '21 at 03:02
  • @JaMiT Appended values are also `bid`. In other words I have to randomly generate new values from log-normal distribution and append them – Cyan Sep 23 '21 at 03:03
  • @cyan I mean you need to have a loop inside a loop. The outer _i-loop_ is outputting a single _line_. The inner _j-loop_ is outputting all the values in that single line. I can't state it more clearly than this. – paddy Sep 23 '21 at 03:06
  • @paddy I got it. Thank you for the hints! – Cyan Sep 23 '21 at 03:27
  • @Cyan *"In other words I have to randomly generate new values from log-normal distribution and append them"* -- Great, you know what you have to do. So doing it (and updating your question) should be no problem, right? – JaMiT Sep 23 '21 at 03:53

1 Answers1

0

Thank you for @paddy 's hint and here is my own solution:

for(int i=0; i<len; ++i){       
    ticktime = i;
    outFile<<ticktime;
    for(int j=0; j<50; ++j){ 
        bid = distribution(generator)+10;
        outFile<<","<<bid;
    }
    outFile<<std::endl;
}
Cyan
  • 319
  • 2
  • 8