CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jun 2011
    Posts
    13

    Max and Min using arrays and pointers

    I'm new in C++, and I have been trying to come with a code to get the max and min value of and array. The program prompt the user to enter a series of integers. I also using pointers.

    Here is my code


    #include<iostream>
    #include<string>
    using namespace std;

    int minimun( const int* values, size_t numValues);
    int maximum( const int* values, size_t numValues);

    int main()
    {
    const size_t maxValues = 10;
    const int array7Size = 10;
    int array7[array7Size] = {};
    int *array7Ptr = array7;
    int array7min = array7[0];
    int array7max = array7[0];

    cout <<" Enter up to 10 integers"<< endl;
    minimun( array7, array7min );
    maximum( array7, array7max );
    cout <<"Minimum value: "<< array7min << endl;
    cout <<"Maximum value: "<< array7max << endl;

    } // end while

    //fFunctions that find the maximum value of the array.

    int maximun( const int* array7 [], size_t array7max )
    {

    for ( size_t i = 0; i < array7max ; i++ )
    {

    cin >> *array7max[i];


    if ( *array7[i] > array7max )
    {
    array7max = array7max[i];
    }
    }

    return array7max;


    }


    // function that find the minimum value nof the array.

    int miminum(const int* array7 [], size_t array7min)
    {

    if (*array7[i] < array7min )
    {
    array7min = *(array7min + i);
    }

    return array7min;

    }

  2. #2
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Exclamation Re: Max and Min using arrays and pointers

    Duplicate!

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Max and Min using arrays and pointers

    Code tags and indentation dude. That mess is unreadable. What's your question?

    I formatted it myself and nothing you wrote really makes any sense.

    You're prompting to enter values, but not actually accepting values.

    You're setting your initial minimum to zero, and I told you in your other thread why that won't work.

    You're accepting values in your minimum and maximum functions. You should run those after the array is full.

    I have no idea what adding i is supposed to do in your minimum function. It's not even defined.
    Last edited by GCDEF; July 15th, 2011 at 02:30 PM.

  4. #4
    Join Date
    Oct 2009
    Posts
    56

    Re: Max and Min using arrays and pointers

    Like the previous poster said, you are not getting the values that they put in. Once you prompt them with
    Code:
    cout <<" Enter up to 10 integers"<< endl;
    you should have something like
    Code:
    cin>>some variable to store what they entered in
    That line will handle if they enter ints, but it won't handle if they put in char or something (look into atoi if you need to check for that stuff).

    Your min function lacks a loop.

    Apparently the previous poster already told you in a different thread, but just to reiterate so people who see this thread and not the other will know - setting the initial value to 0 is wrong because what if the array is full of negative numbers? Instead, you should set it to the first element in the array.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured