【问题标题】:How to check elements in row of 2D array, store them, and compare them?如何检查二维数组行中的元素,存储它们并比较它们?
【发布时间】:2021-12-23 18:34:22
【问题描述】:

我一直在尝试检查二维数组的一行中的元素并将元素存储在一个变量中,以便我可以使用该变量进行比较。我迷失在如何检查元素并将其存储在变量中。

我写了这么多:

#include<stdio.h>
#include<conio.h>

int main()
{
    int array1[10][10];
    int row, column, num, found = 0, passMark = 60, fail = 0;

    printf("How many rows and columns needed: ");
    scanf("%d %d", &row, &column);

    printf("\nHow many students marks you want to enter: ");
    scanf("%d", &num);

    printf("\nEnter %d students marks: ", num);

    for(int i=0; i<row; i++)
    {
        for(int j=0; j<column; j++)
        {
            scanf("%d", &array1[i][j]);
        }
    }

    for(int i=0; i<num; i++)
    {
        // int passMark = array1[i][0];
        for(int j=1; j<num; j++)
        {
            if(array1[i][j] >= passMark)
            {
                passMark = array1[i][j];
            }
            /*else
            {
                min = array1[i][j];
            }*/
        }
        printf("\nYes!");
        // found = 0;
    }

    return 0;
}

【问题讨论】:

  • 您的代码示例完成了 passMark 的声明和初始化。然后将 array[i][j] 与 passMark 中的值进行比较。此代码段不应编译。

标签: arrays c


【解决方案1】:

代码有几个问题。

  1. 您要求用户输入row/column,但您已经声明了一个静态大小的数组10 * 10

    如果希望大小由用户决定,则应使用动态数组并使用malloc()free()

  2. 检查时,从j = 1开始跳过一个标记,你应该从0开始。

  3. 您似乎再次询问要添加多少学生以及每个学生多少分数,但您已经获得了该信息。 rows 代表学生,columns 代表每个学生的分数。

你应该在寻找这样的东西,但这是我的猜测,问题很不清楚。

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

int main(void)
{
    int **array1;
    int row, column, num, found = 0, passMark = 60, fail = 0;

    printf("How many students and how many marks per student?\n");
    
    if (scanf("%d %d", &row, &column) != 2)
    {
        /* conversion failed */
        exit(EXIT_FAILURE);
    }

    array1 = malloc(row * sizeof(int *));
   
    if (array1 == NULL)
    {
      exit(EXIT_FAILURE);
    }

    for (int i = 0; i < row; i++)
    {
        array1[i] = malloc(column * sizeof(int));

        if (array1[i] == NULL)
        {
            /* allocation failed - exit / display error */
            exit(EXIT_FAILURE);
        }

    }

    printf("\nEnter %d students marks: ", row);

    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < column; j++)
        {
            if (scanf("%d", &array1[i][j]) != 1)
            {
                exit(EXIT_FAILURE);
            }
        }
    }

    for (int i = 0; i < row; i++, fail = 0)
    {
        printf("Student's %d grades:\n", i + 1);
        for (int j = 0; j < column; j++)
        {
            if (array1[i][j] >= passMark)
            {
                printf("Passed class %d with grade : %d\n", j + 1, array1[i][j]);
            }
            else
            {
                printf("Failed class %d with grade : %d\n", j + 1, array1[i][j]);
                fail++;
            }
        }

        printf("Student %d failed %d classes", i + 1, fail);
    }

    return 0;
}

【讨论】:

  • 考虑使用指向 VLA 的指针。整个分配代码为:int (*array1)[column] = malloc(row * sizeof *array1);
  • 我已经尝试过了,我得到了我想要的东西。感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 2022-08-22
  • 2019-04-22
  • 2020-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-13
  • 2012-12-17
相关资源
最近更新 更多