And the ascending order turns out to be crazy.

Asked 2 years ago, Updated 2 years ago, 39 views

I created a program where 10 numbers were entered and the result of the ascending order was output, but the result of the ascending order was not asked the same thing twice as before, but the result of the ascending order was strange.
I programmed as follows.

#include<stdio.h>
#define NUMBER10

void swap (int*px, int*py)
{
    int temp=*px;
    *px = *py;
    *py=temp;
}

void readIntArray(inta[], int size)
{
    inti;
    for(i=0;i<size;i=i+1){
        printf("%dth?", i+1);
        scanf("%d", & a[i]);
    }
}

void printIntArray(inta[], int size)
{
    inti;
    for(i=0;i<size;i=i+1){
        printf("%d", a[i]);
    }
    printf("\n");
}

void bsort (inta[], intn)
{
    inti,j;
    for (i=0; i<n-1;i++)
        for (j=n-1; j>i;j--)
            if(a[j-1]>a[j])
                swap(&a[j], &a[j-1]);
}

int main (void)
{
    inti;
    int data [i], point [NUMBER];

    printf("Please enter %d data.\n", NUMBER);

    readIntArray (data, NUMBER);
    printIntArray(data, NUMBER);

    puts("Sorted in ascending order.");
    bsort(point, NUMBER);
    for (i=0; i<NUMBER;i++)
        printf("%d#:%d\n", i+1, point[i]);
    return 0;

}

$./a.out
Please enter 10 pieces of data.
Number one? 10
2nd? 9
Third? Eight.
4th? 7th.
Number five? Number six.
6th? 5th?
7th? 4th?
Number eight? Number three.
9th? 2nd.
Number 10? One.
10 9 8 7 6 5 4 3 2 1 
Sorted in ascending order.
Number 1: -1386554768
Number 2: -1386554691
Number 3: 0
Number 4: 0
Number 5: 0
Number 6: 1
Number 7: 21950
Number 8: 21950
Number 9: 32578
Number 10: 151521696

Which part is wrong?I look forward to hearing from you.

c

2022-09-30 16:38

2 Answers

The first step for tomorrow: Learn how to use debugger.
Step 2: Raise the compilation option Warning Level.A warning is issued when the compiler determines that it is a little strange.Development efficiency increases when you can understand and correct why it is a warning without ignoring it.

Answer:
data
can only be entered Sort and display point


2022-09-30 16:38

int data [i], point [NUMBER];

Why don't you think about what i contains at this time?
Local variables contain random values


2022-09-30 16:38

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.