如何在c中将二维数组与一维数组进行比较?

时间:2022-11-20 21:34:19

I am hoping to compare two array and print two scores.

我希望比较两个阵列并打印两个分数。

In the case below, should be 1 and 5 but I got 6488164 and 7536740. (as the first one only 7 is bigger than 4 and the next row is everything bigger)

在下面的情况下,应该是1和5,但我有6488164和7536740.(因为第一个只有7大于4,下一行是一切更大)

Is it possible to compare a two-dimensional array with a single array and create an array with it? What did I do wrong or what should I do in order to do the comparison?

是否可以将二维数组与单个数组进行比较并使用它创建数组?我做错了什么或者我该怎么做以进行比较?

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

int 
main(int argc, char *argv[]) {
	int mm_avg[2][5] = {{1,2,3,7,4},{2,3,4,5,7}};
	int lt_avg[5]={1,2,3,4,5};
	int i, j, score[100];
	
	for (i=0; i<5; i++){
		for (j=0; j<2; j++){
			if(mm_avg[j][i]>lt_avg[i]){
					score[j]++;
			}
		}
	}
	
	for (j=0; j<2; j++){
		printf("%d\n", score[j]);
	}
	return 0;
}
	

2 个解决方案

#1


1  

The score array is not initialized, so all 100 values in the array are just whatever happens to be lying around in memory.

得分数组未初始化,因此数组中的所有100个值都只是在内存中发生的任何事情。

Try int i, j, score[100] = {0};. This will initialize all 100 values to 0.

尝试int i,j,得分[100] = {0};这会将所有100个值初始化为0。

#2


1  

In C/C++, integers that are not initialized have an undefined value. Unlike say for instance, Java, which will assign a default value of 0 to an integer. In general it is good practice to always explicitly initialize your objects when declared.

在C / C ++中,未初始化的整数具有未定义的值。与例如Java不同,它将默认值0分配给整数。通常,最好在声明时始终显式初始化对象。

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

main(int argc, char *argv[])
{
    int mm_avg[2][5] = {{1,2,3,7,4},{2,3,4,5,7}};
    int lt_avg[5]={1,2,3,4,5};
    int i, j, score[2];

    for(int ndx = 0; ndx < 2; ++ndx)
    {
        score[ndx] = 0;
    }

    for (i = 0; i < 5; ++i)
    {
        for (j = 0; j < 2; ++j)
        {
            if(mm_avg[j][i] > lt_avg[i])
            {
                    /*printf("%d\n", j);
                    printf("%s", "mm_avg: ");
                    printf("%d\n", mm_avg[j][i]);
                    printf("%s", "lt_avg: ");
                    printf("%d\n", lt_avg[i]);
                    */
                    score[j]++;
            }
        }
    }

    for (j=0; j<2; j++)
    {
        printf("%d\n", score[j]);
    }
    return 0;
}

#1


1  

The score array is not initialized, so all 100 values in the array are just whatever happens to be lying around in memory.

得分数组未初始化,因此数组中的所有100个值都只是在内存中发生的任何事情。

Try int i, j, score[100] = {0};. This will initialize all 100 values to 0.

尝试int i,j,得分[100] = {0};这会将所有100个值初始化为0。

#2


1  

In C/C++, integers that are not initialized have an undefined value. Unlike say for instance, Java, which will assign a default value of 0 to an integer. In general it is good practice to always explicitly initialize your objects when declared.

在C / C ++中,未初始化的整数具有未定义的值。与例如Java不同,它将默认值0分配给整数。通常,最好在声明时始终显式初始化对象。

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

main(int argc, char *argv[])
{
    int mm_avg[2][5] = {{1,2,3,7,4},{2,3,4,5,7}};
    int lt_avg[5]={1,2,3,4,5};
    int i, j, score[2];

    for(int ndx = 0; ndx < 2; ++ndx)
    {
        score[ndx] = 0;
    }

    for (i = 0; i < 5; ++i)
    {
        for (j = 0; j < 2; ++j)
        {
            if(mm_avg[j][i] > lt_avg[i])
            {
                    /*printf("%d\n", j);
                    printf("%s", "mm_avg: ");
                    printf("%d\n", mm_avg[j][i]);
                    printf("%s", "lt_avg: ");
                    printf("%d\n", lt_avg[i]);
                    */
                    score[j]++;
            }
        }
    }

    for (j=0; j<2; j++)
    {
        printf("%d\n", score[j]);
    }
    return 0;
}