简体   繁体   English

如何使用 for 循环在 C 中加载最多包含 10,000 个数字的数组?

[英]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.

相关问题 如何用前10个质数填充数组? - How do I fill up the array with the first 10 prime numbers? 在C中找到高达10,000,000的平方 - Finding the squares up to 10,000,000 in C 在C中显示从2-10,000起的质数 - Displaying primes from 2-10,000 in C 你如何在For-Loop函数中获得最大数字和最小数字,问题要求输入10个数字并计算特定类型的数字 - How do you get the largest number and smallest number in a For-Loop function, The problems asks for a 10 number input and count specific type numbers 对于C语言:如何使用每行For循环显示不断增长的数字系列? - For C Language: How to do you Display a growing Series of Numbers using the For Loop per line? 如何在 c 的循环中将一个字符数组中的字符分配给另一个字符数组? - How do you assign a char from one char array to another char array in a loop in c? 如何使用GNU C Vector Extensions从/向数组加载/存储? - How do you load/store from/to an array of doubles with GNU C Vector Extensions? 如何使用循环将值输入到 C 中特定大小的数组中? - How do you use a loop to enter values into an array of a specific size in C? 在C中使用for循环将数字添加到数组中 - Adding numbers to an array with for loop in C 如何将一组数字插入到链表中? - How do you insert an array of numbers in to a linked list?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM