在C中的数组中存储整数的调用函数

时间:2019-05-28 03:31:13

标签: c arrays function sorting scanf

我有一个家庭作业需要帮助。

我编写了一个函数,该函数应该使用scanf接收用户输入并将其存储到数组中。它还将计算输入整数的次数(0除外,终止程序) 我特别遇到在main()中调用此函数的问题 有关如何修复它的任何建议?

我尝试在底部使用函数定义,并在函数内使用其他变量:getVals

#include <stdio.h>

#define ARRAYSIZE 20 //defines the size of the array to 20

int nVals = 0;
int getVals(int myVals[], int maxVals);
int input[ARRAYSIZE+1] = {'0'};

int getVals(int myVals[], int maxVals)
{
  for (maxVals = 0; maxVals < ARRAYSIZE; maxVals++) { //When the row is    less than ten, it will run the loop and increment the row
    printf("Please Enter up to 20 integers "); //which will allow the name to be stored in the next row
    scanf("%d", &myVals[maxVals]);

    if ((myVals[maxVals] <= 0) || (maxVals == ARRAYSIZE -1))
       return nVals;
    if (myVals[maxVals] > 0)
       nVals++;

  }
}

int main() {
  printf("This program will receive up to 20 inputs from you, and sort them from least to greatest\n\n");
  printf("Enter 0 or a negative number to exit the program.\n");

  getVals(int ARRAYSIZE, int input[ARRAYSIZE]);

  printf("You have entered %d numbers\n", nVals);
  printf("%d", input[]);
  printf("\n");
  printf("Now sorting....\n");

  return 0;
}

3 个答案:

答案 0 :(得分:1)

您应该找到一个教程。它们充满了代码示例,您可以控制不确定时在其中完成简单事情的方式。

首先是错误。

函数 declaration 仅声明该函数及其参数。您正确使用它。只要所有程序都一致,就可以在同一程序中有多个声明用于同一函数。

函数定义包含该函数的代码。一个程序应该每个功能仅包含一个定义。顺便说一句,定义也是声明

使用函数的地方是函数调用。它不再是声明,并且该函数的声明在调用之前必须可见。您必须传递与声明一致的现有变量。

您应该在这里:

int input[ARRAYSIZE];          // declare an int array
getVals(input, ARRAYSIZE);     // call the function

此行printf("%d", input[]);也是错误的。数组不是C语言的一等公民,并且一次只能打印(或读取)一个元素。至少您应该编写printf("%d", input);(作为调用而不是声明,因此它需要实际参数,而不是形式参数)。但是该数组将衰减为指向其第一个元素的指针(一直到那里为止),该指针将被转换为一个int值(该数组的第一个元素的地址),然后您将打印该值。不是您想要的:-(

但是还有其他可能的改进。

nVal是一个全局值。除非您有很强的设计理由,否则应避免使用全局变量。最佳做法建议将参数传递给函数。因此,您应该删除全局int nVals = 0,将getVals更改为:

int getVals(int myVals[], int maxVals)
{
    int nVals;
    for (nVals = 0; nVals < maxVals; nVals++) { //When the row is    less than ten, it will run the loop and increment the row
        printf("Please Enter up to 20 integers "); //which will allow the name   to be stored in the next row
        if (1 != scanf("%d", &myVals[nVals])) {    // ALWAYS test scanf return value
            printf("Incorrect input");
            break;
        }
        if (myVals[nVals] <= 0) {
            break;
        }
    }
    return nVals;
}

并从main调用为:

int nVals = getVals(input, ARRAYSIZE);

答案 1 :(得分:0)

main()中的问题

正如Rishi在其answer中所指出的那样,main()中的这一行是错误的一行:

getVals(int ARRAYSIZE, int input[ARRAYSIZE]);

这应该防止您的代码编译,因为在预处理程序完成之后,该行显示为:

getVals(int 20, int input[20]);

那根本不是有效的C,第一个20是毫无意义的语法错误。

假设您拥有或多或少可以编译的东西,例如以下其中之一:

getVals(int SIZE, int input[ARRAYSIZE]);
getVals(int SIZE, int input[SIZE]);

然后您将获得一个没有返回类型的函数声明。在C90规则下,该值不能出现在函数中间。在C99或更高版本的规则下,它可以显示在它的位置,但是它应该具有返回类型–

int getVals(int SIZE, int input[ARRAYSIZE]);
int getVals(int SIZE, int input[SIZE]);

尽管ARRAYSIZE无关紧要,但第一个声明在C90和C99中均有效。第二个声明仅在C99或更高版本中有效,因为它使用VLA(可变长度数组)。这些在C90中不存在。

但是,因为这是一个声明而不是函数调用,所以从不调用实际的getVals()函数-这是您所报告的问题。

您实际上要:

int num = getVals(ARRAYSIZE, input);

除了:

您已使用以下方法定义了getVals()

int getVals(int myVals[], int maxVals)

所以您真的需要打电话:

getVals(input, ARRAYSIZE);

getVals()中的问题

此功能也很混乱。您可能应该使用类似这样的东西:

int getVals(int myVals[], int maxVals)
{
    int i;
    for (i = 0; i < maxVals; i++)
    {
        printf("Please Enter up to 20 integers ");
        if (scanf("%d", &myVals[i]) != 1)
            return i;
        if (myVals[i] <= 0)
            return i;
        nVals++;
    }
    return i;
}

我将nVals保留为全局变量,但这确实不是必需的。您应尽可能避免使用全局变量(在任何函数外部定义的变量)。

答案 2 :(得分:-1)

错误:此行在主函数中

getVals(int ARRAYSIZE, int input[ARRAYSIZE]);

正确的方法:

getVals(input, input[ARRAYSIZE]);

首先:调用函数时,请勿在属性中使用数据类型。

第二个:您的数组名称是“ input”,因为要传递数组,因此您应该使用输入的数组名称,而不是ARRAYSIZE ...