-1

I am making a program for a C++ class. I am wondering why my second output is only showing 18 outputs, when I need it to display 32. Can someone help?

I tried to include all of the code that I have, it is missing the top libraries.

#define max_value_num1 5
#define max_value_num2 6
#define max_value_num3 7
#define max_value_size1 25
#define max_value_size2 50
#define max_value_size3 250
using namespace std;

int main()
{
    int num1, num2, num3;

    // list our variables

    srand(23);
    num1 = 1 + (rand() % (25 - 1 + 1));

    //RNG with a range of 1-25

    cout << "First set of numbers: " << num1 << " values" << endl;
    int i;

    for (i = 1; i <= num1; i = i + 1)  
    // setting the conditions for the for loop
    {
        cout << setw(12) << right;


        if (i % max_value_num1 == 1 and i > 0)
        // making it display 5 per line

        {
            cout << endl;
        }

        cout << rand() << " ";
    }

    num2 = 1 + (rand() % ( max_value_size2 - 1 + 1));

    cout << endl << endl << "Second set of numbers: " << num2 << " 
    values" << endl;

    while ( i <= max_value_size2 )
    {
        i = i + 1;
        cout << setw(12) << right;
        cout << rand() << " ";

        if (i % max_value_num2 == 0 and i > 0) 
        // making it display 6 per line

        {   
            cout << endl;
        }
    }
    return 0;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
GiGi
  • 19
  • 1
  • 3
    You can use a debugger, step through the code and see why exactly it outputs only 18 outputs. BTW What terrible things happened to that picture? – Quimby Sep 22 '22 at 19:37
  • 2
    I wanted to load your code into my IDE so I could debug it. However, my IDE can't extract code from images. No code posted as text == no help. – Thomas Matthews Sep 22 '22 at 19:40
  • Not only is this an image, but it is a bad resolution illegible image. We can't tell `-` from `_` from `=` – Jeffrey Sep 22 '22 at 19:46
  • 1
    I'd guess you just forgot to reset `i` before your second loop. – ChrisMM Sep 22 '22 at 20:02
  • Side note: You are learning a seriously stale version of C++ I recommend updating your teaching materials. – user4581301 Sep 22 '22 at 20:06
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Jesper Juhl Sep 22 '22 at 20:17
  • Why *should* there be 32 outputs? Give us an argument why you believe your code should work. I might accept "obvious" as a reason if the loop in question explicitly had 32 iterations, something as direct as `for (i = 0; i < 32; ++i)`, but your logic is nowhere near that clear. – JaMiT Sep 22 '22 at 23:37

1 Answers1

0

I spotted a few problems:

  • You use srand() + rand() to generate random numbers. Every implementation and version of the implementation may have a different algorithm, so even if you use the same seed (23 in your case) everywhere, the program may produce different numbers on different computers.
  • Your second loop doesn't reset i and also does not loop up to num2.

I suggest that you use a predictable random number generator, like std::mt19937. Given the same seed, it will produce the same output on every computer.

Example:

#include <iomanip>
#include <iostream>
#include <random>

constexpr int max_value_num1 = 5;
constexpr int max_value_num2 = 6;
constexpr int max_value_size2 = 50;

int main() {
    std::mt19937 RNG(241); // a predictable RNG with seed 241 gives you 3 and 32   

    // RNG with a range of 1-25
    // std::uniform_int_distribution<int> d1(1, 25);
    int num1 = (RNG() % 25) + 1;  // d1(RNG);

    std::cout << "First set of numbers: " << num1 << " values\n";
    for (int i = 1; i <= num1; ++i) {
        std::cout << std::setw(12) << std::right
                  << RNG() << (i % max_value_num1 ? ' ' : '\n');
    }

    // std::uniform_int_distribution<int> d2(1, max_value_size2);
    int num2 = (RNG() % max_value_size2) + 1;  // d2(RNG);

    std::cout << "\n\nSecond set of numbers: " << num2 << " values\n";
    for (int i = 1; i <= num2; ++i) {
        std::cout << std::setw(12) << std::right
                  << RNG() << (i % max_value_num2 ? ' ' : '\n');
    }
}

Output:

First set of numbers: 3 values
  4086297886   2483807834    126996727 

Second set of numbers: 32 values
  4258463888      6496754   3499794904   3130452116   3211328137   3617002363
   732548676      1690476   3054654745   2741801503    701850039   1107299635
   152554302   1060507959    416824276   2907487771   1163397615   3250577734
  3044392611     39527168   2595094408   3842149802    828459248   3444516422
  1835210711   2585711173   1254196836   3291822256   4165482234   4008849376
  3511650070    977423543 

Note: I've added comments about std::uniform_int_distribution which is the kind of distribution one usually uses - but, it's not required to produce the same result on different platforms, so I use a simple modulus operation just like you did instead. That way the result will be the same everywhere you run the program.

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108