【问题标题】:Why for loop is not taking element into an array?为什么 for 循环不将元素放入数组中?
【发布时间】:2021-06-10 10:38:09
【问题描述】:

我正在尝试制作一个程序,该程序接受一系列数字并打印最大的数字。逻辑很简单——将序列作为一个数组,使用插入排序的第一步来检查最大的数字。当我运行程序时,它会一直运行到“逐个输入数字”。我输入一个数字并按回车键,一秒钟内没有任何反应,程序自行终止。我在第一个 for 循环中遗漏了什么?

#include <stdio.h>
void largest_number(int ar[], int size);
int main()
{
    int ar[100], index, size;
    printf("Enter total numbers = ");
    scanf("%d", &size);
    printf("Enter the numbers one by one\n");
    for (index = 0; index < size; index++)
        scanf("%d", ar[index]);
    largest_number(ar, size);
}

void largest_number(int ar[], int ar_size)
{
    int index, location = 0;
    for (index = 1; index < ar_size; index++)
        if (ar[index] > ar[location])
            location = index + 1;
    printf("The largest number is %d", ar[location]);
}

【问题讨论】:

  • scanf("%d", ar[index]);,需要传递ar[index]的地址
  • 欢迎来到 SO。您的编译器应该警告scanf 中的类型不匹配。如果它没有警告您,您需要调高诊断级别。对于 GCC,您可以通过选项 -Wall -Wextra 执行此操作
  • 顺便说一句,为什么location = index + 1;+ 1 是干什么用的?
  • 我刚刚了解了 scanf 部分。我正在使用vscode,它没有给出任何错误。顺便谢谢!以及如何在 vscode 中调高诊断级别?
  • 我更改了location = index +1,这是错误的。不需要增量

标签: c loops


【解决方案1】:

此响应更适合https://codereview.stackexchange.com/,但示例代码中有一些问题出现的频率足够高,应该予以解决。示例代码中的主要错误是对scanf 的错误调用,但错误不仅仅是没有传递地址。您必须始终检查 scanf 返回的值。您在互联网上找到的使用 scanf 的 98% 的代码都无法做到这一点。不要假设你的程序会以交互方式运行;包括提示和旨在为交互式用户提供信息的东西将使程序在非交互式环境中无法使用。不能以非交互方式使用的程序永远无法扩展。 (当然,对于像这样的玩具程序,您永远不会期望它可以扩展,但尽早养成习惯是一个好习惯。)另外,动态增长数组并不难,而不是使用固定大小的数组,所以没有必要要求输入包含预期数量的元素:

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

struct int_array;
int largest_number(const int *ar, size_t size);
void push_int(struct int_array *a, int v);

struct int_array {
        int *ar;
        size_t cap;  /* Capacity of the buffer */
        size_t len;  /* Number of elements in the buffer */
};
int
main(void)
{
        struct int_array a = {0};
        int x;

        if( isatty(STDIN_FILENO) ){
                printf("Enter the numbers one by one\n");
                fflush(stdout); /* Probably not necessary */
        }
        while( scanf("%d", &x) == 1 ){ /* Always check return value */
                push_int(&a, x);
        }
        if( a.ar ){
                printf("The largest number: %d\n", largest_number(a.ar, a.len));
        }
}

void
push_int(struct int_array *a, int v)
{
        if( a->len >= a->cap ){
                a->ar = realloc(a->ar, sizeof *a->ar * (a->cap += 128));
                if( a->ar == NULL ){
                        perror("realloc");
                        exit(EXIT_FAILURE);
                }
        }
        a->ar[a->len++] = v;
}

int
largest_number(const int *ar, size_t ar_size)
{
        int max = *ar;
        for( const int *end = ar + ar_size; ar < end; ar += 1 ){
                if( *ar > max ){
                        max = *ar;
                }
        }
        return max;
}

如果用户以交互方式输入数据,他们可以简单地通过关闭输入流来终止输入(例如,通过键入 ^d(在 unix 上,我相信在 windows 上是 ^z)或输入一些不是整数(eq,字符串“quit”)。

【讨论】:

    【解决方案2】:

    答案很简单:您应该使用地址运算符 (&amp;) 来扫描数组的 元素。你写的是ar[index]而不是&amp;ar[index]

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-28
      • 2012-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-08
      相关资源
      最近更新 更多