编程题: 一维数组a中的元素为:1,4,2,7,3,12,5,34,5,9。 下面程序的功能是:求一维数组a中的最大元素及其下标。 程序的输出应为:The max is: 34,position is: 7。
时间: 2023-09-24 10:11:48 浏览: 80
这是一道比较简单的数组题目,只需要遍历一遍数组,找到最大值以及对应的下标即可。
下面是代码实现:
```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;
}
```
希望这些题目和程序对您有所帮助。如果您有任何问题,请随时向我提问。
5.编程题:合并两个已经按照升序排列的一维数组。例如合并前数组a[10]={1,3,5,7,9},b[5]={2,4,8,16,32},合并后a[10]={1, 2,3, 4,5,7,8,9,16,3
题目描述:
给定两个已经按照升序排列的一维数组a和b,假设数组a有n个元素,数组b有m个元素,请将它们合并成一个按升序排列的数组。
示例:
输入:
a = [1,3,5,7,9]
b = [2,4,8,16,32]
输出:
merged = [1,2,3,4,5,7,8,9,16,32]
解决方案:
一个简单的思路是利用归并排序的合并过程来实现。具体步骤如下:
1. 定义一个新的数组,长度为两个数组长度之和。
2. 使用两个指针i和j分别指向数组a和b的第一个元素。
3. 依次比较i和j指向的元素大小,将较小的元素添加到新的数组中,并将指针向后移动一位。
4. 当其中一个指针指向数组末尾时,将另一个数组中剩余的元素添加到新的数组中。
5. 返回新的数组。
代码如下所示:
```
def merge_sorted_arrays(a, b):
n = len(a)
m = len(b)
merged = * (n + m)
i = j = k = 0
while i < n and j < m:
if a[i] <= b[j]:
merged[k] = a[i]
i += 1
else:
merged[k] = b[j]
j += 1
k += 1
while i < n:
merged[k] = a[i]
i += 1
k += 1
while j < m:
merged[k] = b[j]
j += 1
k += 1
return merged
```