If I input 10 numbers, I created a program that outputs the numbers I entered and sorted in ascending order, but it gets a little strange.

Asked 2 years ago, Updated 2 years ago, 37 views

As stated in the title, I would like to create a program that prints the 10 numbers you entered and the numbers you have sorted in ascending order, but the results are slightly different from the expected results.

I did the following program.

#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);
    for(i=0;i<NUMBER;i++){
        printf("%d:",i+1);
        scanf("%d", & point[i]);
    } 
    bsort(point, NUMBER);
    readIntArray (data, NUMBER);
    printIntArray(data, NUMBER);
   
    puts("Sorted in ascending order.");
    for (i=0; i<NUMBER;i++)
        printf("%d#:%d\n", i+1, point[i]);

    return 0;
}

Run Results

$./a.out
Please enter 10 pieces of data.
Number one: 10
Number 2: 9
Number 3: 8
Number 4: 7
Number 5: 6
Number 6: 5
Number 7: 4
Number 8: 3
Number 9: 2
Number 10: 1
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 one: 1
Number two: two.
Number three: three.
Number four: four.
Number five: five.
Number 6: 6
Number 7: 7
Number 8: 8
Number 9: 9
Number 10: 10

Expected results

$./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 one: 1
Number two: two.
Number three: three.
Number four: four.
Number five: five.
Number 6: 6
Number 7: 7
Number 8: 8
Number 9: 9
Number 10: 10

I could only create a program that would ask me the same thing twice, so I tried many other programs, but I didn't know how to program the correct compilation result because the ascending order result was strange or the data before the ascending order processing was not displayed properly.
What should I do?I look forward to hearing from you.

c

2022-09-30 19:17

1 Answers

Tip: readIntArray() is a function that asks users to enter.
Answer: What happens if I call it after bsort()?

The first step for tomorrow: Most development environments have something called a debugger. For Visual Studio, the built-in feature is , gdb.Once the debugger is available, you can track how the program behaves with your hands, increasing debugging efficiency.Please remember.You can find out the cause while you ask the bulletin board site and wait for the answer.


2022-09-30 19:17

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.