C错误:期望在输入结束时声明或声明。

时间:2023-02-05 22:43:32

Alright, so from what I've seen, it seems these errors usually show up whenever there's a semicolon missing, or a misplaced curly-brace or something of that nature. I'm surely just missing it somehow, but I'm not able to find where the error is, or if it is due to an error of that sort. This error in pointing toward line 235, so the end of the last function.

好的,从我所看到的,似乎这些错误通常出现在有分号缺失的时候,或者是一个错位的曲柄或者类似的东西。我肯定是不知道,但我找不到错误所在,或者是由于某种错误。这个错误指向第235行,所以最后一个函数的结束。

This is the code

这是代码

#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;
                }
            }
        }
    }
}

1 个解决方案

#1


5  

Run your code through indent. It can help you find this sort of problem very quickly.

通过缩进运行代码。它能帮助你很快发现这类问题。

Here's the output from your code. As you can see, the problem is caused by a missing brace at the end of count_match():

这是代码的输出。正如您所看到的,这个问题是由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

#1


5  

Run your code through indent. It can help you find this sort of problem very quickly.

通过缩进运行代码。它能帮助你很快发现这类问题。

Here's the output from your code. As you can see, the problem is caused by a missing brace at the end of count_match():

这是代码的输出。正如您所看到的,这个问题是由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