[英]How do you load an array with up to 10,000 numbers in C with a for loop?
This is what I have so far.这是我到目前为止。 My random number generator works but I'm not sure how to load the numbers a user wants into an array.
我的随机数生成器可以工作,但我不确定如何将用户想要的数字加载到数组中。 It also check to make sure more than 2 and less than 10,000 numbers have been entered.
它还检查以确保输入的数字超过 2 个且少于 10,000 个。
#include <stdio.h>
int randu(int randoms);
int main (int argc, char *argv[]) {
int numsor;
int randoms[];
int x=0;
if (argc == 1){
printf("How many numbers do you wish to sort? \n");
scanf("%d", &numsor);
}
if (argc == 2){
sscanf(argv[1], "%d", &numsor);
}
if (argc > 2){
printf("how many numbers do you wish to sort? \n");
scanf("%d", &numsor);
}
while (numsor<2 || numsor>10000){
if (numsor < 2){
printf("Error please enter a number more than 2. \n");
}
else{
printf("Error please enter a number less than 10,000. \n");
}
scanf("%d", &numsor);
}
}
int randu(int numsor){
static int seed=17;
seed=(25179*seed+13849)%65536;
return seed;
};
This is the exact pseudo code I'm supposed to go by.这是我应该通过的确切伪代码。
Write a program that will allow the user to generate as many random numbers as they wish (up to 10,000), sort them into ascending order, and print the sorted numbers.编写一个程序,允许用户生成任意数量的随机数(最多 10,000 个),将它们按升序排序,然后打印排序后的数字。 You must use an array to store the numbers, and subroutines to generate and sort the numbers.
您必须使用数组来存储数字,并使用子例程来生成和排序数字。 You MUST make sure that the user selects at least 2 numbers but not more than 10,000.
您必须确保用户选择至少 2 个号码但不超过 10,000 个。 Quantity of numbers user wishes to sort may come from command line and if not, the user should be prompted.
用户希望排序的数字数量可能来自命令行,如果不是,则应提示用户。 Sorted output MUST be on one line separated by a single space.
排序的输出必须在一行上,由一个空格分隔。
Pseudocode for Main: Main的伪代码:
If user enters the quantity of numbers to generate and sort on command line如果用户在命令行输入要生成和排序的数字数量
o Convert and assign that number to the appropriate variable Otherwise o 转换该数字并将其分配给适当的变量 否则
o Ask the user how many numbers they wish to generate and sort o 询问用户他们希望生成和排序的数字数量
o Read the input into the appropriate variable o 将输入读入适当的变量
While the user's input for the quantity of numbers is less than 2 or greater than 10,000当用户输入的数字数量小于 2 或大于 10,000
o Display an error message o 显示错误信息
o Ask how many numbers they wish to generate and sort o Read the input o 询问他们希望生成和排序多少个数字 o 阅读输入
----This is where my problems begin.------ ----这就是我的问题开始的地方。------
Use a for loop to load specified quantity of random numbers into the array使用 for 循环将指定数量的随机数加载到数组中
Invoke the bubble sorter (Pass the array and number of items in it)调用冒泡排序器(传递数组和其中的项目数)
Use a for loop to print the sorted array from first item to last.使用 for 循环打印从第一项到最后一项的排序数组。
int randu(void);
Definitions:
int randu(){
static int seed= 17;
seed = (25179*seed+13849)%65536; return seed;
}
void bubble(int *, int);
void bubble(int a[], int n) { int i, j;
for (i = 0; i < n-1; i++) for (j = n-1; i < j; j--)
if (a[j-1] > a[j]) swap(&a[j-1],&a[j]);
void swap(int *, int *);
void swap(int *a, int *b) { int temp;
temp = *a; *a = *b; *b = temp;
}
}
Just do this: int* randoms = (int *)malloc(sizeof(int)*numsor);只需这样做: int* randoms = (int *)malloc(sizeof(int)*numsor); // allocate ints
// 分配整数
and just use for(int i = 0; i < numsor; i++) { randoms[i] = generate random number here ;只需使用 for(int i = 0; i < numsor; i++) { randoms[i] = 在这里生成随机数; }
}
#include "stdio.h"
#include "malloc.h"
int randu() {
static int seed= 17;
seed = (25179*seed+13849)%65536;
return seed;
}
void swap(int *a, int *b) {
int temp;
temp = *a;
*a = *b;
*b = temp;
}
void bubble(int a[], int n) {
int i, j;
for (i = 0; i < n-1; i++) {
for (j = n-1; i < j; j--) {
if (a[j-1] > a[j]) {
swap(&a[j-1],&a[j]);
}
}
}
}
void main (int argc, char *argv[]) {
int numsor;
/* Your input logic here ... */
int* randoms = (int*)malloc(sizeof(int) * numsor);
for (int i = 0; i < numsor; i++) {
randoms[i] = randu();
}
bubble(randoms, numsor);
for (int i = 0; i < numsor; i++) {
printf("[%d]: %d\n", i, randoms[i]);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.