如何打印最大值和最小值的索引?
How do I print the index of the maximum and minimum values?
以下代码打印最大值和最小值。我如何着手打印这些值的索引而不是值本身?
#include <stdio.h>
int main()
{
int arr1[100];
int i, mx, mn, n;
printf("Input the number of elements to be stored in the array :");
scanf("%d",&n);
printf("Input %d elements in the array :\n",n);
for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&arr1[i]);
}
mx = arr1[0];
mn = arr1[0];
for(i=1; i<n; i++)
{
if(arr1[i]>mx)
{
mx = arr1[i];
}
if(arr1[i]<mn)
{
mn = arr1[i];
}
}
printf("Maximum element is : %d\n", mx);
printf("Minimum element is : %d\n\n", mn);
return 0;
}
分别更新 mx
时只需保存索引 mn
:
int max_index = 0;
int min_index = 0;
// ...
if(arr1[i]>mx)
{
mx = arr1[i];
max_index = i;
}
if(arr1[i]<mn)
{
mn = arr1[i];
min_index = i;
}
作为上一个答案的替代方案,您可以使用指针和指针算法:
#include <stdio.h>
int main()
{
int arr1[100];
int i, n;
int *mn, *mx;
printf("Input the number of elements to be stored in the array :");
scanf("%d", &n);
printf("Input %d elements in the array :\n",n);
for(i = 0; i < n; i++)
{
printf("element - %d : ", i);
scanf("%d", &arr1[i]);
}
mx = &arr1[0];
mn = &arr1[0];
for(i = 1; i < n; i++)
{
if(arr1[i] > *mx)
mx = &arr1[i];
if(arr1[i] < *mn)
mn = &arr1[i];
}
printf("Maximum element is : %d (%d)\n", *mx, mx - arr1);
printf("Minimum element is : %d (%d)\n\n", *mn, mn - arr1);
return 0;
}
示例:
$ gcc main.c -o main.exe; ./main.exe;
Input the number of elements to be stored in the array :5
Input 5 elements in the array :
element - 0 : 3
element - 1 : 2
element - 2 : 1
element - 3 : 5
element - 4 : 4
Maximum element is : 5 (3)
Minimum element is : 1 (2)
更改代码以跟踪最小和最大元素的索引并不难。
只需在变量 mx
和 mn
中存储相应的最小和最大元素的索引,而不是它们的值,例如。
mx = 0;
mn = 0;
for ( i = 1; i < n; i++ )
{
if ( arr1[i] > arr1[mx] )
{
mx = i;
}
else if ( arr1[i] < mn )
{
mn = i;
}
}
但我想指出的是,请始终尝试编写更通用的代码。
您可以编写一个单独的函数,returns 一对最大和最小元素的索引。
给你。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct Pair { size_t min; size_t max; }
minmax_element( const int a[], size_t n )
{
struct Pair minmax = { .min = 0, .max = 0 };
for ( size_t i = 1; i < n; i++ )
{
if ( a[i] < a[minmax.min] )
{
minmax.min = i;
}
else if ( a[minmax.max] < a[i] )
{
minmax.max = i;
}
}
return minmax;
}
int main(void)
{
size_t n = 1;
printf( "Input the number of elements to be stored in the array: " );
scanf( "%zu", &n );
int a[n];
srand( ( unsigned int )time( NULL ) );
for ( size_t i = 0; i < n; i++ )
{
a[i] = rand() % ( int )n;
}
for ( size_t i = 0; i < n; i++ )
{
printf( "%d ", a[i] );
}
putchar( '\n' );
struct Pair minmax = minmax_element( a, n );
printf( "The minimum value is %d at position %zu\n", a[minmax.min], minmax.min );
printf( "The maximum value is %d at position %zu\n", a[minmax.max], minmax.max );
return 0;
}
程序输出可能类似于
Input the number of elements to be stored in the array: 10
7 1 7 3 1 7 8 5 0 3
The minimum value is 0 at position 8
The maximum value is 8 at position 6
或者,该函数可以定义两个附加参数:指向最小元素索引的指针和指向最大元素索引的指针。
给你。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void minmax_element( const int a[], size_t n, size_t *min, size_t *max )
{
*min = 0;
*max = 0;
for ( size_t i = 1; i < n; i++ )
{
if ( a[i] < a[*min] )
{
*min = i;
}
else if ( a[*max] < a[i] )
{
*max = i;
}
}
}
int main(void)
{
size_t n = 1;
printf( "Input the number of elements to be stored in the array: " );
scanf( "%zu", &n );
int a[n];
srand( ( unsigned int )time( NULL ) );
for ( size_t i = 0; i < n; i++ )
{
a[i] = rand() % ( int )n;
}
for ( size_t i = 0; i < n; i++ )
{
printf( "%d ", a[i] );
}
putchar( '\n' );
size_t min, max;
minmax_element( a, n, &min, &max );
printf( "The minimum value is %d at position %zu\n", a[min], min );
printf( "The maximum value is %d at position %zu\n", a[max], max );
return 0;
}
程序输出可能看起来像
Input the number of elements to be stored in the array: 10
2 0 4 2 4 3 0 9 1 0
The minimum value is 0 at position 1
The maximum value is 9 at position 7
以下代码打印最大值和最小值。我如何着手打印这些值的索引而不是值本身?
#include <stdio.h>
int main()
{
int arr1[100];
int i, mx, mn, n;
printf("Input the number of elements to be stored in the array :");
scanf("%d",&n);
printf("Input %d elements in the array :\n",n);
for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&arr1[i]);
}
mx = arr1[0];
mn = arr1[0];
for(i=1; i<n; i++)
{
if(arr1[i]>mx)
{
mx = arr1[i];
}
if(arr1[i]<mn)
{
mn = arr1[i];
}
}
printf("Maximum element is : %d\n", mx);
printf("Minimum element is : %d\n\n", mn);
return 0;
}
分别更新 mx
时只需保存索引 mn
:
int max_index = 0;
int min_index = 0;
// ...
if(arr1[i]>mx)
{
mx = arr1[i];
max_index = i;
}
if(arr1[i]<mn)
{
mn = arr1[i];
min_index = i;
}
作为上一个答案的替代方案,您可以使用指针和指针算法:
#include <stdio.h>
int main()
{
int arr1[100];
int i, n;
int *mn, *mx;
printf("Input the number of elements to be stored in the array :");
scanf("%d", &n);
printf("Input %d elements in the array :\n",n);
for(i = 0; i < n; i++)
{
printf("element - %d : ", i);
scanf("%d", &arr1[i]);
}
mx = &arr1[0];
mn = &arr1[0];
for(i = 1; i < n; i++)
{
if(arr1[i] > *mx)
mx = &arr1[i];
if(arr1[i] < *mn)
mn = &arr1[i];
}
printf("Maximum element is : %d (%d)\n", *mx, mx - arr1);
printf("Minimum element is : %d (%d)\n\n", *mn, mn - arr1);
return 0;
}
示例:
$ gcc main.c -o main.exe; ./main.exe; Input the number of elements to be stored in the array :5 Input 5 elements in the array : element - 0 : 3 element - 1 : 2 element - 2 : 1 element - 3 : 5 element - 4 : 4 Maximum element is : 5 (3) Minimum element is : 1 (2)
更改代码以跟踪最小和最大元素的索引并不难。
只需在变量 mx
和 mn
中存储相应的最小和最大元素的索引,而不是它们的值,例如。
mx = 0;
mn = 0;
for ( i = 1; i < n; i++ )
{
if ( arr1[i] > arr1[mx] )
{
mx = i;
}
else if ( arr1[i] < mn )
{
mn = i;
}
}
但我想指出的是,请始终尝试编写更通用的代码。
您可以编写一个单独的函数,returns 一对最大和最小元素的索引。
给你。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct Pair { size_t min; size_t max; }
minmax_element( const int a[], size_t n )
{
struct Pair minmax = { .min = 0, .max = 0 };
for ( size_t i = 1; i < n; i++ )
{
if ( a[i] < a[minmax.min] )
{
minmax.min = i;
}
else if ( a[minmax.max] < a[i] )
{
minmax.max = i;
}
}
return minmax;
}
int main(void)
{
size_t n = 1;
printf( "Input the number of elements to be stored in the array: " );
scanf( "%zu", &n );
int a[n];
srand( ( unsigned int )time( NULL ) );
for ( size_t i = 0; i < n; i++ )
{
a[i] = rand() % ( int )n;
}
for ( size_t i = 0; i < n; i++ )
{
printf( "%d ", a[i] );
}
putchar( '\n' );
struct Pair minmax = minmax_element( a, n );
printf( "The minimum value is %d at position %zu\n", a[minmax.min], minmax.min );
printf( "The maximum value is %d at position %zu\n", a[minmax.max], minmax.max );
return 0;
}
程序输出可能类似于
Input the number of elements to be stored in the array: 10
7 1 7 3 1 7 8 5 0 3
The minimum value is 0 at position 8
The maximum value is 8 at position 6
或者,该函数可以定义两个附加参数:指向最小元素索引的指针和指向最大元素索引的指针。
给你。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void minmax_element( const int a[], size_t n, size_t *min, size_t *max )
{
*min = 0;
*max = 0;
for ( size_t i = 1; i < n; i++ )
{
if ( a[i] < a[*min] )
{
*min = i;
}
else if ( a[*max] < a[i] )
{
*max = i;
}
}
}
int main(void)
{
size_t n = 1;
printf( "Input the number of elements to be stored in the array: " );
scanf( "%zu", &n );
int a[n];
srand( ( unsigned int )time( NULL ) );
for ( size_t i = 0; i < n; i++ )
{
a[i] = rand() % ( int )n;
}
for ( size_t i = 0; i < n; i++ )
{
printf( "%d ", a[i] );
}
putchar( '\n' );
size_t min, max;
minmax_element( a, n, &min, &max );
printf( "The minimum value is %d at position %zu\n", a[min], min );
printf( "The maximum value is %d at position %zu\n", a[max], max );
return 0;
}
程序输出可能看起来像
Input the number of elements to be stored in the array: 10
2 0 4 2 4 3 0 9 1 0
The minimum value is 0 at position 1
The maximum value is 9 at position 7