我试图通过使用数组来使代码找到序列中最大的数字... [英] I am trying to make code to find the greatest number in the sequence by using arrays...

查看:64
本文介绍了我试图通过使用数组来使代码找到序列中最大的数字...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的代码...

the code i used...

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

int main()
{
    int arr[10]={0},i=0,j=0,k=0,h,m,g;
    printf("Enter Value: ");
    for(i=0;i<10;i++)
    {
        scanf("%d",&arr[i]);
        j++;
    }
    for(k=0;k<10;k++)
    {
        for(j=0;j<10;j++)
        {

           if(arr[k]>arr[j])
           {
               g=arr[k];
           }
           else
           {
               k++;j++;
           }
        }
    }
    printf("The greatest no.=%d",g);
    return 0;
}





但是我得到了输出......





but i get the output...

Enter Value: 34
24
234
3245
234
3
54
53
54
34
The greatest no.=4200560
Process returned 0 (0x0)   execution time : 5.964 s
Press any key to continue.





我无法从调试器获得帮助..

谢谢提前...



I couldn't get help from the debugger..
Thanks in advance...

推荐答案

你的程序逻辑看起来不对。我会改变

The logic of you program looks wrong. I would change from
Quote:

for(k = 0; k< 10; k ++)

{

for(j = 0; j< 10; j ++)

{



if(arr [k]> arr [j])

{

g = arr [k];

}

其他

{

k ++; j ++;

}

}

}

for(k=0;k<10;k++)
{
for(j=0;j<10;j++)
{

if(arr[k]>arr[j])
{
g=arr[k];
}
else
{
k++;j++;
}
}
}



to


to

g = arr[0];
for (i=1; i<10; ++i)
{
  if ( g < arr[i] ) g = arr[i];
}


问题非常明显:查看代码并查看增加j和k值的次数。

答案是:每次两次。

每次for循环结束时:

The problem is fairly obvious: look at your code and see how many times you increase the value of j and k.
The answer is: twice each.
Once at the end of each for loop:
for(k=0;k<10;k++)
{
    for(j=0;j<10;j++)
    {

如果你没有找到更大的值,那么一旦进入循环内部:

And once each inside the loop if you don't find a "bigger" value:

else
{
    k++;j++;
}



这意味着您不会比较所有元素,因此您获得的结果不太可能是正确的。

正如heryalu所说,你可以简化循环:


This means you don't compare all elements, so the value you get as a result is unlikely to be right.
As heryalu says, you can simplify the loop:

g = 0;
for (k=0; k<10; k++)
{
    if (g < arr[k])
    {
        g = arr[k];
    }
}

但我首先将g设置为系统的最大负值而不是零。

对于32位有符号整数,那将是-2,147,483,648



下次,尝试使用调试器来查找这样的问题:在函数的第一行放置一个断点,调试器将停止让你看看究竟发生了什么。然后,您可以单步执行,查看变量并查看执行代码时会发生什么。它非常简单,它可以很容易地解决这样的问题!

But I would start by setting g to the maximum negative value for your system instead of zero.
For a 32 bit signed integer, that would be -2,147,483,648

Next time, try using the debugger to find problems like these: put a breakpoint on the first line of the function and the debugger will stop and let you see exactly what is going on. You can then step through, looking at your variables and seeing what happens when code is executed. It's pretty simple, and it makes solving problems like this very easy!


看看如何使用 qsort()。它比手动编码搜索要快得多,而且代码要少得多。
Look at how to use qsort(). It's npt going to be appreciably slower than manually coding the search and it's far less code.


这篇关于我试图通过使用数组来使代码找到序列中最大的数字...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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