Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ - Please see my program below. I keep getting a 0 for the 10's and it's not generating a total of 10,000 random numbers.

C++ - Please see my program below. I keep getting a 0 for the 10's and it's not generating a total of 10,000 random numbers. What am I missing? Please bold or highlight what you find wrong or correct. Thank you!

Generate 10,000 random numbers in the range of 1 to 10, and count the number of 1s, 2s, 3s, etc. Store your counts in an array/list instead of using variables like total_ones or total_twos. You should have around a 1,000 of each number, like 978 1s for example. Once you have your array loaded with the random numbers, print the results. Print out the total number of 1s, 2s, 3s, 4s, 5s etc .

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */

/* * File: main.cpp * Author: * * Created on September 17, 2018, 7:38 PM */

#include //to use cout and cin and endl// allows using cout without std::cout #include // to use string data type #include //to use strlen, strcmp, strcpy #include //for pow function,sqrt,abs #include // for set precision, setw, #include //for file input #include // to use assert to disable place before #define NDEBUG #include //for random numbers, exit function #include //time function used for rand seed #include // for toupper, tolower #include #include #include //Random Numbers

using namespace std; using std::ofstream; struct comma : public std::numpunct

{

explicit comma(size_t refs = 0) : std::numpunct(refs) {} //Create a comma

virtual char do_thousands_sep() const { return ','; }

virtual std::string do_grouping() const { return "\003"; }

};

/* * */ int main(int argc, char** argv) { ofstream outfile; locale global;

locale withgroupings(global, new comma);

locale was = cout.imbue(withgroupings); int numbers [11]; srand(time(0)); for(auto &x:numbers) { x=0; } outfile.open("Homework");

for (int r=1; r<=10000; r++) { numbers[rand()%10]++; } for (int r=1; r<=10; r++) { std::cout << "Number " << r << " = " << numbers[r] << " times" << endl; outfile << "Number " << r << " = " << numbers[r] << " times" << " ";

} return 0; }

Number 1 = 947 times Number 2 = 1,014 times Number 3 = 998 times Number 4 = 988 times Number 5 = 946 times Number 6 = 1,055 times Number 7 = 1,009 times Number 8 = 1,037 times Number 9 = 1,056 times Number 10 = 0 times

RUN SUCCESSFUL (total time: 153ms)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Management System MCQs Multiple Choice Questions And Answers

Authors: Arshad Iqbal

1st Edition

1073328554, 978-1073328550

More Books

Students also viewed these Databases questions

Question

=+ Are they breakable for any reason?

Answered: 1 week ago

Question

=+When and under what circumstances are contracts renegotiated?

Answered: 1 week ago