Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Finding Min and Max of array

Finding Min and Max of array

Scheduled Pinned Locked Moved C / C++ / MFC
data-structureshelpquestion
7 Posts 6 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mikerush7
    wrote on last edited by
    #1

    Hi. I have assignment to find difference between Min and Max of an array of 5 numbers. I did some work, but after compiling - my program does not run, it just stops working.. Can you help me, where is my mistake ? Array values needs to be double.

    #include #include int main()
    {
    int i; // element in array
    int size; // size of array
    double array [i]; // number of elements in array
    double min; //minimum value
    double max; // maximum value
    double difference; // difference between maximum and minimum values

    printf("Enter the size of the array: ");
    scanf("%d",&size);
    printf("Enter %d elements in to the array: ", size);
    scanf("%d",&array[i]);

    max = array[0]; // let first number in array be maximum
    for (i=0; iarray[i])
    min = array [i];
    {
    printf("Minimum value is: %d\n",min);
    }

    difference = max - min;

    printf ("Difference between Min and Max values is %d\n", difference);

    /* if (i>= 5)
    {
    printf ("Only 5 numbers allowed. \n");
    break;
    }
    */

    system("PAUSE");
    return 0;
    }

    J C CPalliniC _ L 5 Replies Last reply
    0
    • M Mikerush7

      Hi. I have assignment to find difference between Min and Max of an array of 5 numbers. I did some work, but after compiling - my program does not run, it just stops working.. Can you help me, where is my mistake ? Array values needs to be double.

      #include #include int main()
      {
      int i; // element in array
      int size; // size of array
      double array [i]; // number of elements in array
      double min; //minimum value
      double max; // maximum value
      double difference; // difference between maximum and minimum values

      printf("Enter the size of the array: ");
      scanf("%d",&size);
      printf("Enter %d elements in to the array: ", size);
      scanf("%d",&array[i]);

      max = array[0]; // let first number in array be maximum
      for (i=0; iarray[i])
      min = array [i];
      {
      printf("Minimum value is: %d\n",min);
      }

      difference = max - min;

      printf ("Difference between Min and Max values is %d\n", difference);

      /* if (i>= 5)
      {
      printf ("Only 5 numbers allowed. \n");
      break;
      }
      */

      system("PAUSE");
      return 0;
      }

      J Offline
      J Offline
      jeron1
      wrote on last edited by
      #2

      I've not looked terribly close, but please start by putting { }'s around your for loop blocks. for example;

      for (int i = 0; i < size; i++)
      { //added
      //do loop stuff here

      } //added

      1 Reply Last reply
      0
      • M Mikerush7

        Hi. I have assignment to find difference between Min and Max of an array of 5 numbers. I did some work, but after compiling - my program does not run, it just stops working.. Can you help me, where is my mistake ? Array values needs to be double.

        #include #include int main()
        {
        int i; // element in array
        int size; // size of array
        double array [i]; // number of elements in array
        double min; //minimum value
        double max; // maximum value
        double difference; // difference between maximum and minimum values

        printf("Enter the size of the array: ");
        scanf("%d",&size);
        printf("Enter %d elements in to the array: ", size);
        scanf("%d",&array[i]);

        max = array[0]; // let first number in array be maximum
        for (i=0; iarray[i])
        min = array [i];
        {
        printf("Minimum value is: %d\n",min);
        }

        difference = max - min;

        printf ("Difference between Min and Max values is %d\n", difference);

        /* if (i>= 5)
        {
        printf ("Only 5 numbers allowed. \n");
        break;
        }
        */

        system("PAUSE");
        return 0;
        }

        C Offline
        C Offline
        Chris Losinger
        wrote on last edited by
        #3

        Mikerush7 wrote:

        Array values needs to be double.

        then this is wrong:

        Mikerush7 wrote:

        scanf("%d",&array[i]);

        image processing toolkits | batch image processing

        1 Reply Last reply
        0
        • M Mikerush7

          Hi. I have assignment to find difference between Min and Max of an array of 5 numbers. I did some work, but after compiling - my program does not run, it just stops working.. Can you help me, where is my mistake ? Array values needs to be double.

          #include #include int main()
          {
          int i; // element in array
          int size; // size of array
          double array [i]; // number of elements in array
          double min; //minimum value
          double max; // maximum value
          double difference; // difference between maximum and minimum values

          printf("Enter the size of the array: ");
          scanf("%d",&size);
          printf("Enter %d elements in to the array: ", size);
          scanf("%d",&array[i]);

          max = array[0]; // let first number in array be maximum
          for (i=0; iarray[i])
          min = array [i];
          {
          printf("Minimum value is: %d\n",min);
          }

          difference = max - min;

          printf ("Difference between Min and Max values is %d\n", difference);

          /* if (i>= 5)
          {
          printf ("Only 5 numbers allowed. \n");
          break;
          }
          */

          system("PAUSE");
          return 0;
          }

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #4

          (1)

          Quote:

          int i; // element in array int size; // size of array double array [i]; // number of elements in array

          You shouldn't do that, even if the compiler allows the C99 extension (see, for instance "Arrays of Variable Length"[^]) since variable i is NOT initialized. You should either allocate a fixed size array, e.g

          double array[5];

          or dynamically allocate the array, based on user choice, e.g.

          double * array;
          int size;
          printf("Enter the size of the array: ");
          if ( scanf("%d",&size) != 1 || size < 1)
          {
          return -1; // in real life do a better error handling
          }
          array = (double) * malloc( size * sizeof(double));
          if ( ! array )
          {
          return -1; // ...
          }

          (2)

          Quote:

          printf("Enter %d elements in to the array: ", size); scanf("%d",&array[i]);

          You should prompt the user for every item of the array, that is you should use a loop:

          int n;
          for (n=0; n

          (3) NOTE THIS IS NOT AN ERROR

          Quote:

          for (i=0; i<size; i++)
          if (max < array [i])
          max = array [i];
          {
          printf("Maximum value is: %d\n",max);
          }

          min = array [0]; // let first number in array be minimum
          for (i=0; i < size; i++)
          if (min > array[i])
          min = array [i];
          {
          printf("Minimum value is: %d\n",min);
          }

          You didn't need two separate loops in order to find max & min values:

          double min = max = array[0];
          for (i=0; iarray[i])
          min = array [i];
          if (max

          (4)

          Quote:

          printf ("Difference between Min and Max values is %d\n", difference);

          Here, as usual you used %d instead of %f for double format specifier. Change to

          printf ("Difference between maximum and minimum values is %f\n", difference);

          In testa che avete, signor di Ceprano?

          1 Reply Last reply
          0
          • M Mikerush7

            Hi. I have assignment to find difference between Min and Max of an array of 5 numbers. I did some work, but after compiling - my program does not run, it just stops working.. Can you help me, where is my mistake ? Array values needs to be double.

            #include #include int main()
            {
            int i; // element in array
            int size; // size of array
            double array [i]; // number of elements in array
            double min; //minimum value
            double max; // maximum value
            double difference; // difference between maximum and minimum values

            printf("Enter the size of the array: ");
            scanf("%d",&size);
            printf("Enter %d elements in to the array: ", size);
            scanf("%d",&array[i]);

            max = array[0]; // let first number in array be maximum
            for (i=0; iarray[i])
            min = array [i];
            {
            printf("Minimum value is: %d\n",min);
            }

            difference = max - min;

            printf ("Difference between Min and Max values is %d\n", difference);

            /* if (i>= 5)
            {
            printf ("Only 5 numbers allowed. \n");
            break;
            }
            */

            system("PAUSE");
            return 0;
            }

            _ Offline
            _ Offline
            _Superman_
            wrote on last edited by
            #5

            Since this is your assignment, you probably want to write the algorithm yourself. But just so you know, C++ already has an implementation for this algorithm. So a C++ program would be as follows -

            #include <algorithm>

            int main()
            {
            double arr[7] = { 5.6, 3.4, 9.8, 1.2, 5.5, 9.9, 4.9 };

            std::pair<double\*, double\*> minmax = std::minmax\_element(&arr\[0\], &arr\[7\]);
            double min = \*minmax.first;
            double max = \*minmax.second;
            
            return 0;
            

            }

            With the new C++ standard, you could write this in a more generic way -

            #include <algorithm>

            int main()
            {
            double arr[7] = { 5.6, 3.4, 9.8, 1.2, 5.5, 9.9, 4.9 };

            auto minmax = std::minmax\_element(std::begin(arr), std::end(arr));
            auto min = \*minmax.first;
            auto max = \*minmax.second;
            
            return 0;
            

            }

            «_Superman_»  _I love work. It gives me something to do between weekends.

            _Microsoft MVP (Visual C++) (October 2009 - September 2013)

            Polymorphism in C

            1 Reply Last reply
            0
            • M Mikerush7

              Hi. I have assignment to find difference between Min and Max of an array of 5 numbers. I did some work, but after compiling - my program does not run, it just stops working.. Can you help me, where is my mistake ? Array values needs to be double.

              #include #include int main()
              {
              int i; // element in array
              int size; // size of array
              double array [i]; // number of elements in array
              double min; //minimum value
              double max; // maximum value
              double difference; // difference between maximum and minimum values

              printf("Enter the size of the array: ");
              scanf("%d",&size);
              printf("Enter %d elements in to the array: ", size);
              scanf("%d",&array[i]);

              max = array[0]; // let first number in array be maximum
              for (i=0; iarray[i])
              min = array [i];
              {
              printf("Minimum value is: %d\n",min);
              }

              difference = max - min;

              printf ("Difference between Min and Max values is %d\n", difference);

              /* if (i>= 5)
              {
              printf ("Only 5 numbers allowed. \n");
              break;
              }
              */

              system("PAUSE");
              return 0;
              }

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              You canot write this way

              double array [i];

              beacuse here i is not initialized. so when compiling compiler will give the error like 1. Array unknown size 2. cannot allocate an array of constant size size 0. 2. expected constant expression. so you can write this way

              double array[10]

              or dynamically allocate the array.

              printf("Enter %d elements in to the array: ", size);
              scanf("%d",&array[i]);

              why you are write in this way? you just think how to read the required number of elements in to array without a loop? so reading of this array with in a loop, ie see below

              printf("Enter %d elements in to the array: ", size);
              for( i = 0; i < size; i++ )
              {
              scanf("%d",&array[i]);
              }

              also an important note when writing the code is give meaning full name for variable. because this will increase the readability of your code. Also keep a coding standard when writing code. Next is the logic which is used to find minimum and maximum value from the array. you can optimise your code like this way double Min = array[0]; double Max = array[0];

              for( i = 0; i < size; i++ )
              {
              if( Max < array[i] )
              {
              Max = array[i];
              }
              if( Min > array[i] )
              {
              Min = array[i]
              }
              }

              so code optimization is important when coding in large projects. Thanks

              M 1 Reply Last reply
              0
              • L Lost User

                You canot write this way

                double array [i];

                beacuse here i is not initialized. so when compiling compiler will give the error like 1. Array unknown size 2. cannot allocate an array of constant size size 0. 2. expected constant expression. so you can write this way

                double array[10]

                or dynamically allocate the array.

                printf("Enter %d elements in to the array: ", size);
                scanf("%d",&array[i]);

                why you are write in this way? you just think how to read the required number of elements in to array without a loop? so reading of this array with in a loop, ie see below

                printf("Enter %d elements in to the array: ", size);
                for( i = 0; i < size; i++ )
                {
                scanf("%d",&array[i]);
                }

                also an important note when writing the code is give meaning full name for variable. because this will increase the readability of your code. Also keep a coding standard when writing code. Next is the logic which is used to find minimum and maximum value from the array. you can optimise your code like this way double Min = array[0]; double Max = array[0];

                for( i = 0; i < size; i++ )
                {
                if( Max < array[i] )
                {
                Max = array[i];
                }
                if( Min > array[i] )
                {
                Min = array[i]
                }
                }

                so code optimization is important when coding in large projects. Thanks

                M Offline
                M Offline
                Mikerush7
                wrote on last edited by
                #7

                Thank you for your help !

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups