繁体   English   中英

EASY C Max Array Element程序->无法找到/理解错误

[英]EASY C Max Array Element program -> CANNOT find/understand the error

我编写了这个程序,该程序可以在整数数组中找到最大元素的索引。 出于某种原因,尝试编译时出现以下错误。 关于我的代码有什么问题的任何想法? 我似乎找不到任何东西。

part1.c:9: error: expected ‘;’, ‘,’ or ‘)’ before numeric constant

part1.c: In function ‘main’:

part1.c:13: warning: implicit declaration of function ‘largest’

part1.c: At top level:

part1.c:20: error: expected ‘;’, ‘,’ or ‘)’ before numeric constant
make: *** [part1] Error 1

我的代码:

// Program that finds the largest element in an array of integers 

#include <stdio.h>

// Main body
// Create and initialise a one-dimensional array of integers

#define ARRAY_SIZE 10
int largest(int array, int ARRAY_SIZE);
int main(int argc, char** argv)
{
  int array [ARRAY_SIZE] = { 5, 1, 2, 8, 12, 9, 0, 4, 52, 91 };
  int maxIndex = largest(array, ARRAY_SIZE);
  printf("%d", maxIndex);
}

// largest - function
// takes (array,length) -> returns the index of the largest element in the array

int largest(int array, int ARRAY_SIZE)
{
   int maxIndex;

   for(int index = 0; index < 10; index++)
   {
     if array[i] > array[i+1]
     i = maxIndex;  
   }

   return maxIndex; 

}

问题是您的代码没有任何功能。 我认为您只需要一个main

int main(int argc, char** argv)
{
    int array [ARRAY_SIZE] = { 5, 1, 2, 8, 12, 9, 0, 4, 52, 91 };
    int maxIndex;
    maxIndex = largest(array, ARRAY_SIZE);
    printf("%d", maxIndex);
}

我还添加了一个声明maxIndex里面main功能,因为在变量largest不会是从可见光main

发表评论后:

现在的问题是预处理器将ARRAY_SIZE更改为10 largest的声明更改为:

int largest(int array, int asize);

还要在下面的实际功能中进行更改。

C(尤其是C99)需要调用函数之前了解它们。 有两种方法可以做到这一点:

  1. 将您的整个largest功能放在main功能之前。

     int largest(int *array, int size) { // ... } int main(int argc, char *argv[]) { // ... } 
  2. 将函数原型放在main函数之前。 原型实质上是该函数的返回类型,名称和参数列表的副本,例如:

     int largest(int *array, int size); int main(int argc, char *argv[]) { // ... } int largest(int *array, int size) { // ... } 

    使用函数原型,您可以省略参数的名称,在该阶段仅类型信息很重要,因此可以将函数原型缩短为int largest(int *, int);

请注意,我更改了有关您largest功能的两件事。 现在,第一个参数的类型是指针类型。 当将数组作为函数的参数传递时,这是必要的,实际上,您是在为函数提供数组第一个元素的地址。 我还将第二个参数重命名为size ,因为ARRAY_SIZE是一个扩展为10的宏,因此编译器看到的是:

int largest(int array, int 10)

那只是无效的C。

largest(array, ARRAY_SIZE);
printf("%d", maxIndex);

这可能是个问题。

int maxIndex = largest(array, ARRAY_SIZE);
printf("%d", maxIndex);

??

SO欢迎您,迈克!

#define ARRAY_SIZE 10

int array [ARRAY_SIZE] = { 5, 1, 2, 8, 12, 9, 0, 4, 52, 91 };
largest(array, ARRAY_SIZE);
printf("%d", maxIndex);

// largest - function

这些操作,即largest()调用和printf()调用-它们无法在此处发生。 它们不是合法的语法。 必须在现有函数的声明内调用函数。

考虑将它们移到应调用它们的函数范围内-如果在启动时,可以在main()提早将它们放置,或者如果这是一个库,则将其放在库的入口点。

main之前声明largest 从其参数中删除ARRAY_SIZE 您将ARRAY_SIZE定义为常量,因此不能在函数声明中将其用作参数。
并且由于要向其传递数组名称,并且它将衰减为指向其第一个元素的指针 ,因此应将其声明为

int largest(int *array, int n);

在您的程序主体之前,需要一个main功能。

int main(int argc, char** argv)
{
     int array [ARRAY_SIZE] = { 5, 1, 2, 8, 12, 9, 0, 4, 52, 91 };
     int maxIndex;
     maxIndex = largest(array, ARRAY_SIZE);
     printf("%d", maxIndex);
}  

另外,您还必须在largest函数中声明iindex
经过一些修复的代码:

 // Program that finds the largest element in an array of integers

#include <stdio.h>
#define ARRAY_SIZE 10
// Main body
// Create and initialise a one-dimensional array of integers



int largest(int* array, int n);
int main()
{
     int array [ARRAY_SIZE] = { 5, 1, 2, 8, 12, 9, 0, 4, 52, 91 };
     int maxIndex ;
     maxIndex = largest(array, ARRAY_SIZE);
     printf("%d", maxIndex);
}
// largest - function
// takes (array,length) -> returns the index of the largest element in the array

int largest(int* array, int n)
{
    int maxIndex = 0;
    int max = array[0];

    for(int index = 1; index < n; index++)
    {
        if (max < array[index])
        {
            maxIndex = index;
            max = array[index];
        }

    }

    return maxIndex;
}

暂无
暂无

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

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