It's a C++ question!

Asked 1 years ago, Updated 1 years ago, 116 views

I'm a C++ beginner student.

I wrote a function that generates random numbers from 0 to 9, creating a two-dimensional array of four rows and three rows, and I want to express the frequency of the numbers in this array as a one-dimensional array.

Two zeros appear in a two-dimensional array, like increasing two zeros in a one-dimensional array!

#include <iostream>
#include <iomanip>   // setw()
#include <fstream>   
#include <ctime>     // rand(), time()

using namespace std;

// Symbolic constants, global
const int ROW = 4;
const int COL = 3;

// *** Circular of function ***
void printArray (in num[][COL], in rowSize); // 2D array output
void randomNumbers (intinumbers[][3], rowSize); // initialize to random number
void randomCounts (intinumbers[][3], int size, intcounts[]); // frequency calculation
void printArray1 (int x[], int from, int to); // 1D array output


int main()   //main
{
    int rn[ROW][COL];
    int counts[10] = {0, };

    randomNumbers(rn, ROW);
    printArray(rn, ROW);
    randomCounts(rn, ROW, counts);

    cout <<endl<< "frequency arrangement" <<endl;
    printArray1(counts, 0, 9);

  }   }   // main


// ******************* Function *********************

// 2D array output
void printArray(int num[][COL], int rowSize)
{
    cout << "==================" << endl;
    for (int row = 0; row < rowSize; row++)
    {
        for (int col = 0; col < COL; col++)
            cout << setw(5) << num[row][col];
        cout << endl;
    }
    cout << "==================" << endl;

}

void randomNumbers(int inumbers[][COL], int rowSize)
{
    srand((unsigned int)time(NULL));

    for (int i = 0; i < ROW; i++)
    {
        for (int j = 0; j < COL; j++)
        {
            inumbers[i][j] = rand() % 10;

        }
    }

}

void randomCounts(int inumbers[][COL], int size, int counts[])
{

    for (int i = 0; i < ROW; i++)
    {
        for (int j = 0; j < COL; j++)
        {
            for (int s = 0; s < 12; s++)
            {
                if (counts[s] == inumbers[i][j])
                    counts[s]++;
            }


        }
    }


}




void printArray1(int x[], int from, int to)
{
    // Index Output
    for (int i = from; i <= to; i++)
        cout << setw(5) << i;
    cout << endl;
    cout << "  ";
    // Separator output
    for (int i = from; i <= to; i++)
        cout << "----+";
    cout << endl;
    // Array Output
    for (int i = from; i <= to; i++)
        cout << setw(5) << x[i];
    cout << endl << endl;
}

The randomCounts function that calculates the frequency here is a problem. I'd really appreciate your help!

c++ 2d-array

2022-09-20 10:25

1 Answers

void randomCounts(int inumbers[][COL], int size, int counts[])
{

    for (int i = 0; i < ROW; i++)
    {
        for (int j = 0; j < COL; j++)
        {
            counts[inumbers[i][j]]++;
        }
    }
}


2022-09-20 10:25

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.