C编程:函数与数组 [英] C programming : functions with arrays

查看:127
本文介绍了C编程:函数与数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写它发现了与那里的二维数组充满了用户输入的4行,山坳4二维数组最大值的函数。我知道我的主要错误是在函数的数组,但我不知道它是什么。

这将大大pciated如果有人能找到我在哪里出了错,而不是写一个新的code AP $ P $。除非我只是去南部。

我尝试:

 的#include<&stdio.h中GT;最大无效(int数组);诠释主(){
    INT X,Y,垃圾;
    int数组[4] [4];
    INT行= 4;
    INT COL = 4;
    INT BIGNUM,越大;
    BIGNUM =最大(数组);
    的printf(请输入一个值:)    为(X = 0; X&下;排; X ++){
        为(γ= 0; Y≤(列); Y ++){
            scanf函数(%d个,&安培;数组[X] [Y]);
            scanf函数(%d个,&安培;垃圾);
        }
    }    的printf(数组中人数最多的是:BIGNUM);
}虚空最大(int数组[] []){
    为(X = 0; X&下;排; X ++){
        为(γ= 0; Y≤(COL-1); Y ++){
            如果(阵列[X] [Y]>阵[X] [Y + 1]){
                数组[X] [Y + 1] =阵列[X] [Y]
            }
        }
        的printf(\\ n)
    }
    大阵= [X] [Y]
    返回大;
}


解决方案

您已经声明了功能最大的为无效,并试图存储函数的返回值,这是不可能的,所以更改为int数据类型。
然后将行和山口变量值不传递到最大的功能。
要么你应该全局声明它或者把它作为一个参数传递给函数最大。
而你试图获得垃圾的价值,我不知道什么是需要的。你可以将其删除。
而在做最大的功能如下更改:

 的#include<&stdio.h中GT;
诠释最大(中间体[4] [4]);
INT行= 4;
INT COL = 4;
诠释的main()
{INT X,Y,垃圾;
    int数组[4] [4];
    INT BIGNUM,越大;
    的printf(请输入一个值:);    为(X = 0; X&下;排; X ++)
    {
            对于(Y = 0; Y<西; Y ++)
            {
                    scanf函数(%d个,&安培;数组[X] [Y]);
                    // scanf函数(%d个,&安培;垃圾);
            }
    }    BIGNUM =最大(数组);
    的printf(数组中的最大号码为:%d个\\ N,BIGNUM);
    返回0;
    }
    INT最大(int数组[4] [4])    {
    INT X = 0,Y = 0,大=阵列[0] [0];
    为(X = 0; X&下;排; X ++)
    {
            对于(Y = 0; Y<西; Y ++)
            {
                    如果(阵列[X] [Y]>大)
                    {
                            大阵= [X] [Y]                    }
            }
            的printf(\\ n);    }
    返回大;
    }

试试这个code !!!!!

I'm trying to write a function which finds the largest value in a 2d array with row of 4 and col of 4 where the 2d array is filled with user input. I know my main error is with the array in the function but am not sure what it is.

It would be greatly appreciated if someone could find where I went wrong rather than writing a new code. Unless I just went south.

My attempt:

#include <stdio.h>

void largest(int array);

int main() {
    int x,y,trash;
    int array[4][4];
    int row = 4;
    int col = 4;
    int bigNum,greater;
    bigNum=largest(array);
    printf("Please enter a value: ")

    for(x=0;x<row;x++) {
        for(y=0;y<(col);y++) {
            scanf("%d",&array[x][y]);
            scanf("%d",&trash);
        }
    }

    printf("The largest number in the array is : ",bigNum);
}

void largest(int array[][]) {
    for(x=0; x<row; x++) {
        for(y=0; y<(col-1); y++) {
            if (array[x][y] > array[x][y+1]) {
                array[x][y+1] = array[x][y];
            }
        }
        printf("\n")    
    }
    big = array[x][y];
    return big;
}

解决方案

You have declared the function largest as void and trying to store the return value of the function, it is not possible, so change it as int data type. Then the row and the col variable value is not passed to the largest function. either you should declare it globally or pass it as a argument to the largest function. And you are trying to get the value for trash, I don't know what is the need for that. you can remove it. And do the following changes in the largest function:

#include <stdio.h>
int largest(int [4][4]);
int row = 4;
int col = 4;
int main()
{   int x,y,trash;
    int array[4][4];
    int bigNum,greater;
    printf("Please enter a value: ");

    for(x=0;x<row;x++)
    {
            for(y=0;y<col;y++)
            {
                    scanf("%d",&array[x][y]);
                    //scanf("%d",&trash);
            }
    }

    bigNum=largest(array);
    printf("The largest number in the array is : %d\n",bigNum);
    return 0;
    }
    int largest(int array[4][4])

    {
    int x=0,y=0,big=array[0][0];
    for(x=0;x<row;x++)
    {
            for(y=0;y<col;y++)
            {
                    if (array[x][y]>big)
                    {
                            big = array[x][y];

                    }
            }
            printf("\n");

    }
    return big;
    }

Try this code!!!!!

这篇关于C编程:函数与数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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