C 错误 : expected declaration or statement at end of input

标签 c compiler-errors

好吧,据我所知,这些错误似乎通常在缺少分号、花括号放错位置或类似性质的东西时出现。我肯定只是不知何故错过了它,但我找不到错误在哪里,或者是否是由于那种错误造成的。这个错误指向第 235 行,所以最后一个函数结束。

这是代码

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define MAX 100

void display_menu();
int check_option(int option);
int check_size(int size);
void initialize_2Darray(int x[][MAX], int size);
void print_2Darray(int x[][MAX], int size);
void initialize_1Darray(int y[], int size);
void print_1Darray(int y[], int size);
int search_min(int x[][MAX], int r, int c, int size);
int count_match(int x[][MAX], int y[], int size, int r);
int closest_row(int x[][MAX], int y[], int size);
void sort_1Darray(int y[], int size);
void sort_2Darray(int x[][MAX], int size);

int main()
{
        int size, r, c, option;
        int exit = 0; //exits program when = 1
        int x[MAX][MAX], y[MAX];
        srand(time(NULL));
        printf("Enter the size: ");
        scanf("%d", &size);
        while(check_size(size) == 0) {
                printf("\nInvalid input, enter the size of the array again: ");
                scanf("%d", &size);
        }
        while(exit != 6) {
            initialize_2Darray(x, size);
            initialize_1Darray(y, size);
            display_menu();
            scanf("%d", &option);
            while(check_option(option) == 0) {
                printf("Invalid option, enter again: ");
                scanf("%d", &option);
            }
            //Search Min Operation
            if(option == 1) {
                print_2Darray(x, size);
                printf("Enter the row: ");
                scanf("%d", &r);
                printf("\nEnter the col: ");
                scanf("%d", &c);
                printf("The smallest number present in row %d and col %d is %d", r, c, search_min(x, r, c, size));
            }
            //Count Matches Op.
            else if(option == 2) {
                printf("Count Matches Operation\n\n2D array\n");
                print_2Darray(x, size);
                printf("\n1D array\n");
                print_1Darray(y, size);
                printf("\nEnter the row: ");
                scanf("%d", &r);
                if(count_match(x, y, size, r) > 0)
                        printf("There are %d matches from 1D array present in 2D array", count_match(x, y, size, r));
                else
                        printf("There are no numbers from 1D array present in 2D array");
            }
            //Closest Row Op.
            else if(option == 3) {
                printf("\nClosest Row Operation\n\n2D array\n");
                print_2Darray(x, size);
                printf("\n1D array\n");
                print_1Darray(y, size);
                printf("Row closest to the 1D array is row %d", closest_row(x, y, size));
            }
            //Sort 1D Array Op.
            else if(option == 4) {
                printf("Sort 1D Array Operation\n\n1D Array before sorting:\n");
                print_1Darray(y, size);
                sort_1Darray(y, size);
                printf("\n\n1D Array after sorting:\n");
                print_1Darray(y, size);
            }
            //Sort 2D Array Op.
            else if(option == 5) {
                printf("\nSort 2D Array Option\n\n2D Array before sorting:\n");
                print_2Darray(x, size);
                sort_2Darray(x, size);
                printf("\n\n2D Array after sorting:\n");
                print_2Darray(x, size);
            }
            //Exit
            else if(option == 6)
                exit = 6;
        }
        return 0;
}

void display_menu()
{
        printf("\nArray operations, your options are:\n\n1: Search Min\n2: Count Matches\n3: Closest Row\n4: Sort 1D Array\n5: Sort 2D Array\n6: Exit\nEnter the operation you want to perform: ");
}

int check_option(int option)
{
    if(option > 0 && option < 7)
        return 1;
    else
        return 0;
}

int check_size(int size)
{
    if(size > 0 && size <= 100)
       return 1;
    else
       return 0;
}

void initialize_2Darray(int x[][MAX], int size)
{
    int i, s; //counters
    for(i=0; i < size; i++) {
        for(s=0; s < size; s++) {
            x[i][s] = rand()%10;
        }
    }
}

