Finding min/max with c++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Deanm007
    New Member
    • Apr 2010
    • 37

    Finding min/max with c++

    Hi, I have three questions. The code is below my Q's.

    1. I just made it to work blindly. I don't even know what this means:
    max=iNumbers[1];
    min=iNumbers[1];
    what does it mean?

    2. And what does this mean:

    if (iNumbers[i] > max ) {
    max=iNumbers[i];

    3. The sum and avg part could have been done in an easier way?


    Code:
    //Make a program that calculates the 
    //sum, mean, minimum, and maximum of a series of numbers.
    //Programer inputs initial numbers. Not user.
    
    #include<iostream>
    #include<cmath>
    
    using namespace std;
    
    int main() {
    	
    	double sum, mean, min, max;
        double iNumbers[]={1,12,9,154};
    	sum = iNumbers[0] + iNumbers[1] + iNumbers[2] + iNumbers[3];
    	mean = (iNumbers[0] + iNumbers[1] + iNumbers[2] + iNumbers[3])/4;
    	cout << "the sum is: " << sum << endl;
    	cout << "the mean is " << mean << endl;
    
    	max=iNumbers[1];
    	min=iNumbers[1];
    
    	for (int i=0; i<4; i++) {
    		if (iNumbers[i] > max ) {
    			max=iNumbers[i];
    			cout << "the max is " << max << endl;
    		}
    		if (iNumbers[i] < min) {
    			min=iNumbers[i];
    			cout << "the min is " << min << endl;
    		}
    		
    	}
    
    	return 0; //program worked after placing 1 in min=iNumbers[1]
    
    }
    Last edited by Banfa; Apr 15 '10, 09:55 AM. Reason: Added [code]...[/code] tags
  • Dheeraj Joshi
    Recognized Expert Top Contributor
    • Jul 2009
    • 1129

    #2
    Code:
    max=iNumbers[1];
    min=iNumbers[1];
    You are assigning the variable max and min to 2nd element of array.
    Please note array indexes start from 0

    Code:
    if (iNumbers[i] > max ) {
    max=iNumbers[i];
    You are checking the value at position i of an array, if it is greater than variable max(which is you have initialized for the first time with iNumbers[1]). If it is true you are reassigning the max with iNumbers[i].

    One way to find out sum and average:
    Code:
    int iSum = 0;
    float fAvg = 0.0;
    for(int iCount=0;iCount<length;iCount++)
    {
             sum = sum + array[iCount];
    }
    avg = sum/length;
    Regards
    Dheeraj Joshi

    Comment

    • jkmyoung
      Recognized Expert Top Contributor
      • Mar 2006
      • 2057

      #3
      max=iNumbers[1];
      You're accessing the element in position 0. So for {1,12,9,154};
      iNumbers[0] = 1
      iNumbers[1] = 12
      iNumbers[2] = 9
      iNumbers[3] = 154

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Basically your current algorithm is wrong because you have the output statements in the the wrong place, currently lines 25 and 29. This is inside the calculation because it is inside the loop. You should output your results once the calculation has finished, after the loop has finished.

        Because of this you have had to choose start indexes at lines 19 and 20 that cause the correct output but that does not change the fact the algorithm is wrong and only works for the input data you have given not any data, for example try

        double iNumbers[]={11,12,9,154};

        or

        double iNumbers[]={13,12,9,154};

        Line 19 and 20 should initialise from index 0, you need to fix the rest of your algorithm so that it then prints the correct values.

        Comment

        • Deanm007
          New Member
          • Apr 2010
          • 37

          #5
          Dheeraj, thanks for the explanation and the sum/avg solution. Banfa, thanks for solving my issue. Using your tip, I have done the following and it worked. But two more questions are posted with the code below:

          //Make a program that calculates the
          //sum, mean, minimum, and maximum of a series of numbers.
          //Programer inputs initial numbers. Not user.

          #include<iostre am>
          #include<cmath>

          using namespace std;

          int main() {

          double sum, mean, min, max;
          double iNumbers[]={11,-1,-9,154};

          sum = 0; //why are we setting these equal to 0?
          mean = 0.0;
          for(int iCount=0;iCount <4;iCount++){
          sum = sum + iNumbers[iCount];
          }
          mean = sum/4;

          cout << "the sum is: " << sum << endl;
          cout << "the mean is " << mean << endl;

          max=iNumbers[0];
          min=iNumbers[0];

          for (int i=0; i<4; i++) {
          if (iNumbers[i] > max ) { //what is the first step that takes place here?
          max=iNumbers[i]; // 0 is passed into [i] and then what happens?
          }
          }
          for (int i=0; i<4; i++) {
          if (iNumbers[i] < min) {
          min=iNumbers[i];
          }
          }
          cout << "the max is " << max << endl;
          cout << "the min is " << min << endl;

          return 0; //program works now.

          }

          Comment

          • Deanm007
            New Member
            • Apr 2010
            • 37

            #6
            Ok i realized that the sum needs a base(which is 0) to start adding the array's elements inside the loop. Also realized that the mean does not have to be initialized to 0. Am I correct in these deductions?

            And I am still wondering how that for loop starts executing(see Q in code).

            Thanks so much.

            Comment

            • asna
              New Member
              • Apr 2010
              • 2

              #7
              we set sum innitially at zero coz the rest of elements add up in it and are saved the. in contrast if u set it as some other number the elements of array will be added to it and you wont get the exact sum

              down code is for max

              #include<iostre am>

              using namespace std;

              int main()
              {
              int max;
              int A[] = {5,7,12,6,9,4};
              int M=0;


              M=0;
              for(int i=0;i<6;i++)
              {
              if(A[M]<A[i])
              M=i;
              max = A[M];

              }
              cout<<"The maximum value is"<<max<<end l;

              system("pause") ;

              return 0;
              }


              for minimum only reverse A[M] with A[i] i.e if(A[i]<A[M])

              for sum
              sum +=A[i];

              average

              sum/total elements of array

              Comment

              • asna
                New Member
                • Apr 2010
                • 2

                #8
                if (iNumbers[i] > max ) { //what is the first step that takes place here?
                max=iNumbers[i]; // 0 is passed into [i] and then what happens?
                }
                for the first Q there is no step the compiler will check which one is greater and thats it

                0 is not passed into 'i' it is the initial value of 'i', as the value increases the checks which one is greater and stores it in max... if 'i' is greater it will replace value of max with value of 'i' otherwise it will remain unchanged

                Comment

                • Deanm007
                  New Member
                  • Apr 2010
                  • 37

                  #9
                  thanx for the code Asna. i think i will better understand the workings of a for loop if u can give me a concrete example of how this works. for example:

                  double iNumbers[]={111,-1,-9,15};

                  max=iNumbers[0]; // max is now set at the number located in the 0 position of the array which is 111. correct?

                  for (int i=0; i<4; i++) {
                  if (iNumbers[i] > max ) {
                  max=iNumbers[i];

                  so here, 1st we are asking if (iNumbers[0] >111}...which is if (111>111)--> this isn't true so max cannot be set equal to 111. But somehow my program figures out that the max is 111. Thats why I am asking how this works.

                  In other words, if the max number is in any other position, its easy to understand. But if it is in the 0 position of the array, how does the program manage to find the answer?

                  thanks

                  Comment

                  • jkmyoung
                    Recognized Expert Top Contributor
                    • Mar 2006
                    • 2057

                    #10
                    Take your initial array. double iNumbers[]={1,12,9,154};
                    max = 1

                    Now iterate:
                    i = 0: max = 1
                    1 > 1? NO
                    i = 1: max = 1
                    12 > 1? YES! max = 12
                    i = 2: max = 12
                    9 > 12? NO.
                    i = 3: max = 12
                    154 > 12? YES! max = 154

                    So max changes from 1 to 12, stays at 12, then changes to 154.
                    Max ends up at 154, and only ever increases.

                    Comment

                    • Deanm007
                      New Member
                      • Apr 2010
                      • 37

                      #11
                      thanks JKM, that was very clear. My question has been resolved. A lot of people helped me in this thread. I can't just pick one best answer...

                      Comment

                      Working...