C ++使用存储在动态数组中的文本文件中的数据查找模式

问题描述 投票:0回答:2

我在完成从整数列表中找到模式的功能时遇到麻烦。我应该使用指针作为数组,并使用指针符号“ *(arr + 1)”代替例如arr [1]。我的讲师提供了伪代码来解释编写函数的过程,但是我在理解某些部分时遇到了麻烦。 (很抱歉,如果这篇文章的格式不正确,这是我的第一篇文章)

完整的程序还完成了其他一些事情,但是我已经成功地完成了这些功能,没有问题

伪代码如下:

int* calculateMode(int* movies, int size, int& numModes)

按升序排列电影

创建称为整数大小模式的整数的动态数组

将电影中的第一个int分配给当前

将计数器设置为1

将numModes设置为1

将modePos设置为0

将maxCount设置为1

将模式中的第一个元素设置为当前

对于i = 1等于尺寸

如果*(电影+ i)==当前//与当前相同(我们看过的上一个)

增量计数器

如果计数器> maxCount // //这意味着我们有一个新的modePos

清除模式//需要清除数组中的所有旧模式,请考虑std :: fill_n(modes,size,-1)

将modePos设置为0 //从模式数组的开头开始

将模式中的第一个元素设置为当前// //将我们的新模式分配给数组的开头

将maxCounter设置为计数器

将numModes设置为1

否则,如果counter == maxCount //获得另一种模式

increment modePos //获取下一个模式的位置

将电流分配给下一个模式在模式中定位

increment numModes

其他//我们有新的潮流

将电影值分配给当前等到1

如果计数器== maxCount //如果所有值都被表示一次,那么它们就是所有模式

increment modePos

将电流分配给模式和modePos

increment numModes

返回模式

最终输出应该是(以及所有其他功能):

[看电影的学生总数为28所有学生看电影的平均数量是27.46所有学生看电影的中位数是28.00所有学生观看的电影的模式编号是47、16按任意键继续

文本文件是(其中第一个数字“ 28”是文件中存在的数字量:]

28

14

16

19

6

9

47

43

28

35

16

30

6

47

12

14

47

41

16

44

22

20

31

45

34

31

44

4

8

这是我的尝试

int* calculateMode(int* movies, int size, int& numModes)
{

    sort(movies, movies + size);    //sorted movies array

    int* modes = new int[size]; //dynamic array of modes

    int current = *(movies + 0);    //the current mode (start on first number in array

    int counter = 1;    //how many of the same number there are

    numModes = 1;   //number of modes, the minimum always being 1

    int modePos = 0;    //position of the mode, 0 being the first number in array

    int maxCount = 1;   

    *(modes + 0) = current;

    for (int i = 1; i < size; i++)
    {
        if (*(movies + i) == current)   //same as current
        {
            counter++;
            if (counter > maxCount) //new modePos
            {
                std::fill_n(modes, size, -1);   //clear modes
                modePos = 0;    //start back at beginning of the array for modes
                *(modes + 0) = current;
                maxCount = counter;
                numModes = 1;
            }
            else if (counter == maxCount) //got another mode
            {
                modePos++;  //get position for the next mode
                current = modePos;
                numModes++;
            }
        }
        else   //if we have new current
        {
            current = *(movies + i);
            counter = 1;
            if (counter == maxCount)    //if all values are represented once they are all modes
            {
                modePos++;
                current = modePos;
                numModes++;
            }
        }
    }   //end of for loop

    return modes;
}
c++ pointers dynamic-arrays mode
2个回答
0
投票

这是答案,我设法使它起作用

int* calculateMode(int* movies, int size, int& numModes)
{
    sort(movies, movies + size);    //sorted movies array
    int* modes = new int[size]; //dynamic array of modes
    int current = *(movies + 0);    //the current mode (start on first number in array
    int counter = 1;    //how many of the same number there are
    numModes = 1;   //number of modes, the minimum always being 1
    int modePos = 0;    //position of the mode, 0 being the first number in array
    int maxCount = 1;



*(modes + 0) = current;

for (int i = 1; i < size; i++)
{
    if (*(movies + i) == current)   //same as current
    {
        counter++;
        if (counter > maxCount) //new modePos
        {
            std::fill_n(modes, size, -1);   //clear modes
            modePos = 0;    //start back at beginning of the array for modes
            *(modes + 0) = current;
            maxCount = counter;
            numModes = 1;
        }
        else if (counter == maxCount) //got another mode
        {
            modePos++;  //get position for the next mode
            *(modes + modePos) = current; //here is where i made the change
            numModes++;
        }
    }
    else   //if we have new current
    {
        current = *(movies + i);
        counter = 1;
        if (counter == maxCount)    //if all values are represented once they are all modes
        {
            modePos++;
            *(modes + modePos) = current;    //here is where i made the change
            numModes++;
        }
    }
}   //end of for loop

return modes;
}

0
投票

您可以索引一个指针数组,因此无需通过递增第一个内存地址来对其进行迭代。如果您仍然希望这样做,则OP2适合您。

int calculateMode(int* movies, int size) {
    std::sort(movies, movies + size);
    /* OPT 1 */
    int number = movies[0], mode = movies[0], counter = 1, numModes = 1;
    for (int i = 1; i < size; i++) {
        if (movies[i] == number) {
            counter++;
        } else {
            if (counter > numModes)
            {
                numModes = counter;
                mode = number;
            }
            counter = 1;
            number = movies[i];
        }
    }
    /* OPT 2 */
    number = *movies, mode = *movies, counter = 1, numModes = 1;
    for (int i = 1; i < size; i++) {
        if (*(movies + i) == number) {
            counter++;
        }
        else {
            if (counter > numModes)
            {
                numModes = counter;
                mode = number;
            }
            counter = 1;
            number = *(movies + i);
        }
    }
    return mode;
}
© www.soinside.com 2019 - 2024. All rights reserved.