【发布时间】:2021-11-08 17:05:25
【问题描述】:
在我大学一年级的课程中,我们刚刚开始摆弄数组,在工作表上我得到了这段代码,这似乎不起作用。我一直在扫描并寻找问题,但似乎没有任何问题可以解决。这是我的代码...
#include <stdio.h>
#include <stdbool.h>
int main(){
int size = 10;
float suspect[size]; //Declaring suspect array
int sizeR = 3;
int sizeC = 10;
float criminals[sizeR][sizeC]; //Declaring criminals array
//Read 10 input values into suspect array from keyboard
printf("Enter the 10 chromosomes of the suspect separated by spaces: \n");
for (int i = 0; i < size; i++)
scanf(" %f", &suspect[i]);
//Read multiple profiles of 10 values into criminals array from the keyboard
for (int i = 0; i < sizeR; i++){
printf("Enter the 10 chromosomes of the %dth criminal: \n", i+1);
//Read 10 input values of a criminal into criminals array from the keyboard
for (int j = 0; j < sizeC; j++)
scanf(" %f", &criminals[i][j]);
}
//Match two profiles
bool match = true;
for (int i = 0; i < size; i++)
if(suspect[i] != criminals[i]) //Error is in this line
match = false;
//Display matching result
if (match)
printf("The two profiles match! \n");
else
printf("The two profiles don't match! \n");
return 0;
}
当我运行这段代码时,我返回:
错误:二进制表达式的操作数无效('float' 和 'float [sizeC]')
错误指向匹配的两个配置文件部分中的 !=。对不起,如果解决方案很简单,编码对我来说相对较新,我正在努力使用谷歌找到这个特定问题的解决方案。
【问题讨论】:
-
错误告诉您您正在尝试将
float与数组进行比较。那显然行不通。由于criminals是一个二维数组,因此您还需要从第二维中选择一个元素 -
它应该给出行号。无论如何,为什么你的“嫌疑人”和“罪犯”是
float?这是一个坏主意,尤其是在您尝试比较它们时。 -
请说明您对
suspect[i] != criminals[i]中两个操作数的数据类型的理解以及您的期望该行应该做什么。 -
@Yunnosch 我希望它返回一个打印,说明犯罪阵列与嫌疑阵列不匹配。该程序的目标是比较两个数组并告诉用户输入的数组是否与“系统”上的数组匹配
-
@EugeneSh。代码来自我们大学提供给我们的实践工作表,这不是我写的,只是改写成 VS
标签: c if-statement multidimensional-array comparison