如何获得此代码的正确输出以及我在代码错误的地方 [英] How do I get the correct output for this code and where I m wrong in the code

查看:207
本文介绍了如何获得此代码的正确输出以及我在代码错误的地方的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查找数组的最大元素

我尝试过的事情:

#include< stdio.h>
void arrays(int a [],int size);
void maximum();

int main()
{
int array [5];
arrays(array,5);
printf("\ n");

printf(最大值为\ n");
maximum();

返回0;
}
无效数组(int a [],int size)
{
int array [5],i;
printf(输入五个学生的数量\ n");
for(i = 0; i< 5; i ++)
{
scanf(%d",& array [i]);
}
printf(数组元素为\ n");
for(i = 0; i< 5; i ++)
{
printf(%d",array [i]);
}

}
void maximum()
{
int array [5];
int i;
for(i = 0; i< 5; i ++)
{
if(array [0]< array [i])>
{
array [0] = array [i];
}
}
printf(%d",array [0]);


}

to find maximum element of array

What I have tried:

#include<stdio.h>
void arrays(int a[] ,int size);
void maximum();

int main()
{
int array[5];
arrays(array,5);
printf("\n");

printf("The maximum value is\n");
maximum();

return 0;
}
void arrays(int a[],int size)
{
int array[5],i;
printf("Enter the number of five students\n");
for(i=0;i<5;i++)
{
scanf("%d",&array[i]);
}
printf("Elements of array are\n");
for(i=0;i<5;i++)
{
printf("%d",array[i]);
}

}
void maximum()
{
int array[5];
int i;
for(i=0;i<5;i++)
{
if(array[0]<array[i])>
{
array[0]=array[i];
}
}
printf("%d",array[0]);


}

推荐答案

嗯...我从哪里开始? :laugh:
您需要返回几个阶段,然后思考您要做什么.
查看您的代码(我已经删除了多余的内容,以便您可以更清晰地看到它们)
Um...where do I start? :laugh:
You need to go back a couple of stages and think about what you are trying to do.
Look at your code (I''ve ripped out the extraneous stuff so you can see more clearly)
int main()
    {
    int array[5];
    arrays(array,5);
    maximum();
    return 0;
    }

您声明一个数组,将其传递给arrays函数以使用值填充它,然后调用一个名为maximum的函数-但由于您没有将数组传递给它,因此可以无法访问您要查看的值!
而且比那更糟,因为您的arrays函数无论如何都不会将数据放入数组中:

You declare an array, pass it to the arrays function to fill it with values, and then call a function called maximum - but since you don''t pass the array to it, it can''t access the values you want to look at!
And it''s worse than that, because your arrays function doesn''t put the data in the array anyway:

void arrays(int a[],int size)
    {
    int array[5],i;
    printf("Enter the number of five students\n");
    for(i=0;i<5;i++)
        {
        scanf("%d",&array[i]);
        }
        printf("Elements of array are\n");
    for(i=0;i<5;i++)
        {
        printf("%d",array[i]);
        }
    }

它将创建一个新的数组,填充该数组,并且对您传递的数组完全不执行任何操作-只需注意传递的大小即可!因此,用户输入的值将在函数末尾丢弃,并且无论如何都无法传递给maximum函数.

停止编码:回到您的家庭作业问题并加以思考.然后设计您需要哪些数据,以及需要去往何处的数据.然后开始编码-但永远不要只是跳入代码"-如果您不计划首先要做的事情,那么最终会遇到类似这样的混乱代码.

It creates a new array, fills that and does nothing at all with the array you passed it - much less pay any attention to the size you passed in! So the values your user enters are thrown away at the end of the function, and couldn''t be passed to the maximum function anyway.

Stop coding: go back to your homework question and think about it. Then design what data you need, and what needs to go to and from where. Then start coding - but never just "leap into code" - if you don''t plan what you need to do first, you end up with confused code like this.


您应该学习尽快使用调试器.现在是时候查看您的代码执行并确保它执行您期望的操作了,而不是猜测您的代码在做什么.

调试器使您可以逐行执行代码,检查变量,然后您会发现它停止了您所期望的工作.
调试器-维基百科,免费百科全书 [ Visual Studio 2010中的精通调试-入门指南 [ ^ ]

调试器在这里向您展示您的代码在做什么,并且您的任务是将其与应该做的事情进行比较.
如果代码未按预期执行操作,则说明您接近错误.
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner''s Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don''t do what is expected, you are close to a bug.


这篇关于如何获得此代码的正确输出以及我在代码错误的地方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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