简体   繁体   中英

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.

#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. 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. 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:

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 Ask the user how many numbers they wish to generate and sort

o Read the input into the appropriate variable

While the user's input for the quantity of numbers is less than 2 or greater than 10,000

o Display an error message

o Ask how many numbers they wish to generate and sort o Read the input

----This is where my problems begin.------

Use a for loop to load specified quantity of random numbers into the array

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.

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); // allocate ints

and just use for(int i = 0; i < numsor; i++) { randoms[i] = generate random number here ; }

#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]);
  }
}  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM