Home > Enterprise >  In C, I'm having trouble getting an Index sorted High to Low and vice versa
In C, I'm having trouble getting an Index sorted High to Low and vice versa

Time:04-27

I'm working on a project involving a preset array: primaryArray[8] = {8, 4, 2, 16, 32, 124, 64, 256}; Im calling a function before the main, its not quite working. Here is the code.

    void lowhighSort(int primaryArray[], int arrayLength) {
    int i;
    int a;
    int indexMin;

    for (i = 0; i < arrayLength - 1;   i){
        indexMin = i;

        for (a = (i   1)   1; a < arrayLength;   a) {
            if (primaryArray[a] < primaryArray[indexMin]){
                indexMin = a;
            }
                simpleSwap(&primaryArray[indexMin], &primaryArray[i]);
        }
    }
}

    void highlowSort(int primaryArray[], int arrayLength) {
        int i;
    int a;
    int indexMin;

    for (i = 0; i < arrayLength - 1;   i){
        indexMin = i;
        for (a = i   1; a < arrayLength;   a) {
            if(primaryArray[a] > primaryArray[indexMin])
                indexMin = a;
                simpleSwap(&primaryArray[indexMin], &primaryArray[i]);
        }
    }
}

My "simpleSwap" might be the mistake, when learning I didn't quite understand entirely what the * did. Regardless, here it is as well:

    void simpleSwap(int* x, int* y) {
    int temp = *x;

    *x = *y;
    *y = temp;
} 

*edit This is part of a "larger" code so I'm not entirely sure if perhaps the error is elsewhere.

CodePudding user response:

#include <stdio.h>
#include <stdlib.h>
void simpleSwap(double* x, double* y) {
    double temp = *x;

    *x = *y;
    *y = temp;
}

double largest(double Array[], double Length) {
    double largestNum = Array[0];

    for (int i = 1; i < Length;   i) {
        if(largestNum < Array[i]) {
            largestNum = Array[i];
        }
    }
    return largestNum;
}

double smallest(double Array[], double Length) {
    double smallestNum = Array[0];

    for (int i = 1; i < Length;   i) {
        if(smallestNum > Array[i]) {
            smallestNum = Array[i];
        }
    }
    return smallestNum;
}
void lowhighSort(double Array[], double Length) {

    int a;
    int indexMin;

    for (int i = 0; i < Length;   i){
        indexMin = i;

        for (a = (i   1); a < Length;   a) {
            if (Array[a] < Array[indexMin]){
                indexMin = a;

                simpleSwap(&Array[indexMin], &Array[i]);
            }
        }
    }
}

void highlowSort(double Array[], double Length) {

    int a;
    int indexMin;

    for (int i = 0; i < Length;   i){
        indexMin = i;

        for (a = (i   1); a < Length;   a) {
            if(Array[a] > Array[indexMin]){
                indexMin = a;
                simpleSwap(&Array[indexMin], &Array[i]);
            }
        }
    }
}

float arrayAverage (double Array[], double Length) {
    int i;
    double arrayAverage = 0;
    for (i = 0; i < Array[i];   i) {
        arrayAverage = Array[i]   arrayAverage;
    }
    return arrayAverage;
}


int main()
{

    double primaryArray[8] = {8, 4, 2, 16, 32, 124, 64, 256};
    double arrayLength = sizeof(primaryArray)/sizeof(primaryArray[0]);
    double ancillaryArray[10] = {8, 4, 2, 16, 32, 124, 64, 256};

    printf("Primary Array:\n");

    printf("\nLargest Array Element: %.1lf\n", largest(primaryArray, arrayLength));

    printf("Smallest Array Element: %.1lf\n\n", smallest(primaryArray, arrayLength));

    lowhighSort(primaryArray, arrayLength);
    printf("Array Ascending Order: ");
    for(int i = 0; i < 8;   i){
        printf("%.1lf, ", primaryArray[i]);
    }
    printf("\n");

    highlowSort(primaryArray, arrayLength);
    printf("Array Descending Order: ");
    for(int i = 0; i < 8;   i){
        printf("%.1lf, ", primaryArray[i]);
    }
    printf("\n");
    printf("\nArray Element Average: %.3lf", (arrayAverage(primaryArray, arrayLength))/8);


    int ancillaryarrayLength = sizeof(ancillaryArray)/sizeof(ancillaryArray[0]);

    printf("\n\nPlease Finish Ancillary Array (Max 10 Elements)\nCurrent Elements: ");
    for(int i = 0; i < arrayLength;   i) {
        printf("%.1lf, ", ancillaryArray[i]);
    }

    printf("\nEnter Elements: ");
    for (int i = 8; i < 10;   i) {
        scanf("%lf", &ancillaryArray[i]);
    }


    printf("\nLargest Array Element: %.1lf\n", largest(ancillaryArray, ancillaryarrayLength));

    printf("Smallest Array Element: %.03lf\n", smallest(ancillaryArray, ancillaryarrayLength));

    lowhighSort(ancillaryArray, ancillaryarrayLength);
    printf("Array Ascending Order: ");
    for(int i = 0; i < 10;   i){
        printf("%.3lf, ", ancillaryArray[i]);
    }
    printf("\n");

    highlowSort(ancillaryArray, ancillaryarrayLength);
    printf("Array Descending Order: ");
    for(int i = 0; i < 10;   i){
        printf("%.3lf, ", ancillaryArray[i]);
    }
    printf("\n");

    printf("Array Element Average: %.4lf", (arrayAverage(ancillaryArray, ancillaryarrayLength))/10);


    return 0;
}

Here is the final code, it works as intended! Thank you all for your help.

  • Related