how to find the average of thee groups of values?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • neesaY
    New Member
    • Aug 2014
    • 3

    how to find the average of thee groups of values?

    HELLO,

    Any idea how to compute the averages for 6 values that have been computed from a for loop,

    I mean once the for loop finishes it prints me six different values classified into 3 groups as shown down:

    group1 has value1
    groupe2 has value2, value3, value3
    group3 has value5, value6

    I want to find the average for each group.

    considering the number of values in each group might change.
    this is the code for finding the values:


    Code:
    for (int i=0; i<n; i++)
    	{
    		if (value < 25)
    		{
    		    group1++;
    		    class = "group1";
    
    		}
    		else if (value > 50)
    		{
    			groupe3++;
    			class = "groupe3";
    		}
    		else 
    		{
    			group2++;
    			class = "group2";
    		}
    the final result like this:
    cout<<"average of group1 is"<<
    cout<<"average of group2 is"<<
    cout<<"average of group3 is"<<

    :) ?
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    The average is the sum of the elements in the group divided by the number of elements in the group.

    I see you count the number of elements in the group but I do not see where you add up the values of the elements. You need to do that so you can divide this sum by the number of elements in the group.

    You don't need the class literal at all:

    Code:
    cout << "Group 1 average= " << sumgroup1/group1 << endl;

    Comment

    • neesaY
      New Member
      • Aug 2014
      • 3

      #3
      the problem is that the program gets the value from a file and it did classifies the values into groups according to some characteristics they had,
      so your way required me to know each value in each group! and I can't do that.

      what I want is the program to classify the values into groups and then find the averages.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        You know each value in each group.

        This code:
        Code:
        if (value < 25)
                 {
                     group1++;
                     class = "group1";
        etc....
        identifies value as a member of group 1. So each time you get here add value to a total value for group1. It's the class you don't need.

        Comment

        • neesaY
          New Member
          • Aug 2014
          • 3

          #5
          hmmm I got the values,
          for the classes it is required for the final output of the project, I need to classify the values as ( fast , medium , slow)
          the same as I used up like (group1, group2, group3)
          the values are actually speeds, and I need to find the average of the fast speeds, medium speeds and slow speeds!

          also the values came from an equation already computed the the original project.

          the program do this: get numbers from file then use them in an equation then find the speeds (values)
          .

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            As soon as you start classifying values a FAST, MEDIUM, or SLOW, I start thinking about a struct:

            Code:
            enum group {FAST, MEDIUM, SLOW};
            
            struct Speed
            {
               int value:
               enum group;  
            };
            Each time you read a value from the disc file you could create a Speed variable.

            Code:
            Speed* ptr = malloc(sizeof Speed);
            Put the value in the value member:

            Code:
            ptr->value = disc value;
            Then use the logic to classify the value:

            Code:
            for (int i=0; i<n; i++)
            	{
            		if (value < 25)
            		{
            		    ptr->group = SLOW;
            		}
            		else if (value > 50)
            		{
            			ptr->group = MEDIUM;		}
            etc...
            You could keep these Speed variables in an array or a linked list. When you have processed the disc values, you will have a little database. Now you can scan the database and now you can count the SLOW, MEDIUM and FAST values, add them up, calculate averages, etc.
            Last edited by weaknessforcats; Aug 14 '14, 06:55 PM. Reason: typo

            Comment

            Working...