0

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;
}
3
  • Welcome to Stack Overflow. What happens when you try to generate and print one random number? (And please show us the real code.) Commented Jun 10, 2020 at 11:16
  • Could you describe what you're trying to do? Commented Jun 10, 2020 at 11:21
  • @Beta I couldn't attach how it should appear in .exe as I am not allowed to attach image files. I edited the code just now. Commented Jun 10, 2020 at 11:22

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I'm sorry if this sounds a bit awkward but I posted this as I didn't know how to call the func properly in main().... I thought of writing; for (int i ~~~) {for (int j ~~~) { random [i] [j] = randomNumbers(random, 5);}} But it wouldn't work as I tried to put void into int
You should add this detail to the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.