void print_2Darray(int x[][MAX], int size)
{
    int i, s; //counters
    for(i=0; i < size; i++) {
        printf("\n");
        for(s=0; s < size; s++) {
            printf("%d ", x[i][s]);
        }
    }
    printf("\n");
}

void initialize_1Darray(int y[], int size)
{
    int i, r;
    for(i=0; i < size; i++) {
        r = rand()%10;
        y[i] = r;
    }
}

void print_1Darray(int y[], int size)
{
    int i;
    //Prints array values until (s)ize is reached
    for(i=0; i < size; i++) {
        printf("%d ", y[i]);
    }
}

int search_min(int x[][MAX], int r, int c, int size)
{
    int i, j; //counters
    int min = 9;
    for(i=0; i < size; i++) {
        if(x[r][i] < min) {
            min = x[r][i];
        }
    }
    for(j=0; j < size; j++) {
        if(x[j][c] < min) {
            min = x[j][c];
        }
    }
    return min;
}

int count_match(int x[][MAX], int y[], int size, int r)
{
    int i, j, count;
    for(i=0; i < size; i++) {
        for(j=0; j < size; j++) {
            if(y[i] == x[r][j])
                count++;
        }
    return count;
}

int closest_row(int x[][MAX], int y[], int size)
{
    int l, i, j; //counters
    int sum = 0;
    int dif = 0;
    int row, totaldif; //best matching row & total dif
    for(l=0; l < size; l++) {
        for(i=0; i < size; i++) {
            for(j=0; j < size; j++) {
                dif = abs(y[j] - x[i][j]);
                sum += dif;
            }
            if(sum < totaldif) {
                totaldif = sum;
                row = i;
            }

            sum = 0;
        }
    }
    return row;
}

void sort_1Darray(int y[], int size)
{
    int a, b, temp;
    //Loops through the array, swapping values until in proper order of ascension
    for(a = 0; a < size; a++) {
        for(b = 0; b < size-1; b++) {
            if(y[b] > y[b+1]) {
                temp = y[b+1];
                y[b+1] = y[b];
                y[b] = temp;
            }
        }
    }
}

void sort_2Darray(int x[][MAX], int size)
{
    int a, b, c, temp;
    //Loops through the array, swapping values until in proper order of ascension
    for(c=0; c < size; c++) {
        for(a = 0; a < size; a++) {
            for(b = 0; b < size-1; b++) {
                if(x[a][b] > x[a][b+1]) {
                    temp = x[a][b+1];
                    x[a][b+1] = x[a][b];
                    x[a][b] = temp;
                }
            }
        }
    }
}

最佳答案

通过缩进 运行您的代码。它可以帮助您快速找到此类问题。

Here's the output from your code.如您所见,问题是由 count_match() 末尾缺少大括号引起的:

int
count_match(int x[][MAX], int y[], int size, int r)
{
        int             i         , j, count;
        for (i = 0; i < size; i++) {
                for (j = 0; j < size; j++) {
                        if (y[i] == x[r][j])
                                count++;
                }
                return count;
        }
 /*** CLOSING BRACE MISSING HERE ***/
        int             closest_row(int x[][MAX], int y[], int size){
                int             l         , i, j;
                              //counters

关于C 错误 : expected declaration or statement at end of input,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29519468/

相关文章:

java - 通用类型解析 - 编译器步骤

c++ - 如果函数在类范围内声明,则 constexpr 不起作用

C++ 错误 : Class has-a-relationship with Struct in Same Header File

c - c 中的二进制文件更新....fwrite 未成功写入整个结构

c - STM32F1发送UART时丢失字节

c - h264 全局 header

为 PuTTYTray 创建预制项目

c - 试图读取制表符分隔字符串的文件并将它们放入数组并输出,但我只得到一个字符?

java - "Cannot find symbol"或 "Cannot resolve symbol"错误是什么意思?

javascript - const utf8Encoder = new TextEncoder();在 Node js