编程题: 一维数组a中的元素为:1,4,2,7,3,12,5,34,5,9。 下面程序的功能是:求一维数组a中的最大元素及其下标。 程序的输出应为:The max is: 34,position is: 7。 请写出fun( )函数,使程序完整并能够正确运行。 #include"stdio.h" int max; /**********fun( )函数**********/ 请编写完成! /**********fun( )函数**********/ void main() { int a[10]={1,4,2,7,3,12,5,34,5,9},n; n= fun(a,10); printf("The max is: %d ,position is: %d\n", max , n);}
时间: 2023-07-18 10:51:50 浏览: 29
int fun(int a[], int n)
{
int max_index = 0;
max = a[0];
for (int i = 1; i < n; i++) {
if (a[i] > max) {
max = a[i];
max_index = i;
}
}
return max_index;
}
相关问题
编程题: 一维数组a中的元素为:1,4,2,7,3,12,5,34,5,9。 下面程序的功能是:求一维数组a中的最大元素及其下标。 程序的输出应为:The max is: 34,position is: 7。
这是一道比较简单的数组题目,只需要遍历一遍数组,找到最大值以及对应的下标即可。
下面是代码实现:
```c
#include <stdio.h>
int main()
{
int a[10] = {1, 4, 2, 7, 3, 12, 5, 34, 5, 9};
int max = a[0];
int max_index = 0;
for (int i = 1; i < 10; i++) {
if (a[i] > max) {
max = a[i];
max_index = i;
}
}
printf("The max is: %d, position is: %d\n", max, max_index);
return 0;
}
```
输出结果为:
```
The max is: 34, position is: 7
```
出5道C语言编程题 ,关于一维数组,并给出程序和注释
好的,我可以为您提供五道关于一维数组的 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;
}
```
希望这些题目和程序对您有所帮助。如果您有任何问题,请随时向我提问。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)