在整数数组中输入100个随机变量 [英] Input 100 random variables in an integer array

查看:129
本文介绍了在整数数组中输入100个随机变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何编写一个程序,将100个随机变量作为整数数组的输入,然后将该数组按升序排序?

请告诉我.我需要知道.

How to write a program to take 100 random variables as an input in an integer array and then sort this array in ascending order?

Please tell me. I need to know.

推荐答案

因为这听起来很像作业,所以您不太可能会得到答案,随时可以剪切和粘贴-我们不会不要那样做.进行家庭作业的原因是:让您思考所学的内容,并让您真正地自己使用它.

相反,这是要经历的过程.由于您刚刚起步,因此您可以根据需要单独进行操作-即完成一个过程,并确保它可以正常工作,然后再进行下一个操作.

1)用100个随机整数填充数组.
1a)声明一个由100个整数组成的数组,这样您就可以在某个地方放置它们.
1b)设置循环以依次遍历数组中的每个整数
1c)在数组的每个元素中放置一个随机整数.
为此,您需要输入int, for rand-使用Google.

2)编写代码以证明您在数组中有100个随机整数.
2a)设置循环以依次遍历数组中的每个整数
2b)将数组的每个元素打印到控制台.
为此,您需要for, <<


3)对数组进行排序.
3a)确定如何对其进行排序.有很多方法,从琐碎的事情(使用排序功能)到复杂的事情(自己实现快速排序).只有您可以决定这如何适合您的作业问题!

祝您好运-这并不困难,您只需尝试一下即可!
Because this sounds seriously like homework, it is not likely that you will just get the answer, ready to cut and paste - we don''t do that. Homework is given for a reason: to make you think about what you have been taught, and to get you actually using it for yourself.

Instead, here are the processes to go through. Since you are just starting out, you can do them separately if you want - i.e. finish one process and be sure it works before moving to the next.

1) Fill an array with 100 random integers.
1a) Declare an array of 100 integers so you have somewhere to put them.
1b) Set up a loop to go through each integer in the array in turn
1c) Put a random integer in each element of the array.
To do these, you will need the words int, for and rand - use Google.

2) Write code to prove you have 100 random integers in the array.
2a) Set up a loop to go through each integer in the array in turn
2b) Print each element of the array to the console.
To do these, you will need for, <<


3) Sort the array.
3a) Decide how you will sort it. There are loads of ways, form the trivial (use the sort function) to the complex (implement quick sort yourself). Only you can decide how this fits into your homework question!

Good luck - it''s not difficult, all you have to do is try it yourself!


如果数组很小,则可以#include< algorithm>并使用std :: sort或使用C的qsort.请注意,std :: sort在算法上和效率上都不是很有效,并且在需要最高性能时不应该使用.
If it''s that small of an array you can #include <algorithm> and use std::sort or use C''s qsort. Note that std::sort is neither very algorithmically nor memory efficient and should not be utilized when you need the highest performance.


#include <iostream.h>
#include <stdlib.h>
int sort(my_array, my_array+ARRAY_SIZE)
int main()
{
     const int NUMBER_MAX_PLUS1 = 100;
     const int ARRAY_SIZE = 100;
     int my_array[ARRAY_SIZE];
     for (int i=0; i<ARRAY_SIZE; ++i)

         {

            { 

                my_array[i] = rand() % NUMBER_MAX_PLUS1;

                int sort(my_array, my_array+ARRAY_SIZE);

            }

            for (int i=0; i<ARRAY_SIZE; ++i)

             {

                    cout<< i<<               my_array[i]<<"\n";

             }

         }

     return 0;

}


这篇关于在整数数组中输入100个随机变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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