array的降序. [英] Descending order of array .

查看:256
本文介绍了array的降序.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经按照我的方式开发了该程序,但没有成功

我尝试过的事情:

i have worked on this program in my way but it is not working

What I have tried:

#include<stdio.h>
#include<conio.h>
main () {
int  r , f ,  i , j , k , a, z, d, count =0;
int arr[50];
int stl[10];

printf(" enter the number of elements : ");
scanf(" %d" , &a);
for(i=0;i<a;i++){
printf(" Enter %d element: \n" , i);
scanf(" %d" , &arr[i]);
}
for(i=0;i<5;i++){
	count = 0;
	for(j=0;j<5;j++){
		   if(arr[i] >= arr[j]){
		   ++count;
}else{}
}
r =0;
f = a;
	do{
	if( count == f){
	 stl[r] = arr[i];
	 --a;
	 ++r;
	 }else{
	 --a;
	 ++r;
	 }
	 }while(  f <= 0);



}
for(z=0;z<=5;z++){
printf(" %d" , stl[z]);
}

getch();
return 0;

}


[edit]已添加代码块-OriginalGriff [/edit]


[edit]Code block added - OriginalGriff[/edit]

推荐答案

此处有几处内容:
1)我们不知道该怎么办,因此完全不可能告诉您这是怎么回事.告诉您如何修复.
2)我们不知道您在做什么,这是您没有想到的,还是没有做到这一点.由于我们不知道您输入了什么值,因此我们甚至无法在相同条件下运行它.
3)这是您的作业,不是我的.这意味着它是整个学习过程的一部分,其中包括使您编写的代码正常工作.

编译并不意味着您的代码是正确的! :laugh:
将开发过程视为编写电子邮件:成功编译意味着您以正确的语言(例如英语,而不是德语)编写了电子邮件,而不是电子邮件中包含您要发送的消息.

因此,现在您进入开发的第二阶段(实际上是第四或第五阶段,但是稍后将进入较早的阶段):测试和调试.

首先查看它的功能以及与您想要的功能有何不同.这很重要,因为它可以为您提供信息,说明执行该操作的原因.例如,如果打算让用户输入一个数字并将其加倍并打印答案,那么输入/输出是这样的:
Several things here:
1) We have no idea what it is supposed to do, so telling you what is wrong with it is completely impossible. As it telling you how to fix it.
2) We have no idea what it is doing that you didn''t expect, or not doing that you did. As as we have no idea what values you entered, we can''t even run it against the same conditions.
3) This is your homework, not mine. And that means it is part of a whole learning process,which includes getting the code you wrote working.

Compiling does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it''s the fourth or fifth, but you''ll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it''s doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16

然后,很显然,问题出在加倍它的位上-它不是自身相加或乘以2,而是自身相乘而后返回输入的平方.
因此,您可以看一下代码,很明显它就在这里:

Then it''s fairly obvious that the problem is with the bit which doubles it - it''s not adding itself to itself, or multiplying it by 2, it''s multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it''s obvious that it''s somewhere here:

int Double(int value)
   {
   return value * value;
   }


一旦知道什么地方可能出问题了,就开始使用调试器找出原因.在您的行上放置一个断点:


Once you have an idea what might be going wrong, start using teh debugger to find out why. Put a breakpoint on your line:

f = a;


并运行您的应用.在执行代码之前,请考虑一下代码中的每一行应该执行的操作,并将其与使用"Step over"按钮依次执行每一行时的实际操作进行比较.它符合您的期望吗?如果是这样,请转到下一行.
如果没有,为什么不呢?有什么不同?

这是一项技能,值得开发,因为它可以在现实世界以及开发中为您提供帮助.和所有技能一样,它只能通过使用来提高!

帮自己一个忙:几个忙.
缩进您的代码.如果您可以看到哪里,它会使整个负载更易于阅读和理解.此刻,即使加上我添加的格式,这也是狗的晚餐!

停止使用单个字符名称:它们做两件事.第一个是使您的代码难以阅读-如果您使用的名称能够反映变量的含义,它会自我记录"代码,并使代码易于理解.第二个是它使您的代码更健壮和可靠.如果您看这样的代码:


and run your app. Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?

This is a skill, and it''s one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!

And do yourself a favour: Several favours.
Indent your code. It makes it a whole load easier to read and understand if you can see what is where. At the moment, (even with the formatting I added) that is a dog''s dinner!

Stop using single character names: they do two things. The first is the make your code hard to read - if you use names which reflect what the variable is there for, it "self documents" the code, and makes it a lot easier to understand. The second is that it makes your code more robust and reliable. If you look at code like this:

if (a < b)

您正在比较的东西并不明显.

It isn''t obvious what you are comparing.

if (applesCount < maxOranges)

一目了然,这显然是错误的!

One glance and it''s obvious that that is wrong!


学会正确地缩进代码,显示代码的结构,有助于阅读和理解.
Learn to indent properly your code, it show its structure and it helps reading and understanding.
#include<stdio.h>
#include<conio.h>
main () {
    int  r , f ,  i , j , k , a, z, d, count =0;
    int arr[50];
    int stl[10];

    printf(" enter the number of elements : ");
    scanf(" %d" , &a);
    for(i=0;i<a;i++){
        printf(" Enter %d element: \n" , i);
        scanf(" %d" , &arr[i]);
    }
    for(i=0;i<5;i++){
        count = 0;
        for(j=0;j<5;j++){
            if(arr[i] >= arr[j]){
                ++count;
            }else{}
        }
        r =0;
        f = a;
        do{
            if( count == f){
                stl[r] = arr[i];
                --a;
                ++r;
            }else{
                --a;
                ++r;
            }
        }while(  f <= 0);
    }
    for(z=0;z<=5;z++){
        printf(" %d" , stl[z]);
    }
    getch();
    return 0;
}


专业程序员的编辑器具有此功能,而其他功能例如括号匹配和语法突出显示.
Notepad ++主页 [ ^ ]
ultraedit [ ^ ]


Professional programmer''s editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]

报价:

我已经按照我的方式开发了该程序,但没有成功

i have worked on this program in my way but it is not working


由于我们不知道代码应该做什么或机器人可以工作什么代码,因此我只能为您提供一个建议,即使用一种可以帮助您了解正在发生的事情的工具.该工具是调试器.

有一个工具可以让您查看代码的作用,其名称为 debugger .这也是一个很好的学习工具,因为它可以向您显示现实,并且您可以看到哪个期望与现实相匹配.
当您不了解代码在做什么或为什么要做什么时,答案是调试器.
使用调试器查看您的代码在做什么.只需设置一个断点并查看您的代码执行情况,调试器就可以让您一行一行地执行代码,并在执行时检查变量.

调试器-维基百科,免费百科全书 [ Visual Studio 2010中的精通调试-入门指南 [ ^ ]
使用Visual Studio 2010进行基本调试-YouTube [


As we have no idea of what the code should do or in what the code is bot working, the only advice I can give you is to use a tool that will help you to understand what is going on. That tool is the debugger.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don''t understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner''s Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don''t find bugs, it just help you to. When the code don''t do what is expected, you are close to a bug.


这篇关于array的降序.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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