xixxaxee
xixxaxee

Reputation: 1

C++ array with random counts

I am stuck somewhere(have some problem with the random number) and I know it, but can't find where to fix...

Two void functions must be used; void randomNumbers(int numbers[][3], int rowSize), void randomCounts(int numbers[][3], int size, int counts[])

I can't put images to show how it should and does look like in .exe files as I just signed up today ...... Hope this works ;(

Expected Result:

//========================
//     7      6      5
//     2      1      1
//     6      7      2
//     9      3      3
//     8      1      1
//========================

//Ran. Number:       0    1    2    3    4    5    6    7    8    9
//Frequency(Counts): 0    4    2    2    0    1    2    2    1    1

What I DID:

//========================
//     0      0      0
//     0      0      0
//     0      0      0
//     0      0      0
//     0      0      0
// ========================

// Ran. Number:       0    1    2    3    4    5    6    7    8    9
// Frequency(Counts): 001A148D

Code:

#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;

const int COL = 3;
const int SIZE = 5;

void randomNumbers(int inumbers[][3], int rowSize) {
    int num = 0;
    for (int i = 0; i < 10; i++) {
        num = rand() % 10;
    }
}

void randomCounts(int inumbers[][3], int size, int counts[]) {
    for (int i = 0; i < 10; i++) {
        counts[i]++;
        cout << setw(5) << counts[i];
    }
}

int main(){

    int random[SIZE][COL] = {};
    srand((unsigned)time(NULL));

    cout << endl;
    cout << "==================" << endl;

    for (int i = 0; i < SIZE; i++) {
        for (int j = 0; j < COL; j++) {
            cout << setw(5) << random[i][j];
            if (j == COL - 1) {
                cout << endl;
            }
        }
    }

    cout << "==================" << endl;
    cout << endl;


    cout << "Ran. Number: " << setw(5) << "0" << setw(5) << "1" << setw(5) << "2" << setw(5) << "3" << setw(5) << "4" << setw(5) << "5" << setw(5) << "6" << setw(5) << "7" << setw(5) << "8" << setw(5) << "9" << endl;
    cout << "Frequency(Counts): " << randomCounts << endl;



    return 0;
}

Upvotes: 0

Views: 121

Answers (1)

Fantastic Mr Fox
Fantastic Mr Fox

Reputation: 33904

Ok, so why are you getting 0, 0, 0.... Because you never actually call your functions. You initialize your array:

int random[SIZE][COL] = {};

Then you print it here:

cout << setw(5) << random[i][j];

And nowhere in between do you set anything into this array. When you do start calling your functions you will find they don't work, due to copying the input and doing some undefined behaviour. When you have debugged this a bit more, ask a new question.

Upvotes: 1

Related Questions