vendredi 17 février 2023

DEVC++ is showing a duplicate code in the output window, while other C++ give me the right ouput. What am I doing wrong? (Random Number Generator)

As default, I use DevC++ to write programs for my computer science class. I am really new to all of this. For this program, we are told to use a random number generator to display values using loops. We used for, while, and do..while loops. I was able to do the for loop (Set1) and while loop (Set2) with no struggle. Once I got to do.. while loop (Set3), and finished it, the output window shows a repeat of Set 2, which is the while loop. I ran it through onlinegdb and it gave me the right output. Is it something with DevC++ or is there something wrong in my code that I haven't been able to see?

The coding for my program:

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>

using namespace std;

//declare constant doubles for the program

#define NUM_ONE 30
#define MAX_NUM_TWO 75
#define MAX_NUM_THREE 155
#define MIN_RAND_DOUBLE_VAL 0.0
#define MAX_RAND_DOUBLE_VAL 100.0
#define VAL_DISPLAY 7

int main()
{
    //input the seed value of 12 into the random number generator

    srand(12);

    //declare integer values

    int random_num;
    int cnt;
    int per_line;

    per_line = 0;

    //start the random number generator for the first set

    cout << "Set 1 -- " << NUM_ONE << " values" << endl;

    for (cnt = 1; cnt <= NUM_ONE; cnt++) {
        random_num = rand();

        cout << setw(13) << random_num;
        per_line++;

        if (per_line % VAL_DISPLAY == 0) {
            cout << endl;
        }
    }

    cout << endl
         << endl;

    //form the formula to start the random number generator for the second set (1-75)

    int random_num2;

    random_num2 = rand() % MAX_NUM_TWO + 1;

    //start the random number generator for the second set

    cout << "Set 2 -- " << random_num2 << " values" << endl;

    int cnt2;
    int per_line2;
    cnt2 = 1;
    per_line2 = 0;

    while (cnt2 <= random_num2) {
        random_num = rand();
        cnt2++;

        cout << setw(13) << random_num;
        per_line2++;

        if (per_line2 % VAL_DISPLAY == 0) {
            cout << endl;
        }
    }

    cout << endl
         << endl;

    //start the random number generator for the third set

    int random_num3 = rand() % MAX_NUM_THREE + 1;

    cout << "Set 3 -- " << random_num3 << " values" << endl;

    double double_random_num;
    int cnt3;
    int per_line3;
    cnt3 = 1;
    per_line3 = 0;

    do {
        double_random_num = MIN_RAND_DOUBLE_VAL + (rand() / (RAND_MAX / (MAX_RAND_DOUBLE_VAL MIN_RAND_DOUBLE_VAL)));

        cout << fixed << setprecision(5) << setw(13) << double_random_num;
        cnt3++;
        per_line3++;

        if (per_line3 % VAL_DISPLAY == 0) {
            cout << endl;
        }
    }

    while (cnt3 <= random_num3);

    cout << endl;

    return 0;
}

On DevC++ the output is supposed to look like this:

Set 1 -- 30 values
 77     5628     6232   29052   1558   26150   12947
 29926  11981   22371   4078   28629   4665     2229
 24699  27370    3081   18012   24965   2064    8285
 21054  5225    11777   29853   2956   22439   3341
 31337  14755
Set 2 -- 65 values
 24855   4173   32304    292   5344   15512   12952
 1868   10888   19581   13463  32652   3409   28353
 26151  14598   12455   26295  25763   26040   8285
 27502  15148   4945   26170   1833    5196    9794
 26804  2831    11993   2839   9979   27428    6684
 4616   30265   5752   32051   10443   9240    8095
 28084  26285   8838   18784   6547    7905    8373
 19377  18502   27928  13669   25828   30502  28754
 32357   2843   5401   10227   22871  20993    8558
 10009 6581
Set 3 -- 87 values
 39.08811   14.20026   75.05417   65.71551   28.70876   20.87466   92.68166
 7.11081    0.00916    85.52507   67.95251   58.98312   55.28123   55.23850
 47.74316   66.31062   52.77261   25.62334   84.13038   6.10981    11.68859
 38.34346   4.33363    90.12421   66.59139   30.37812   25.38835   33.32011
 24.34767   75.70421   91.63793   53.89264   26.74642   9.94293    63.23130
 20.26124   71.88940   78.69503   33.71685   10.81576   97.50053    0.06714
 4.85549    1.96539    79.29014   82.14972   96.79250   50.13276   47.45933
 85.93097   21.68950   83.30638   74.52010   74.41633   98.99594   67.82434
 37.49199   38.45637   40.39125   65.93829   7.67846    39.96399   82.64412
 49.83978   71.09287   63.16111   96.37745   87.76513   32.64565   14.43525
 48.99747   67.77551   7.29698    61.47343   49.82147   74.88327   51.20396
 52.24464   55.53758   87.09067   36.05152    4.54726   64.19263   6.03656

But it is giving me:

Set 1 -- 30 values
           77         5628         6232        29052         1558        26150        12947
        29926        11981        22371         4078        28629         4665         2229
        24699        27370         3081        18012        24965         2064        26890
        21054         5225        11777        29853         2956        22439         3341
        31337        14755

Set 2 -- 65 values
        24855         4173        32304          292         5344        15512        12952
         1868        10888        19581        13463        32652         3409        28353
        26151        14598        12455        26295        25763        26040         8285
        27502        15148         4945        26170         1833         5196         9794
        26804         2831        11993         2839         9979        27428         6684
         4616        30265         5752        32051        10443         9240         8095                             Set 2 -- 65 values
        24855         4173        32304          292         5344        15512        12952
         1868        10888        19581        13463        32652         3409        28353
        26151        14598        12455        26295        25763        26040         8285
        27502        15148         4945        26170         1833         5196         9794
        26804         2831        11993         2839         9979        27428         6684
         4616        30265         5752        32051        10443         9240         8095
        28084        26285         8838        18784         6547         7905         8373
        19377        18502        27928        13669        25828        30502        28754
        32357         2843         5401        10227        22871        20993         8558
        10009         6581

Set 3 -- 87 values
     39.08811     14.20026     75.05417     65.71551     28.70876     20.87466     92.68166
      7.11081      0.00916     85.52507     67.95251     58.98312     55.28123     55.23850
     47.74316     66.31062     52.77261     25.62334     84.13038      6.10981     11.68859
     38.34346      4.33363     90.12421     66.59139     30.37812     25.38835     33.32011
     24.34767     75.70421     91.63793     53.89264     26.74642      9.94293     63.23130
     20.26124     71.88940     78.69503     33.71685     10.81576     97.50053      0.06714
      4.85549      1.96539     79.29014     82.14972     96.79250     50.13276     47.45933
     85.93097     21.68950     83.30638     74.52010     74.41633     98.99594     67.82434
     37.49199     38.45637     40.39125     65.93829      7.67846     39.96399     82.64412
     49.83978     71.09287     63.16111     96.37745     87.76513     32.64565     14.43525



Aucun commentaire:

Enregistrer un commentaire