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

标签 c++ arrays

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

我的阵列是平行的。一个是 double 组(但在查看时一直输出 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。老实说,我非常困惑,任何综合资源都会很棒。

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

另一个编辑: 现在我可以获得数组的大小!伊皮!但是你找到最大的一个(通过使用尺寸)的所有建议都不适合我。

这是我当前的主要代码片段

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)。

关于c++ - 我需要在部分填充的数组中找到最大的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29958401/

相关文章:

ios - 尝试在 Swift 中访问嵌套字典数组中的键

c++ - 跨各种线程的unique_lock

c++ - 计算白色条纹

c++ - LLVM 异常;捕获处理程序未处理,未调用清理

python - 搜索具有多个值的 Numpy 数组

python - 索引错误 : shape mismatch: indexing arrays could not be broadcast together with shapes

java - 检查数组是否包含 Java 中的整数

c++ - OpenCV findContours函数问题

c++ - 将 map<key, value> 重建为 map<value, key> 的最短/最简单方法

c# - 如何反转 int 的字节顺序?