Created
March 24, 2020 01:49
-
-
Save AmosAidoo/17c3c014638fad56e7404868b1caf8d2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdlib> | |
#include <ctime> | |
#include <iostream> | |
#include <fstream> | |
#include <iomanip> | |
#include <cmath> | |
using namespace std; | |
//Function prototypes | |
char* genName(int n); | |
int getBinIndex(int n); | |
int main () { | |
srand(time(NULL)); | |
ofstream file; | |
string categories[] = {"Electronics", "Clothing", "Footwear", "Books", "Appliance", "Apps"}; | |
double minn[] = {35, 45, 99, 7, 150, 0.5}; | |
double maxx[] = {1500, 550, 339, 45, 600, 4.99}; | |
int bin[10]{0}; | |
//Data types | |
int id; | |
string name; | |
string category; | |
double price; | |
bool two_day_shipping; | |
int numclicked; | |
//Random number index, bin index and maximum click index | |
int randIdx, bin_idx, max_clicks = -1; | |
//Store the file name and most popular data | |
string fname, most_popular = ""; | |
for (int i = 0; i < 3; i++){ | |
cout << "Sample " << i << "\n"; | |
cout << left << setw(5) << "id" << setw(12) << "name" << setw(10) << "price" << setw(15) << "category" << setw(10) << "2D ship" << setw(15) << "numclicked" << "\n"; | |
//String stream to collect the 1000 randomly generated data | |
stringstream data; | |
for (id = 1; id <= 1000; id++) { | |
//Stringstream to collect data line by line | |
stringstream line; | |
randIdx = rand()%6; | |
name = new char[randIdx+5]; //Allocate memory for the name | |
name = genName(randIdx+5); | |
category = categories[randIdx]; | |
//Compute random value between minimum price and maximum price | |
double tmp = (double)rand()/(maxx[randIdx]-minn[randIdx]); //quotient | |
tmp = (tmp - floor(tmp))*(maxx[randIdx]-minn[randIdx]); //remainder | |
price = tmp; | |
two_day_shipping = rand()%2; | |
numclicked = rand()%10001; | |
//Copy line into stream with nice formatting | |
line << left << setw(5) << id << setw(12) << name << fixed << setprecision(2) << setw(10) << price << setw(15) << category << setw(10) << two_day_shipping << setw(15) << numclicked << "\n"; | |
data << line.str(); | |
if (numclicked > max_clicks) | |
most_popular = line.str(); | |
//Print the first 50 lines | |
if (id <= 50) | |
cout << line.str(); | |
line.clear(); | |
//Compute the bin index | |
bin_idx = getBinIndex(numclicked); | |
bin[bin_idx]++; | |
} | |
//Output Statistics | |
cout << "\nStatistics\n\n"; | |
for (int i = 0; i < 9; i++) { | |
cout << "Bin " << i << ":" << bin[i] << "\n"; | |
} | |
//Print out the most popular item | |
cout << "\nMost popular item\n"; | |
cout << most_popular; | |
//Take the name of the file | |
cout << "Please enter the name of the file: "; | |
cin >> fname; | |
//Write data to the file | |
file << data.str(); | |
delete[] name; //free memory | |
data.clear(); | |
file.close(); | |
} | |
} | |
//Generate a random name between 5 and 10 | |
char* genName(int n) { | |
char* name = new char[n+1]; | |
name[n] = 0; | |
for (int i = 0; i < n; i++) { | |
name[i] = 'a' + (rand() % 26); | |
} | |
return name; | |
} | |
//Compute the index for each bin | |
int getBinIndex(int n) { | |
int idx; | |
switch(n) { | |
case 0 ... 1000: idx=0;break; | |
case 1001 ... 2000: idx=1;break; | |
case 2001 ... 3000: idx=2;break; | |
case 3001 ... 4000: idx=3;break; | |
case 4001 ... 5000: idx=4;break; | |
case 5001 ... 6000: idx=5;break; | |
case 6001 ... 7000: idx=6;break; | |
case 7001 ... 8000: idx=7;break; | |
case 8001 ... 9000: idx=8;break; | |
case 9001 ... 10000: idx=9;break; | |
} | |
return idx; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment