返回在二维阵列函数数组? [英] Returning an array in a two dimensional array function?

查看:137
本文介绍了返回在二维阵列函数数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用功能不同的数组中返回二维数组的平均值,该程序运行正常,但它返回一个很大的负数,我如何返回数组或申请指向我的功能?我在哪里可以添加指针,使其工作?

我遇到这样的:结果
警告:传递returnAvg'的参数1时将整数指针,未作施放[默认启用] |

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&math.h中GT;无效returnAvg(INT allTest [2] [2],诠释学生,INT测试);诠释的main()
{    诠释学生= 2,试验= 2,I,J;
    INT allTest [学生] [测试];
    对于(i = 0; I<学生;我++){
        为(J = 0; J<试验; J ++){
            的printf(学生[%D]。测试内容[%d]的比分是>中,I + 1,J + 1);
            scanf函数(%d个,&安培; allTest [I] [J]);
        }
    }
    returnAvg(allTest [2] [2],学生试验);
    返回0;
}
无效returnAvg(INT allTest [2] [2],诠释学生,诠释测试){
    INT I,J;
    INT平均(学生);    对于(i = 0; I<学生;我++){
        INT总和= 0;
        为(J = 0; J<试验; J ++){
            总和+ =(allTest [I] [J]);
        }
        平均[I] = SUM /测试;
    }
    的printf(平均为%d,%D,AVG [0],平均[1]);
    返回;
}


解决方案

您用来打发阵列功能returnAvg的方法是错误的!我认为最简单的方法是,以传递数组的指针。这是因为这种阵列是一大块或连续的内存区域!

我认为阵列和载体可使用不同的方式来分配!也许使用C ++新的或C的malloc;但是这会成为你的下一个步骤!

检索含有平均向量的方法将在下面讨论!

我在64位系统中添加此code到下编译你的code主:

 为(i = 0; I<学生;我++)
    为(J = 0; J<试验; J ++)
        的printf(%P \\ N,及(allTest [I] [J]));

的输出应是这样的:

  0x7fff0cd89e60
0x7fff0cd89e64
0x7fff0cd89e68
0x7fff0cd89e6c

这说明我说的!所有的元素都是连续的!

我们了解到,有一个基地指针指向第一个元素。在输出是0x7fff0cd89e60(即指针allStudent [0] [0])。

此指针和数组的元素的所有指针之间的关系是:

  0x7fff0cd89e60 +的sizeof(INT)*(我*测试+ J)

陈述的指针运算,我们可以修改你的函数为:

 无效returnAvg为(int * allTest,诠释学生,诠释测试){
    INT I,J;
    INT平均(学生);    对于(i = 0; I<学生;我++){
        INT总和= 0;
        为(J = 0; J<试验; J ++){
            总和+ =(allTest [我*学生+ J]);
        }
        平均[I] = SUM /测试;
    }
    的printf(平均为%d,%D,AVG [0],平均[1]);
    返回;
}

您可以调用这个函数在你的主要为:

  returnAvg(及(allTest [0] [0]),学生试验);

现在我们可能会看到怎样的平均数组传递给主!

在这里,code,你也可以修改学生和测试结果的数量!

 无效returnAvg为(int * AVG,为int * allTest,诠释学生,INT测试);诠释的main()
{    诠释学生= 2,试验= 2,I,J;
    INT allTest [学生] [测试];    INT平均(学生);    / *
    对于(i = 0; I<学生;我++)
    为(J = 0; J<试验; J ++)
           的printf(%P \\ N,及(allTest [I] [J]));
    * /    对于(i = 0; I<学生;我++){
        为(J = 0; J<试验; J ++){
            的printf(学生[%D]。测试内容[%d]的比分是>中,I + 1,J + 1);
            scanf函数(%d个,&安培; allTest [I] [J]);
        }
    }
    returnAvg(平均,及(allTest [0] [0]),学生试验);
    对于(i = 0; I<学生;我++){
        的printf(学生%d个平均数:%d \\ n,i + 1的,平均[I]);
    }    返回0;
}无效returnAvg为(int * AVG,为int * allTest,诠释学生,诠释测试){
    INT I,J;    对于(i = 0; I<学生;我++){
        INT总和= 0;
        为(J = 0; J<试验; J ++){
            总和+ =(allTest [我*测试+ J]);
        }
        平均[I] = SUM /测试;
    }    返回;
}

I want to return the average of the two dimensional array with a different array using a function, the program runs fine, but it returns a big negative number, how do i return the array or apply pointers to my function? where do i add the pointer to make it work?

I encounter this:
warning: passing argument 1 of 'returnAvg' makes pointer from integer without a cast [enabled by default]|

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

void returnAvg(int allTest[2][2],int students,int test);                       



int main ()                                                                    
{                                                                              

    int students = 2, test = 2, i,j;                                           
    int allTest[students][test];                                               


    for(i=0;i<students;i++){                                                   
        for(j=0;j<test;j++){                                                   
            printf("Student [%d] test [%d] score was> ",i+1,j+1);              
            scanf("%d",&allTest[i][j]);                                        
        }                                                                      
    }                                                                          
    returnAvg(allTest[2][2],students,test);                                    


    return 0;                                                                  
}                                                                              
void returnAvg(int allTest[2][2],int students,int test){                       
    int i,j;                                                                   
    int avg[students];                                                         

    for(i=0;i<students;i++){                                                   
        int sum = 0;                                                           
        for(j=0;j<test;j++){                                                   
            sum += (allTest[i][j]);                                            
        }                                                                      
        avg[i] = sum/test;                                                     
    }                                                                          
    printf("the average is %d, %d", avg[0],avg[1]);                            
    return;                                                                    
} 

解决方案

The way you used to pass the array to the function returnAvg is wrong! The simplest way I see it's to pass the array as a pointer. This because this kind of array is a chunk or contiguous memory areas!

I think the array and the vector may be allocated using a different way! Maybe using C++ new or C malloc; but this will become your next step!

The way to retrieve the vector containing the avg will be discussed below!

I've compiled your code under a 64 bit system adding this code into your main:

for(i=0;i<students;i++)
    for(j=0;j<test;j++)                                                   
        printf("%p\n",&(allTest[i][j]));

The output shall be something like this:

0x7fff0cd89e60
0x7fff0cd89e64
0x7fff0cd89e68
0x7fff0cd89e6c

This indicates what I said! All elements are contiguous!

We understand that there's a "base" pointer that points the first element. In the output is 0x7fff0cd89e60 (that is the pointer to allStudent[0][0]).

The relationship between this pointer and all pointers of the element of the array is:

0x7fff0cd89e60 + sizeof(int) * (i*test+j)

Stating the pointer arithmetic we can modify your function as:

void returnAvg(int * allTest,int students,int test){                       
    int i,j;                                                                   
    int avg[students];                                                         

    for(i=0;i<students;i++){                                                   
        int sum = 0;                                                           
        for(j=0;j<test;j++){                                                   
            sum += (allTest[i*students+j]);                                            
        }                                                                      
        avg[i] = sum/test;                                                     
    }                                                                          
    printf("the average is %d, %d", avg[0],avg[1]);                            
    return;                                                                    
} 

You may call this function in your main as:

returnAvg(&(allTest[0][0]),students,test);  

Now we may see how to pass the avg array to the main!

Here the code where you may also modify the number of students and tests results!

void returnAvg(int *avg, int * allTest,int students,int test);

int main ()                                                                    
{                                                                              

    int students = 2, test = 2, i,j;                                           
    int allTest[students][test];                                               

    int avg[students];

    /*
    for(i=0;i<students;i++)
    for(j=0;j<test;j++)                                                   
           printf("%p\n",&(allTest[i][j]));
    */

    for(i=0;i<students;i++){                                                   
        for(j=0;j<test;j++){                                                   
            printf("Student [%d] test [%d] score was> ",i+1,j+1);              
            scanf("%d",&allTest[i][j]);                                        
        }                                                                      
    }                       


    returnAvg(avg,&(allTest[0][0]),students,test);                                    
    for(i=0;i<students;i++){                                                   
        printf("Student %d average: %d\n", i+1, avg[i]);
    }

    return 0;                                                                  
}

void returnAvg(int * avg, int * allTest,int students,int test){                       
    int i,j;                                                                   

    for(i=0;i<students;i++){                                                   
        int sum = 0;                                                           
        for(j=0;j<test;j++){                                                   
            sum += (allTest[i*test+j]);                                            
        }                                                                      
        avg[i] = sum/test;                                                     
    }                                                                          

    return;                                                                    
} 

这篇关于返回在二维阵列函数数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