lundi 5 juillet 2021

I want to know why this don't work the same(function definition outside-main and if inside-main function)

#include <stdio.h>

int generator(int n) {
    int ten[6]; // array that saves digit number
    int result = n; 
    for (int i = 0; i < 6; i++) {

        ten[i] = n % 10; //saving digit number
        n = n / 10;
        result += ten[i];
    }
    return result;
 }
void sort(int x, int y) { //the problem function. it didn't work
if (x > y) {
    int temp;
    temp = y;
    y = x;
    x = temp;
}


int main(void) {
    int number[10000];
    int selfNumber[10000];
    int result[10000];


    for (int i = 0; i < 100; i++) {
        number[i] = generator(i+1); //generator function
    }
    for (int i = 0; i < 100; i++) {
        printf("%d\n", number[i]);
    }
    for (int i = 0; i < 100; i++) {
        if (number[i] > number[i + 1]) { //it worked.
            int temp;
            temp = number[i + 1];
            number[i+1] = number[i];
            number[i]=temp;
        }
        sort(number[i], number[i + 1]);
     }
    for (int i = 0; i < 100; i++) {
        printf("%d\n", number[i]);
    }
    return 0;

I defined the void function sort to sort the numbers in size. But it didn't work. So, I used if inside of main function to sort the numbers in size. I don't know why it does work it different.

Aucun commentaire:

Enregistrer un commentaire