0

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. 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. 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.

5
  • 3
    You are not returning anything from the function. Commented Mar 28, 2015 at 13:21
  • @haccks whilst true, that's not what's generating the compiler error (which is OP's question). Commented Mar 28, 2015 at 13:23
  • BTW "%d" for int Commented Mar 28, 2015 at 13:23
  • 1
    @abligh; I didn't say that. My comment was in response to the title. Commented Mar 28, 2015 at 13:24
  • OT: It's int main(void) at least. Commented Mar 28, 2015 at 13:57

5 Answers 5

4

You did not initialize the variable x. You merely wrote to the the location pointed to by x, here:

    *x = maximum;

when x was uninitialized, which is what the compiler is complaining about.

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:

  1. Use size_t for the array size and the counter, not a double and an int.
  2. Retain a pointer to the maximum element, not the maximum element's value.
  3. Return the pointer to the maximum element.
  4. Remove superflous braces.
Sign up to request clarification or add additional context in comments.

Comments

2

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. 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;
}

1 Comment

For completeness it should be mentioned that for array indices the type size_t is the preferred one. Your code will still fail for negative "sizes".
1

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:

#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;
}

Comments

1
//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;    
}

Comments

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

3 Comments

@Amnon Hanuhov What do you mean?!
I enter like 4 numbers and it finishes the scan, it is suppose to scan 10 times, however if I enter integers it scans 10 times. plus it prints a weird number.
Never mind it works, I did a small mistake that I did not notice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.