请用C语言完成,输入 100 以内的 20 个正整数到数组中和一个需要查找的正整数 x,设计 5 个函数:(1)getdata 函 数完成输入数据输入(需要处理输入<=0 和>100 的数据);(2)最大值的函数getmax,返回最大值; (3)排序函数 sort,对 20 个数进行从小到大排序。(4)查找函数find 在 20 个数中查找 x,找到返 回数组中的下标,没有找到返回-1;(5)输出函数printdata,要求每行输出 5 个正整数,每个正整数 占 4 位,分 4 行输出。在 main 函数中定义数组,以菜单的形式调用 5 个函数,完成数据的输入,求最大值,排序,查找 x 和输出数据
时间: 2024-04-07 20:33:04 浏览: 84
以下是用C语言实现的代码,实现了输入数据、最大值、排序、查找和输出数据的功能,并使用菜单形式调用这些函数:
```c
#include <stdio.h>
#define SIZE 20
void getdata(int arr[]);
int getmax(int arr[]);
void sort(int arr[]);
int find(int arr[], int x);
void printdata(int arr[]);
void displayMenu();
int main() {
int arr[SIZE];
int x, max, index;
int choice;
do {
displayMenu();
scanf("%d", &choice);
switch (choice) {
case 1:
getdata(arr);
break;
case 2:
max = getmax(arr);
printf("Max value: %d\n", max);
break;
case 3:
sort(arr);
printf("Array sorted.\n");
break;
case 4:
printf("Enter the number to search: ");
scanf("%d", &x);
index = find(arr, x);
if (index == -1) {
printf("%d not found in the array.\n", x);
} else {
printf("%d found at index %d in the array.\n", x, index);
}
break;
case 5:
printdata(arr);
break;
case 0:
printf("Exiting program.\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 0);
return 0;
}
void getdata(int arr[]) {
printf("Enter 20 positive integers less than or equal to 100:\n");
for (int i = 0; i < SIZE; i++) {
scanf("%d", &arr[i]);
// Check for valid input
while (arr[i] <= 0 || arr[i] > 100) {
printf("Invalid input! Enter a positive integer <= 100: ");
scanf("%d", &arr[i]);
}
}
}
int getmax(int arr[]) {
int max = arr[0];
for (int i = 1; i < SIZE; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}
void sort(int arr[]) {
int temp;
for (int i = 0; i < SIZE - 1; i++) {
for (int j = 0; j < SIZE - i - 1; j++) {
if (arr[j] > arr[j+1]) {
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
int find(int arr[], int x) {
for (int i = 0; i < SIZE; i++) {
if (arr[i] == x) {
return i;
}
}
return -1;
}
void printdata(int arr[]) {
printf("Data in sorted order:\n");
for (int i = 0; i < SIZE; i++) {
printf("%4d ", arr[i]);
if ((i+1) % 5 == 0) {
printf("\n");
}
}
}
void displayMenu() {
printf("\nMenu:\n");
printf("1. Enter data\n");
printf("2. Find maximum value\n");
printf("3. Sort array\n");
printf("4. Find number\n");
printf("5. Print data\n");
printf("0. Exit\n");
printf("Enter your choice: ");
}
```
这段代码实现了一个菜单系统,通过输入不同的选项来调用相应的函数,完成数据的输入、求最大值、排序、查找指定数和输出数据的功能。用户可以根据菜单选择不同的操作。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)