lundi 24 avril 2023

positive random number matrix gives only 0 and negative in c++

I need to make a 100x100 matrix with only odd integer random numbers, but it only gives me zeros and negative numbers. Something like this:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 61 0 0 0 0 0 0 0 0 0 0 0 0 0 0 605810241 32763 0 0 0 0 0 0 0 0 0 0 0 0 -1109404240 -1109404223 46 -1109404000 46 -1109404196 46 -1109404200 46 0 0 0 0 0 1204 0 0 141694856 367 32 3

#include <iostream>
#include <stdlib.h>
#include <time.h>

using namespace std;

int main()
{

    int matrix[100][100];

    srand(time(NULL));

    for (int i = 0; i < 100;)
    {
        for (int j = 0; j < 100;)
        {
            int random = 1 + rand() % 100;
            if (random % 2 != 0 && random > 0)
            {
                matrix[i][j] = random;
                i++;
                j++;
            }
        }
    }

    for (int i = 0; i < 100; i++)
    {
        for (int j = 0; j < 100; j++)
        {
            cout << matrix[i][j]
                 << " ";
        }
        cout << "\n";
    }

    return 0;



Aucun commentaire:

Enregistrer un commentaire