簡體   English   中英

生成一個數列,然后對數列進行排序

[英]generate a number sequence, and then sort the sequence

我應該編寫一個程序來處理 100 個正數的序列。 該程序應該有 3 種不同的功能,可以從菜單中選擇。

  1. 使用隨機生成器生成數字序列並將數字打印在屏幕上。 生成的數字應在 0 ≤ n ≤ 900 范圍內

  2. 我必須用冒泡排序對序列進行排序,然后必須打印數字,我不能使用內置的排序函數,例如 qsort。

  3. 退出程序。 1 和 2 必須在自己的函數中。 最后每次程序打印出數字時,它應該被打印成一個十行十列的表格。 除非選擇了選項一,否則無法選擇選項二。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 100
#define N 10
#define INVALID 0
#define VALID 1

void randomNum() {
    //random number gen
    int num[SIZE] = {0};
    int i = 0;
    
    srand(time(NULL));
    
    
for(i = 0; i < SIZE; i++) {
      
       num[i] =  rand() % 901;
    }
        for(i = 0; i < SIZE; i++) {
        printf("%4d", num[i]);
        if(i % 10 == 9)
            printf("\n");
    }  
}

void sort() {
    //sort of the generated number sequence
    int num[SIZE] = {0};
    int i = 0;
    int j = 0;
    int temp = 0;
    
     for (int i = 0 ; i < SIZE; i++)
      {
            for (int j = 0; j < SIZE - 1; j++)
            {
              if (num[j] > num[j+1]) 
                {
                temp = num[j];
                num[j]   = num[j+1];
                num[j+1] = temp;
                  }
            }
        }
            
         for(i = 0; i < SIZE; i++) {
        printf("%4d", num[i]);
        if(i % 10 == 9)
            printf("\n");
         }   
}


int main() {
    
    randomNum();
    printf("\n");
    
    sort();
    
    
    
    return 0;    
}

我已經找到了如何生成序列並對其進行排序的解決方案,並且當所有代碼都在 main() 中時它可以正常工作,但是,當我在 main() 之上放入自己的函數時它不起作用。 我被困住了,不知道如何前進。 當我運行該程序時,它會生成一個隨機序列,但隨后排序 function 只會打印出 100 個零。

我修正了你的代碼,試圖以一種非常直接的方式做同樣的事情。

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

#define SIZE 100

void fill_random_array( int array[const], size_t sz )
{
    srand(time(NULL));  
    for(int i = 0; i < sz; ++i)
    {
        array[i] =  rand() % 901;
        printf("%3d%s", array[i], (i+1)%16? " ": "\n" );
    }  
}

void sort( int array[const], size_t sz )
{
    for (int i = 0 ; i < SIZE; i++)
    {
        for (int j = 0; j < SIZE - 1; j++)
        {
            if (array[j] > array[j+1]) 
            {
                int temp   = array[j];
                array[j]   = array[j+1];
                array[j+1] = temp;
            }
        }
    }
}


int main()
{
    int data[SIZE];
    fill_random_array( data, SIZE );
    printf("\n\nResult:\n");
    sort( data, SIZE );
    
    for(int i = 0; i < SIZE; ++i)
    {
        printf("%3d%s", data[i], (i+1)%16? " ": "\n" );
    }  

    return 0;    
}

示例 Output:

Success #stdin #stdout 0s 5536KB
757 872 260 877 827 747 839 279 468 311 361 584 382 364 528  24
848  32 307 479 594  59 172  54 811 530 550 451 618 893  39 474
261 597 450 187 740 686 467 307 393 224 288 775 588 816 195 832
848 502 707 838 859 879 892 769 806 839 616 523 831 656  97 191
352 844 676 488 629 539 796 121 763 183 896 747 395 190  74 639
 89 781 873  47 759 865 212  60 199 828 584 129 880  77 618 628
 20 690 216 650 

Result:
 20  24  32  39  47  54  59  60  74  77  89  97 121 129 172 183
187 190 191 195 199 212 216 224 260 261 279 288 307 307 311 352
361 364 382 393 395 450 451 467 468 474 479 488 502 523 528 530
539 550 584 584 588 594 597 616 618 618 628 629 639 650 656 676
686 690 707 740 747 747 757 759 763 769 775 781 796 806 811 816
827 828 831 832 838 839 839 844 848 848 859 865 872 873 877 879
880 892 893 896 
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

#define SIZE 100
#define N 10

void fill_random_array( int array[SIZE],  size_t size )
{
int i= 0;
srand(time(NULL));  
for(i = 0; i < SIZE; ++i)
{
    array[i] =  rand() % 901;    
} 
for(i = 0; i < SIZE; i++) {
    printf("%4d", array[i]);
    if(i % 10 == 9)
        printf("\n");
}
}

void sort( int array[SIZE], size_t size )
{
int i = 0;
int j = 0;
for (i = 0 ; i < SIZE; i++)
{
    for (j = 0; j < SIZE - 1; j++)
    {
        if (array[j] > array[j+1]) 
        {
            int temp   = array[j];
            array[j]   = array[j+1];
            array[j+1] = temp;
        }
    }
}
for(i = 0; i < SIZE; i++) {
    printf("%4d", array[i]);
    if(i % 10 == 9)
        printf("\n");
     }   
}

void calculations(int array[SIZE], size_t size) {
//max och min

int i = 0;
int max = 0;
int min = 900;
int sum = 0;
double average = 0;
double median = 0;
for(i = 0; i < SIZE; i++){

    if(array[i] > max) {
        max = array[i];   
    }
    if(array[i] < min) {
        min = array[i];
    }    
}  
//average
for(i = 0; i < SIZE; i++) {
    sum += array[i];
}
average = sum / SIZE;
//median
median = (array[49] + array[50])/2;
printf("Max: %d  Min: %d  Average: %f  Median: %f\n", max, min, average, median);          
}

void binsearch(int array[SIZE], size_t size) {
//binary search

int i = 0;
int pos = 0; //position
int start = 0; //start position i vector
int end = 99; //end position i vector
int number = 0;
int flag;
int row = 0; //row
int col = 0; //column
printf("Enter number: \n");
scanf("%d", &number);
flag = 0;
while(start <= end) {
    
    pos = (start + end)/2;
    if(array[pos] == number){
        row = (pos / N) + 1;
        col = (pos % N) + 1;
        printf("The number %d was found in position %d and row %d and column     %d\n", number, pos, row, col);
         
        flag = 1;
        break;
    }
    else if(array[pos] < number)
        start = pos + 1;
    else if(array[pos] > number)
        end = pos - 1;
    
    break;
}
 if(flag == 0)
     printf("Number not found\n");     
}    

int main()
{
int data[SIZE];
fill_random_array( data, SIZE );
printf("\n");
sort( data, SIZE );
printf("\n");
calculations( data, SIZE);
printf("\n");
binsearch(data, SIZE);

return 0;    
}

這就是我的代碼現在的樣子。 我認為該程序現在可以正常運行,但是當我運行它時,它會生成一個隨機數序列,並對它進行排序,然后打印出最大最小值、平均值和中值。 但是當我為二進制搜索輸入一個數字時,它說:即使數字在排序序列中,也找不到數字,這是怎么回事? 我該如何解決?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM