Question

Why am I getting the same results with my rand() every time I run my C++...

Why am I getting the same results with my rand() every time I run my C++ program?

I am trying to do this problem:

each receive $200 per week plus 9 percent of their gross sales for that week. For example, a salesperson who grosses $5000 in sales in a week receives $200 plus 9 percent of $ 5000, or a total of $650. Write a program(using an array of counters) that determines how many of the salespeople earned salaries in each of the following ranges (assume that each salesperson's salary is truncated to an integer amount.

In my code I am calculating this with a rand() * 0.09 in order to get a random gross number to get 9 percent of for the problem. However, everytime I run it I get the same numbers. Also, what is an array counter exaclty? And am I using one correctly??

#include<iostream>
#include<array>

using namespace std;


int main() {
  
   double grossales, salary,counterA=0,counterB=0,counterC=0,counterD=0;
   double counterE=0, counterF=0, counterG=0, counterH=0, counterI=0;

   array<double, 5> money{};

   for (int i = 1; i < 5; i++) {

       grossales = rand() * 0.09;

       salary = 200 + grossales;

       money[i] = salary;
   }

   for (int j = 1; j < 5; j++)
       cout << money[j] << endl;


   for (int k = 1; k < 5; k++) {

       if (money[k] >= 200 && money[k] <= 299)
           counterA++;
       else if (money[k] >= 300 && money[k] <= 399)
           counterB++;
       else if (money[k] >= 400 && money[k] <= 499)
           counterC++;
       else if (money[k] >= 500 && money[k] <= 599)
           counterD++;
       else if (money[k] >= 600 && money[k] <= 699)
           counterE++;
       else if (money[k] >= 700 && money[k] <= 799)
           counterF++;
       else if (money[k] >= 800 && money[k] <= 899)
           counterG++;
       else if (money[k] >= 900 && money[k] <= 999)
           counterH++;
       else if (money[k] >= 1000)
           counterI++;
   }

   cout << "The results are:" << endl;

   cout << "A: " << counterA << endl;
   cout << "B: " << counterB << endl;
   cout << "C: " << counterC << endl;
   cout << "D: " << counterD << endl;
   cout << "E: " << counterE << endl;
   cout << "F: " << counterF << endl;
   cout << "G: " << counterG << endl;
   cout << "H: " << counterH << endl;
   cout << "I: " << counterI << endl;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

You have missed a line in main program to include.

int main()

{

srand(time(NULL));

//Rest of code

}

If you not include srand function than each time you will get the same number from rand() function.

Add a comment
Know the answer?
Add Answer to:
Why am I getting the same results with my rand() every time I run my C++...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This is c++ programming and here is my code. I am getting an exception thrown on...

    This is c++ programming and here is my code. I am getting an exception thrown on the destructor for permanentworker. Can you tell me why I am getting this exception? #include <iostream> #pragma warning(disable:4996) class PermanentWorker { private:    char *name;    int salary; public:    PermanentWorker(const char* nam, int money) : salary(money)    {        name = new char[strlen(nam) + 1];        strcpy(name, nam);    }    int getPay() const    {        return salary;   ...

  • Am I getting this error because i declared 'n' as an int, and then asking it...

    Am I getting this error because i declared 'n' as an int, and then asking it to make it a double? This is the coude: #include "stdafx.h" #include <iostream> #include <fstream> #include <string> #include <algorithm> #include <vector> using namespace std; void sort(double grades[], int size); char calGrade(double); int main() {    int n;    double avg, sum = 0;;    string in_file, out_file;    cout << "Please enter the name of the input file: ";    cin >> in_file;   ...

  • C++ XOR two strings. I am trying to XOR two strings 00112233445566778899AABBCCDDEEFFAABB060606060606 and 4ca00fd6dbf1fb284ca00fd6dbf1fb284ca00fd6dbf1fb28 my program...

    C++ XOR two strings. I am trying to XOR two strings 00112233445566778899AABBCCDDEEFFAABB060606060606 and 4ca00fd6dbf1fb284ca00fd6dbf1fb284ca00fd6dbf1fb28 my program is giving me: SPTWPVSPT[X   q'&t'!"u#'t~u"#rPTTTVVT as a result However, when i go to http://xor.pw/? and input the two hexadecimal strings in I get the result: 4cb12de59fa49d5fc439a56d172c15d7e61b09d0ddf7fd2e what am I doing wrong in my program below? // plaintext value here string pthex = "00112233445566778899AABBCCDDEEFFAABB"; // IV value here string sHex = "4ca00fd6dbf1fb28"; string PaddedPlainText = pthex + "060606060606"; //added the pad //cout<<PaddedPlainText<<endl; // the above...

  • I need help fixing my code: In C++ *************** 1) I want to sum the digits...

    I need help fixing my code: In C++ *************** 1) I want to sum the digits of an n*n matrix 2) find the average I have completed the rest ****Do not use C++ standard library. You must use pointers and pointer arithmetic to represent the matrix and to navigate through it. MY CODE: (I have indicated at which point I need help) #include <iostream> using namespace std; void swap(int *xp, int *yp) { int temp = *xp; *xp = *yp;...

  • Hello, I am working on a C++ pick 5 lottery game that gives you the option...

    Hello, I am working on a C++ pick 5 lottery game that gives you the option to play over and over. I have everything working right except that every time the game runs it generates the same winning numbers. I know this is an srand or rand problem, (at least I think it is), but I can't figure out what my mistake is. I've tried moving srand out of the loop, but I'm still getting the same random numbers every...

  • My if/else statement wont run the program that I am calling. The menu prints but once...

    My if/else statement wont run the program that I am calling. The menu prints but once I select a number the menu just reprints, the function called wont run. What can I do to fix this probelm? #include <iostream> #include "miltime.h" #include "time.h" #include "productionworker.h" #include "employee.h" #include "numdays.h" #include "circle.h" using namespace std; int main() { int select=1;     while (select>0) { cout << "Option 1:Circle Class\n"<< endl; cout << "Option 2:NumDay Class\n" << endl; cout <<"Option 3:Employee and Production...

  • I am trying to run this program in Visual Studio 2017. I keep getting this build...

    I am trying to run this program in Visual Studio 2017. I keep getting this build error: error MSB8036: The Windows SDK version 8.1 was not found. Install the required version of Windows SDK or change the SDK version in the project property pages or by right-clicking the solution and selecting "Retarget solution". 1>Done building project "ConsoleApplication2.vcxproj" -- FAILED. #include <iostream> #include<cstdlib> #include<fstream> #include<string> using namespace std; void showChoices() { cout << "\nMAIN MENU" << endl; cout << "1: Addition...

  • How would I be able to get a Merge Sort to run in this code? MY...

    How would I be able to get a Merge Sort to run in this code? MY CODE: #include <iostream> #include <fstream> #include <stdlib.h> #include <stdio.h> #include <time.h> using namespace std; class mergersorter { private:    float array[1000] ;    int n = 1000;    int i=0; public:    void fillArray()    {        for(i=1;i<=n;i++)        {            array[i-1]= ( rand() % ( 1000) )+1;        }    }    void arrayout ()    {   ...

  • I am trying to write this code which asks "Write a program that ask the user,...

    I am trying to write this code which asks "Write a program that ask the user, the question: What is a^b? //The program generates two signed integers and gives the user four attempts to get the correct answer //a=- , b = + //a= + , b = - " So far this what I wrote. I am not sure how to do this correctly. #include<iostream> #include<cstdlib> #include<ctime> using namespace std; int main() {    srand(time(0));    int guess,a,ans,b, k;...

  • This is an assignment for my algorithm class which I have written the code partially and...

    This is an assignment for my algorithm class which I have written the code partially and only need to complete it by adding a time function that calculates the average running time. We could use any programming language we want so I am using C++. I am including the instruction and my partial code below. Thank you! Implement linearSearch(a,key) and binarySearch( a,key)functions. Part A.In this part we will calculate theaverage-case running time of each function.1.Request the user to enter a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT