-1

I am making a program that sorts integers in ascending and descending order through bubble sort algorithm in C. So, I am providing a random data of 20 integers(fixed)first and then deciding in which manner to sort it, which is basically done through simple menu system which is like:

A. provide random data
B. Sort high to low
C. Sort low to high

*I want to print a message "Data not provided" if the user tries to sort without getting the random data first.

Code below:

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

void random_number_list(int array[]);
void low_to_high(int array[]);
void high_to_low(int array[]);
void display(int array[]);

int main(void)
{
    int original_array[21];
    char selection;

    
    do 
    {
        puts("----> Please make your selection from the following:\n\n"
       " A.Define Random Number List\n"
       " B.Sort Number List(High to Low)\n"
       " C.Sort Number List(Low to High)\n"
       " D.Exit ");

        printf_s(" \nYour selection: ");
        scanf_s("\n%c", &selection);
        
        if (selection == 'A' || selection == 'a')
        {
            
             random_number_list(original_array);
        }
        else if (selection == 'B' || selection == 'b')
        {
                high_to_low(original_array);
        }
        else if (selection == 'C' || selection == 'c')
        {
                low_to_high(original_array);
        }
        else if (selection == 'D' || selection == 'd')
        {
            puts("\nThank you for using the application.\n");
        
            return 0;
        }
        else
        {
            puts("\nSorry, input not understood. Please try again.\n");
        }
    } 
    while (selection != 'D');
}

void random_number_list(int array[])
{
    srand(time(NULL));

    printf("\n\nThe Random Data: ");

    for (size_t i = 0; i < 20; i++)
    {
        array[i] = 1 + rand() % 100;
        printf_s("%d,", array[i]);
        
    }
    puts("\n\n");

}

void low_to_high(int array[])
{
    int i, j, temp;

        for (i = 0;i < 20 - 1;i++)
        {
            for (j = i + 1;j < 20;j++)
            {
                if (array[i] > array[j])
                {
                    temp = array[i];
                    array[i] = array[j];
                    array[j] = temp;

                    display(array);
                    printf("\n");
                }
            }
        }

        printf_s("\nSorted Data : ");
        
        display(array);
        puts("\n");
}

void high_to_low(int array[])
{
    int i, j, temp;
    
        for (i = 0; i < 20 - 1; i++)
        {
            for (j = i + 1; j < 20; j++)
            {
                if (array[i] < array[j])
                {
                    temp = array[i];
                    array[i] = array[j];
                    array[j] = temp;

                    display(array);
                    printf("\n");
                }
            }
        }

        printf_s("\nSorted Data : ");

        display(array);
        puts("\n");
}

void display(int array[])
{
    for (size_t i = 0; i < 20; i++)
    {
        printf("%d,", array[i]);
    }
}
Jens
  • 69,818
  • 15
  • 125
  • 179
  • What do you mean by random data here ? And please provide minimal code what you have tried till now. – S M Vaidhyanathan Nov 04 '20 at 14:29
  • 2
    Show your existing (even incomplete or wrong) code. Otherwise we can't help other than tell you: you need to write the code that does what you want. – Jabberwocky Nov 04 '20 at 14:30
  • @SMVaidhyanathan i am providing random data by srand(time(NULL)) – Deep Patel Nov 04 '20 at 14:35
  • OT: your usage of `srand` is wrong: read this: https://stackoverflow.com/questions/7343833/srand-why-call-it-only-once – Jabberwocky Nov 04 '20 at 14:38
  • @Jabberwocky i know, the compiler is already giving a warning but my concern is that data being provided or not and if not then notify that data is not provided – Deep Patel Nov 04 '20 at 14:43
  • @DeepPatel yes, that's why I wrote __OT__ meaning "off topic". An off topic comment is a kind of bonus comment which points out a different problem than the one being asked about. – Jabberwocky Nov 04 '20 at 14:48

3 Answers3

3

What about using a boolean flag indicating whether option A was called? Initialize that flag with false and set it true in option A. Then in options B and C, test if the flag is true. If false, complain.

Showing us the code you have so far would be helpful.

Jens
  • 69,818
  • 15
  • 125
  • 179
  • @DeepPatel Wonderful! The next step on Stackoverflow then is to accept the answer that was most helpful. (And maybe upvote all answers that helped you, which can be more than one). – Jens Nov 04 '20 at 15:15
  • I did but stackoverflow says it will not display my upvotes as I dont have enough reputation . sorry – Deep Patel Nov 04 '20 at 16:28
0

I would include stdbool.h and create a boolean variable in the main function called something like is_generated set to false by default and set it to true when the option A is entered. Then you can check it instead of checking if original_array is equal to zero.

ayb
  • 1
  • 1
-1

You can use the sizeof operator and divide the result by the size of an element.

size_t n = sizeof your_array

Jens
  • 69,818
  • 15
  • 125
  • 179
Mel
  • 625
  • 9
  • 25