You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C语言创建随机数数组并排序,调用排序函数的实现求助

How to Create, Sort, and Output Random Arrays in C

Hey there! Let's walk through exactly how to create two random number arrays, sort them with a reusable function, and print the sorted results in main(). I'll break down each part to clear up the confusion around declaring arrays for function calls.

Step 1: Declare Your Arrays

First, decide on the size of your arrays. Using a macro makes it easy to adjust the length later. In main(), you can declare fixed-size arrays directly on the stack like this:

#define ARRAY_SIZE 10  // Tweak this number to change how many elements your arrays hold

int main() {
    int arr1[ARRAY_SIZE];
    int arr2[ARRAY_SIZE];
    // ... rest of your code will go here
}

If you need dynamic sizes (like letting the user choose the length), you could use malloc() instead, but fixed-size works perfectly for this example.

Step 2: Generate Random Numbers

To fill the arrays with random values, you'll need the <stdlib.h> and <time.h> headers. Use srand() to seed the random number generator (so you get different values every time you run the program), then loop through each array to assign random numbers:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

// ... (macro and main declaration here)

// Initialize random seed with current time
srand(time(NULL));

// Fill arr1 with random numbers between 0 and 99
for (int i = 0; i < ARRAY_SIZE; i++) {
    arr1[i] = rand() % 100;
}

// Fill arr2 the same way
for (int i = 0; i < ARRAY_SIZE; i++) {
    arr2[i] = rand() % 100;
}

Step 3: Write a Reusable Sorting Function

Here's the key part you were confused about: when passing an array to a function, C treats it as a pointer to the first element. You can declare the function parameter in two equivalent ways—they work exactly the same:

  • void sortArray(int *arr, int size) (using pointer syntax)
  • void sortArray(int arr[], int size) (using array syntax, which is just a friendly shortcut for the pointer version)

Let's implement a simple bubble sort (you could also use the standard library's qsort() if you prefer a more efficient option):

void sortArray(int arr[], int size) {
    for (int i = 0; i < size - 1; i++) {
        for (int j = 0; j < size - i - 1; j++) {
            if (arr[j] > arr[j + 1]) {
                // Swap the two elements
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
}

Important note: Since we're modifying the array through its pointer, the changes will affect the original array in main()—no need to return anything from the function!

Step 4: Call the Sort Function and Print Results

Back in main(), call sortArray() on both arrays, then loop through each to print the sorted values:

// Sort both arrays
sortArray(arr1, ARRAY_SIZE);
sortArray(arr2, ARRAY_SIZE);

// Print sorted arr1
printf("Sorted Array 1:\n");
for (int i = 0; i < ARRAY_SIZE; i++) {
    printf("%d ", arr1[i]);
}
printf("\n");

// Print sorted arr2
printf("Sorted Array 2:\n");
for (int i = 0; i < ARRAY_SIZE; i++) {
    printf("%d ", arr2[i]);
}
printf("\n");

return 0;
}

Full Working Code

Putting it all together, here's the complete program you can copy and run:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define ARRAY_SIZE 10

void sortArray(int arr[], int size) {
    for (int i = 0; i < size - 1; i++) {
        for (int j = 0; j < size - i - 1; j++) {
            if (arr[j] > arr[j + 1]) {
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
}

int main() {
    int arr1[ARRAY_SIZE];
    int arr2[ARRAY_SIZE];

    // Seed random number generator with current time
    srand(time(NULL));

    // Fill both arrays with random numbers (0-99)
    for (int i = 0; i < ARRAY_SIZE; i++) {
        arr1[i] = rand() % 100;
        arr2[i] = rand() % 100;
    }

    // Sort the arrays
    sortArray(arr1, ARRAY_SIZE);
    sortArray(arr2, ARRAY_SIZE);

    // Print the sorted results
    printf("Sorted Array 1:\n");
    for (int i = 0; i < ARRAY_SIZE; i++) {
        printf("%d ", arr1[i]);
    }
    printf("\n\n");

    printf("Sorted Array 2:\n");
    for (int i = 0; i < ARRAY_SIZE; i++) {
        printf("%d ", arr2[i]);
    }
    printf("\n");

    return 0;
}

Quick Clarification on Array Parameters

When you pass arr1 to sortArray(), you're actually passing a pointer to the first element of arr1. That's why both int arr[] and int *arr work as parameters—they're interchangeable here. The function can modify the original array because it's working directly with the memory location of the array elements.

内容的提问来源于stack exchange,提问作者Dušo Morháč

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:24:09