繁体   English   中英

我需要在部分填充的数组中找到最大的数字

[英]I need to find the largest number in a partially filled array

所以我有一个部分填充的数组,并且我试图找到最大的数组。 我的老师给我们的所有工具只能用于完全填充的数组,所以我在这里迷路了。

我的数组是并行的。 一个是双精度数组(但是在查看时仍会输出int -我稍后再问),另一个是字符串。

我们必须以控制台形式显示信息。 这就是为什么我必须找到最大的屏幕,所以我们可以生成不会太大也不会太小的屏幕。 当然,数量最多的人将是拥有最大艺术的人,其余的将根据占其中最大百分比的人来计算。

这是我的功能:

int findLargest(double sales[])
{
    int indexOfLargest = 0;

    for (int i = 0; i < 100; i++)
    {
        if (sales[i] > sales[indexOfLargest])
        indexOfLargest = i;
    }
    return indexOfLargest;
}

这是main的一部分:

double sales[100];
string names[100];

int elementNumber = 0;

elementNumber = findLargest(sales);
cout << "largest element number is " << elementNumber << endl;

当我运行此代码对其进行测试时,即使我只有一个条目,所以它输出的最大数字数组的索引为78,因此它应该为0。老实说,这非常令人困惑,任何COMPREHENSIVE资源都很棒。

编辑:这被保存到文本文件中,必须重新使用。

另一个编辑:现在我可以得到数组的大小了! 义pe! 但是您找到所有最大建议(使用尺寸)的所有建议对我来说都行不通。

这是我当前来自main的代码片段

string fileName = "";
int size = 0;
int indexOfLargest = 0;
indexOfLargest = findLargest(sales, size, fileName);

功能如下:

int findLargest(double sales[], int &sz, string fileName) 
{
    double numbers = 0;
    string names;
    int indexOfLargest = 0;
    ifstream fin(fileName.c_str());
    if (fin)
    {
        cout << "file opened" << endl;
        while (isalnum(fin.peek()))
        {
            getline(fin, names);
            fin >> numbers;
            fin.ignore(5, '\n');
            sz++;
        }
        cout << "The size is " << sz << endl;
        //size = 4
        if (sz == 0) return -1;  // no data means no highest index
        for (int i = 0; i < sz; ++i)
        {
            if (sales[i] > sales[indexOfLargest])
                indexOfLargest = i;
        }
        cout << "Index: " << indexOfLargest;
        fin.close();
     }

    return indexOfLargest;

再次感谢您为此付出时间。 我的伴侣和我是如此困惑。 令我惊讶的是,有人在给定的两个小时内完成了这项工作。

数组具有大小,并且所有元素都存在于数组中。 除非您对数组施加自己的限制(和代码),否则没有部分填充的数组。

因此,如果只进行部分填充,则仅表示您仅使用(例如)这一百个数组元素中的前十个,只需将检查限制在该区域即可,例如:

int findLargest(double sales[], int sz) {
    if (sz == 0) return -1;  // no data means no highest index
    int indexOfLargest = 0;
    for (int i = 1; i < sz; i++)
        if (sales[i] > sales[indexOfLargest])
            indexOfLargest = i;
    return indexOfLargest;
}

当然,这意味着您需要保持大小以及错误的位置:

double sales[100];
int sz = 0;
sales[sz++] = 3.141592653589;
sales[sz++] = 2.718281828459;
int highIdx = findLargest (sales, sz);

例如,这是一个测试程序,该程序使用大型数组buff并在该数组上施加额外的信息sz

#include <stdio.h>

int main (void) {
    char buff[1000];
    int sz = 0;
    buff[sz++] = '3'; buff[sz++] = '.'; buff[sz++] = '1';
    buff[sz++] = '4'; buff[sz++] = '1'; buff[sz++] = '5';

    printf ("Buffer size is %d\n", sizeof (buff));
    printf ("Buffer used is %d\n", sz);
    printf ("Buffer is ");
    for (int i = 0; i < sz; i++)
        putchar (buff[i]);
    putchar ('\n');

    return 0;
}

输出为:

Buffer size is 1000
Buffer used is 6
Buffer is 3.1415

也许更适用的代码片段是:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int findLargest(double sales[], int sz) {
    if (sz == 0) return -1;
    int indexOfLargest = 0;
    for (int i = 1; i < sz; i++)
        if (sales[i] > sales[indexOfLargest])
            indexOfLargest = i;
    return indexOfLargest;
}

#define SZ 15

int main (void) {
    double sales[SZ];
    int sz = 0;

    srand (time (NULL));

    for (int i = 0; i < SZ; i++)
        sales[i] = 9999.99;
    for (int i = 0; i < 10; i++)
        sales[sz++] = (float)(rand() % 10000) / 100;

    printf ("Array size is: %d\n", sizeof (sales) / sizeof (*sales));
    printf ("Array used is: %d\n", sz);
    printf ("Array is:\n");
    for (int i = 0; i < SZ; i++)
        printf ("   %2d: %8.2f%s\n", i, sales[i], (i < sz) ? "" : " - unused");
    printf ("Highest value at index: %d\n", findLargest (sales, sz));

    return 0;
}

通过几次测试运行,它向您展示了正在运行的功能:

Array size is: 15
Array used is: 10
Array is:
    0:    22.66
    1:    10.66
    2:    63.28
    3:    41.05
    4:    22.50
    5:    78.05
    6:    56.96
    7:    21.48
    8:    21.69
    9:    98.77
   10:  9999.99 - unused
   11:  9999.99 - unused
   12:  9999.99 - unused
   13:  9999.99 - unused
   14:  9999.99 - unused
Highest value at index: 9

(看到索引9是最高值98.77 ),并且

Array size is: 15
Array used is: 10
Array is:
    0:    93.56
    1:    94.28
    2:    38.54
    3:    36.54
    4:    20.90
    5:    38.39
    6:    15.02
    7:     5.18
    8:    67.72
    9:    17.09
   10:  9999.99 - unused
   11:  9999.99 - unused
   12:  9999.99 - unused
   13:  9999.99 - unused
   14:  9999.99 - unused
Highest value at index: 1

(索引1为最高值94.28 )。

另一种选择是使用vector而不是array 每当需要添加元素时,都可以使用push_back方法将其附加到向量中。 然后,您可以按以下方式重写findLargest函数:

int findLargest(vector<double>& sales)
{
    int indexOfLargest = 0;

    for (int i = 0; i < sales.size(); i++)
    {
        if (sales[i] > sales[indexOfLargest])
        indexOfLargest = i;
    }

    return indexOfLargest;
}

编辑:据我了解,您面临的主要困难是您不知道数组的大小,因此最终分配了额外的空间。 如上所述,使用向量将帮助您克服此问题。 当您将元素放入其中时,它将动态调整大小。 而且您不必担心跟踪数组的实际大小。

您应该能够通过以下方式传递数组的大小:

int GetLargestElementByIndex(double sales[], size_t size)
{
    int index = 0;
    for (int i = 0; i < size; ++i)
    {
        if (sales[i] > sales[index])
            index = i;
    }
    return index;
}

可以这样称呼:

double sales[8] = { 5.5, 2.2, 1.8, 6.6, 9.3, 14.0, 3.5, 9.2 };
int largestIndex = GetLargestElementByIndex(sales, 8);

但是,您也可以使用模板来做到这一点:

// Because we are using a template, this can be used with any array that has operator > declared for it, rather than restricting its use for an int or double
// Type = the type of the object (e.g. int, double, std::string). This is implicitly deduced from the array passed in
// size = this is the size of the array implicitly determined by the array passed in
template <typename Type, size_t size>
int GetLargestElementByIndex(const Type (&arrayOfType)[size]) // pass a const reference as we do not need to modify this object
{
    int index = 0;
    for (int i = 0; i < size; ++i)
    {
        if (arrayOfType[i] > arrayOfType[index])
            index = i;
    }
    return index;
}

像上面这样调用,但是不需要传递大小,并且它可以用于更多类型而不只是双精度类型:

double sales[] = { 2.2, 3.3, 1.0, -1.5, 5.9, 6 };
int largestIndex = GetLargestElementByIndex(sales);
int wholeSales[] = { 19, 33, 1, -4, -100, 60 };
largestIndex = GetLargestElementByIndex(wholeSales);

最简单的方法是在输入最大值时跟踪最大值:

double sales[100];
string names[100];
int num_entries = 0;
size_t max_sales_index = 0;
while (num_entries < sizeof(sales) / sizeof(sales[0]) &&
       std::cin >> sales[num_entries] &&
       getline(std::cin, names[num_entries]))
{
    if (sales[num_entries] > sales[max_sales_index])
        max_sales_index = num_entries;
    ++num_entries;
}
cout << "largest element number is " << max_sales_index << endl;

此代码跟踪num_entries中输入的元素数。 使用该数字,您可以调用“查找最大”函数,但是为什么要麻烦呢?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM