如何获取数组进行排序 [英] How to get an array to sort

查看:60
本文介绍了如何获取数组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图找到一个数组来整理有人进入的4个数字。我一直在阅读如何做到这一点但没有任何作用......


这个程序应该按升序和降序对数字进行排序。


这是我到目前为止所拥有的。我正试图让他们现在提升,但我失败了。

I have been trying to get an array to sort out 4 numbers that someone enters in. I have been reading on how to do this but nothing will work...

This program is supposed to sort the numbers in ascending and descending order.

This is what I have so far. I''m trying to get them to ascend right now but I''m failing.

展开 | 选择 | Wrap | 行号

推荐答案

第17行的支撑是错误的,这样当第18行的循环开始时,第15行的循环就完成了。这意味着在第18行,我的值为4,j初始化为5,第18行的循环从不输入。


但是即使你换掉第16和17行,这是我认为你打算这样做是行不通的,因为在这种情况下,第18行的循环试图将后半部分的数据排序到数组,但尚未输入该部分数据的数据。


您不应该尝试在实际获取数据的同一循环中进行排序。获取所有数据,然后对所有数据进行排序,分2步。
Your brace at line 17 is mis-place so that by the time the loop on line 18 starts the loop on line 15 has finished. That means that at line 18 i has the value 4, j is initialised to 5 and the loop at line 18 is never entered.

However even if you swap lines 16 and 17 which is what I think you intended this will not work because in that case the loop at line 18 tries to sort the data in the second half to the array but the data for that part of the array has not been entered yet.

You should not be trying to sort in place in the same loop that is actually acquiring the data. Acquire all the data then sort all the data, 2 steps.


您可以使用标准库函数 qsort 还是需要自己实现排序功能?


如果你必须编写自己的排序函数,那么你应该熟悉更常见的排序算法。你的导师是否已经向班级提出了任何排序算法?
Can you use the Standard Library function qsort or are you required to implement the sort function yourself?

If you have to write your own sort function then you should familiarize yourself with the more common sort algorithms. Has your instructor presented any sort algorithms to the class yet?


我之前从未听说过qsort所以我不知道我们是否可以使用它。只要我们让它去做问题所要求的,他就不在乎。至于算法。在我们所在的章节中,我没有看到算法上的任何内容。现在我们正在学习指针应用程序,它所讨论的只是动态/静态数组和指针算法。这是一个在线课程,所以我们几乎要从这本书中学到一点都没有帮助.....


这是我必须要做的问题帮助任何:


编写一个程序,从键盘读取整数并将它们放在一个数组中。然后程序将数组按升序和降序排序并打印排序列表。程序不得更改原始数组或创建任何其他整数数组。
I have never heard of qsort before so I don''t know if we can use that or not. He doesn''t really care as long as we get it to do what the problem asked for. As for the algorithms. I don''t see anything on algorithms in the chapter we are on. Right now we are learning about pointer applications and all it talks about is dynamic/static arrays and pointer arithmetic. This is an online class so we pretty much have to learn from the book which doesn''t help at all.....

Here is the problem i have to do if it helps any:

Write a program that reads integers from the keyboard and places them in an array. The program then will sort the array into ascending and descending order and print the sorted lists. The program must not change the original array or create any other integer arrays.


这篇关于如何获取数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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