Question
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
using namespace std; using std::ofstream; struct comma : public std::numpunct
{
explicit comma(size_t refs = 0) : std::numpunct
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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started