出5道C语言编程题 ,关于一维数组,并给出程序和注释
时间: 2024-03-10 11:50:31 浏览: 68
好的,我可以为您提供五道关于一维数组的 C 语言编程题,并附上程序和注释。希望能帮到您。
1. 题目:输入一个一维数组,将数组元素逆序输出。
```c
#include <stdio.h>
#define MAX_SIZE 100
int main()
{
int arr[MAX_SIZE], n, i, temp;
printf("Enter the size of array: ");
scanf("%d", &n);
printf("Enter the elements of array: ");
for(i=0; i<n; i++)
scanf("%d", &arr[i]);
// Reverse the array
for(i=0; i<n/2; i++)
{
temp = arr[i];
arr[i] = arr[n-i-1];
arr[n-i-1] = temp;
}
printf("The reversed array is: ");
for(i=0; i<n; i++)
printf("%d ", arr[i]);
return 0;
}
```
2. 题目:输入一个一维数组,计算数组元素的平均值并输出。
```c
#include <stdio.h>
#define MAX_SIZE 100
int main()
{
int arr[MAX_SIZE], n, i, sum=0;
float avg;
printf("Enter the size of array: ");
scanf("%d", &n);
printf("Enter the elements of array: ");
for(i=0; i<n; i++)
{
scanf("%d", &arr[i]);
sum += arr[i];
}
avg = (float)sum / n;
printf("The average of array is: %.2f", avg);
return 0;
}
```
3. 题目:输入一个一维数组,查找数组中最大的元素并输出。
```c
#include <stdio.h>
#define MAX_SIZE 100
int main()
{
int arr[MAX_SIZE], n, i, max;
printf("Enter the size of array: ");
scanf("%d", &n);
printf("Enter the elements of array: ");
for(i=0; i<n; i++)
scanf("%d", &arr[i]);
max = arr[0];
for(i=1; i<n; i++)
{
if(arr[i] > max)
max = arr[i];
}
printf("The maximum element in array is: %d", max);
return 0;
}
```
4. 题目:输入一个一维数组,将数组中的元素按照从小到大的顺序进行排序并输出。
```c
#include <stdio.h>
#define MAX_SIZE 100
int main()
{
int arr[MAX_SIZE], n, i, j, temp;
printf("Enter the size of array: ");
scanf("%d", &n);
printf("Enter the elements of array: ");
for(i=0; i<n; i++)
scanf("%d", &arr[i]);
// Sort the array in ascending order
for(i=0; i<n-1; i++)
{
for(j=0; j<n-i-1; j++)
{
if(arr[j] > arr[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
printf("The sorted array in ascending order is: ");
for(i=0; i<n; i++)
printf("%d ", arr[i]);
return 0;
}
```
5. 题目:输入一个一维数组和一个元素,查找该元素在数组中的位置并输出。
```c
#include <stdio.h>
#define MAX_SIZE 100
int main()
{
int arr[MAX_SIZE], n, i, search, flag=0;
printf("Enter the size of array: ");
scanf("%d", &n);
printf("Enter the elements of array: ");
for(i=0; i<n; i++)
scanf("%d", &arr[i]);
printf("Enter the element to search: ");
scanf("%d", &search);
// Search the element in array
for(i=0; i<n; i++)
{
if(arr[i] == search)
{
printf("%d found at location %d.\n", search, i+1);
flag = 1;
break;
}
}
if(flag == 0)
printf("%d is not found in the array.", search);
return 0;
}
```
希望这些题目和程序对您有所帮助。如果您有任何问题,请随时向我提问。
阅读全文