首页 > 解决方案 > 如何检查C中是否在数组中提供了任何数据?

问题描述

我正在制作一个程序,通过C中的冒泡排序算法对整数进行升序和降序排序。所以,我首先提供20个整数(固定)的随机数据,然后决定以哪种方式对其进行排序,这基本上是通过简单的菜单系统,例如:

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

*如果用户在没有先获取随机数据的情况下尝试排序,我想打印一条消息“未提供数据”。

下面的代码:

#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]);
    }
}

标签: c

解决方案


使用布尔标志来指示选项 A 是否被调用怎么样?用选项 A初始化该标志false并将其设置true。然后在选项 B 和 C 中,测试标志是否为true。如果false,投诉。

向我们展示您目前拥有的代码会很有帮助。


推荐阅读