[英]Function to return a pointer to the largest number in an array
as you can tell by the title I need to write a function that returns a pointer to the largest number in an array, the functions gets a pointer to a double array and it's size. 如标题所示,我需要编写一个返回指向数组中最大数字的指针的函数,该函数获取指向double数组及其大小的指针。 In addition I need to write a main function that will use this function. 另外,我需要编写一个将使用此功能的主要功能。 Here is the code that I wrote: 这是我编写的代码:
#include <stdio.h>
#include <stdlib.h>
void BigEl(double* arr, double arrSize);
void BigEl(double* arr, double arrSize)
{
int i;
double maximum, *x;
maximum = arr[0];
for (i = 1; i < arrSize; i++)
{
if (arr[i]>maximum)
{
maximum = arr[i];
}
}
*x = maximum;
}
void main()
{
double myarr[10];
int i;
printf("Please insert 10 numbers to the array\n");
for (i = 0; i < 10; i++)
{
scanf("%d", &myarr[i]);
}
BigEl(myarr, 10);
}
I get this error: 我收到此错误:
Error 1 error C4700: uninitialized local variable 'x' used
I don't understand what I did wrong because I did initialized x. 我不明白我做错了什么,因为我初始化了x。 Any kind of help is appreciated, in addition, tell me if the idea of my code was right because Im not sure if I understood the question correctly. 此外,请提供任何帮助,告诉我代码的想法是否正确,因为我不确定我是否正确理解了问题。
You did not initialize the variable x
. 您没有初始化变量x
。 You merely wrote to the the location pointed to by x
, here: 您仅在以下位置写到x
指向的位置:
*x = maximum;
when x
was uninitialized, which is what the compiler is complaining about. 当x
未被初始化时,这就是编译器所抱怨的。
You want something like: 您想要类似的东西:
double *
BigEl(double* arr, size_t arrSize)
{
size_t i;
double *max = arr;
for (i = 1; i < arrSize; i++)
if (arr[i] > *max)
max = &arr[i];
return max;
}
Things I've changed: 我已经改变的事情:
size_t
for the array size and the counter, not a double
and an int
. 使用size_t
作为数组大小和计数器,而不是double
和int
。 You're not returning anything. 你什么也没退。 Also, it might be good to take into consideration the case when the array size is 0. Moreover, the size should be passed as a constant. 另外,最好考虑数组大小为0的情况。此外,大小应作为常量传递。 No other answer has mentioned this. 没有其他答案提到这一点。
double* BigEl(double* arr, const size_t iSize)
{
if(iSize == 0)
return 0;
double max = arr[0], *x = &arr[0];
for(unsigned int i=1;i<iSize;++i){
if(arr[i] > max){
max = arr[i];
x = &arr[i];
}
}
return x;
}
//function that returns a pointer to the largest number
double *BigEl(double* arr, int arrSize)
{
int i;
double *maximum;
maximum = &arr[0];
for (i = 1; i < arrSize; i++)
{
if (arr[i] > *maximum)
{
maximum = &arr[i];
}
}
return maximum;
}
int main(void)
{
double myarr[10];
int i;
printf("Please insert 10 numbers to the array\n");
for (i = 0; i < 10; i++)
{
scanf("%lf", &myarr[i]);
}
printf("%f\n", *BigEl(myarr, 10));
return 0;
}
Your assignment to *x
is incorrect - you are saying "assign to the location pointed to by x
, without first saying where that is. Aside from that, there are a couple of other issues: 您对*x
分配是不正确的-您说的是“分配给x
指向的位置,而无需先说出它在哪里。除此之外,还有其他两个问题:
#include <stdio.h>
#include <stdlib.h>
// return pointer to location from function
double * BigEl(double* arr, double arrSize)
{
int i;
// initialise both variables
double maximum = arr[0], *max_pos = arr;
for (i = 1; i < arrSize; i++)
{
if (arr[i]>maximum)
{
maximum = arr[i];
// assign address here
max_pos = &arr[i];
}
}
// return address
return max_pos;
}
int main()
{
double myarr[10];
double * max_pos;
int i;
printf("Please insert 10 numbers to the array\n");
for (i = 0; i < 10; i++)
{
scanf("%lf", &myarr[i]);
}
// use return value here
max_pos = BigEl(myarr, 10);
return 0;
}
I need to write a function that returns a pointer to the largest number in an array 我需要编写一个返回指向数组中最大数字的指针的函数
I think you need the follwoing 我认为您需要关注
#include <stdio.h>
double * largest_element( const double *a, int n )
{
const double *largest = a;
int i;
for ( i = 1; i < n; i++ )
{
if ( *largest < a[i] ) largest = a + i;
}
return ( double *)largest;
}
#define N 10
int main(void)
{
double a[N];
int i;
printf("Please insert %d numbers to the array: ", N );
for ( i = 0; i < N; i++ )
{
scanf( "%lf", &a[i] );
}
printf( "\nThe largest element of the array is %lf\n", *largest_element( a, N ) );
return 0;
}
If to enter for example 例如输入
2.2 1.5 5.2 1.8 3.9 5.9 7.7 6.8 2.9 0.8
then the program output will be 然后程序输出将是
The largest element of the array is 7.700000
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.