아하
생활

생활꿀팁

도덕적인하마173
도덕적인하마173

c++ random class에 관해서 질문합니다. 급해요 ㅠㅠㅠㅠ

void Shownumbers

void Showminnumber

void showmaxnumber

void findmostfreqnumber 를 따로 함수로 만들어서 코딩을 완성해야 되는데 너무 어렵습니다 .도와주세요. ㅜㅜ 급해요 ㅠㅠ

55글자 더 채워주세요.
1개의 답변이 있어요!
  • 꾸준한하마55
    꾸준한하마55

    [소스코드]

    // Random Class

    #include <iostream>

    #include <time.h>

    using namespace std;

    class Random

    {

    private :

    // 10 random numbers from constructors

    int ranNum[10];

    public:

    // Default constructor with no arguments

    Random();

    // Constructor with arguments

    Random(int fromNum, int toNum);

    // show all numbers in ranNum[10]

    void ShowNumbers();

    // show minimum number in ranNum[10]

    void ShowMinNumber();

    // show maximum number in ranNum[10]

    void ShowMaxNumber();

    // show most frequent numbers in ranNum[10] // 가장 빈번한수

    void FindMostFreqNumber();

    };

    Random:: Random()

    {

    //임의로 10개의 정수 랜덤하게 중복 허용 ?

    srand((unsigned int)time(NULL));

    for (int i = 0; i < 10; i++) {

    this->ranNum[i] = rand() % (10 -1) + 1; // 0이 나오면 안됨.

    }

    }

    Random::Random(int fromNum, int toNum)

    {

    // 참고 : https://www.python2.net/questions-904357.htm

    // 파라미터 범위 안에서 정수 값을 랜덤하게 중복 허용

    srand((unsigned int)time(NULL));

    for (int i = 0; i < 10; i++) {

    this->ranNum[i] = rand() % (toNum - fromNum) + fromNum;

    }

    }

    void Random::ShowNumbers()

    {

    cout <<"ShowNumbers() 출력 : ";

    for (int i = 0; i < 10; i++)

    {

    if (i == 0)

    cout << this->ranNum[i];

    else

    cout <<", "<< this->ranNum[i] ;

    }

    cout << endl;

    }

    void Random::ShowMinNumber()

    {

    int min = this->ranNum[0];

    cout << "ShowMinNumber() 출력 : " ;

    for (int i = 1; i < 10; i++)

    {

    if (min > this->ranNum[i])

    min = this->ranNum[i];

    }

    cout << min << endl;

    }

    void Random::ShowMaxNumber()

    {

    int max = this->ranNum[0];

    cout << "ShowMaxNumber() 출력 : ";

    for (int i = 1; i < 10; i++)

    {

    if (max < this->ranNum[i])

    max = this->ranNum[i];

    }

    cout << max << endl;

    }

    void Random::FindMostFreqNumber()

    {

    cout << "FindMostFreqNumber() 출력 : " << endl;;

    // 랜덤 배열의 값에는 0이 없다고 가정

    int freq[10][2]{}; // 0으로 초기화

    int i,j, cnt = 0, jchk;

    //중복값 제외한 임시 2차원 배열 구성

    for (i = 0; i < 10; i++) {

    if (i == 0)

    {

    freq[cnt][0] = this->ranNum[i];

    cnt++;

    }

    else {

    jchk = 0;

    for (j = 0; j <cnt; j++)

    {

    if (this->ranNum[i] == freq[j][0])

    jchk++;

    }

    if (jchk == 0) {

    freq[cnt][0] = this->ranNum[i];

    cnt++;

    }

    }

    }

    // 중복값 누적

    for (j = 0;j < cnt; j++) {

    for (i = 0; i < 10; i++) {

    if (freq[j][0] == this->ranNum[i]) freq[j][1]++;

    }

    }

    // 최다 빈도횟수 구하기

    int max = freq[0][1];

    for (j = 0; j < cnt; j++) {

    if (max < freq[j][1])

    max = freq[j][1];

    }

    //최다 빈도횟수에 해당되는 배열값들만 출력

    for (j = 0; j < cnt; j++) {

    if (max == freq[j][1]) {

    cout << freq[j][0] << ", 횟수는=" << freq[j][1] << endl;

    }

    }

    }

    int main()

    {

    // create random object which random integers from 1 to 10

    Random a;

    a.ShowNumbers();

    a.ShowMinNumber();

    a.ShowMaxNumber();

    a.FindMostFreqNumber();

    cout << endl;

    // create random object which random integers from 40 to 45

    Random b(40, 45);

    b.ShowNumbers();

    b.ShowMinNumber();

    b.ShowMaxNumber();

    b.FindMostFreqNumber();

    system("pause");

    return 0;

    }

    [실행결과 캡쳐]