help with functions and array please

This is a discussion on help with functions and array please within the C++ Programming forums, part of the General Programming Boards category; Hi Guys, Very new with C++ so please bear with me ;-) Been asked to create a SIMPLE program with ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    25

    help with functions and array please

    Hi Guys,

    Very new with C++ so please bear with me ;-)

    Been asked to create a SIMPLE program with an array [20] that holds values from 0 - 100. I then need to write functions to calculate:

    - largest number in the array
    - smallest number in the array
    - the sum of all of the numbers in the array
    - the mean of all of the numbers in the array

    Now on my own I have started it off by splitting up the functions into four sections:

    - void get_largest_number();
    - void get_smallest_number();
    - void get_sum_of_number();
    - void get_mean_of_number();

    Now in the get_largest_number() function I have created an array that increments by one in a FOR loop every time a number is entered then each number is added to the total each time it loops. I have so far got this far, however Iím not sure about my calculations for the:

    - void get_smallest_number();
    - void get_sum_of_number();
    - void get_mean_of_number();

    Iíve had a go, but donít think itís right Ė can anyone please advise where im wrong with these other functions (if they are wrong)?

    Many Thanks

    Cbo

    Code:
    / values.h
    // Determining the largest, smallest, sum and mean
    #include <iostream.h>
    
    class values
      {
       public:
         void get_largest_number();
         void get_smallest_number();
         void get_sum_of_number();
         void get_mean_of_number();
       protected:
         int sum = 0;
         int mean = 0;
       };
    
    void values::get_largest_number(int a[])
    {
         largest_number = a[0];
         
         for(i=1; i<20; i++)
         { 
           if(a[i] > largest_number)
           {
                largest_number = a[i];
           }
         }
    } 
    
    void values::get_smallest_number(int a[])
    {
         smallest_number = a[0];
         
         for(i=1; i<20; i++)
         { 
           if(a[i] < smallest_number)
           {
                smallest_number = a[i];
           }
         }
    } 
    
    
    void values::get_sum_number()
    {
         total=0;
         for ( int i = 0; i < 20; i++)
    
            { total +=[i];
            }
    
        sum = total;
    }
    
    
    
    void values::get_mean_number()
    {
    
         for ( int i = 0; i < 20; i++)
    
            { sum +=a[i];
            }
    
        mean = sum / 20;
    }

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    85
    For starters, you want to be returning the type of the largest number, assuming they are ints change it to
    Code:
    int values::get_largest_number(int a[])
    {
         largest_number = a[0];
         
         for(i=1; i<20; i++)
         { 
           if(a[i] > largest_number)
           {
                largest_number = a[i];
           }
         }
    }
    You have an array argument here so you must also have it in your class definition.

    The logic seems correct by glance, just change your return types to ints and correct argument lists.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    25
    Cheers jlf029, just me being stupid declaring my functions as void() and not int().

    The thing i'm not sure about are these two functions:

    Code:
    void values::get_sum_number()
    {
         total=0;
         for ( int i = 0; i < 20; i++)
    
            { total +=[i];
            }
    
        sum = total;
    }
    
    
    
    void values::get_mean_number()
    {
    
         for ( int i = 0; i < 20; i++)
    
            { sum +=a[i];
            }
    
        mean = sum / 20;
    }
    Can someone tell me if my implementation is right here to get the sum of numbers in the first function and mean number in the second function - am I going on the right line?

    Many Thanks

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    In get_mean_number, mean is not declared (and I think you should return it, too):
    Code:
    int values::get_mean_number()
    {
        int mean;
    
         for ( int i = 0; i < 20; i++)
    
            { sum +=a[i];
            }
    
        mean = sum / 20;
    
        return mean;
    }
    [edit]
    Never mind; I see mean is part of the class.
    [/edit]
    Last edited by dwks; 11-24-2005 at 09:00 AM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Code:
    #include <iostream.h> --> <iostream>
    However, you never use <iostream> in that file anyway.
    ----
    The thing i'm not sure about are these two functions:
    Code:
    void values::get_sum_number()
    {
         total=0;
         for ( int i = 0; i < 20; i++)
    
            { total +=[i];
            }
    
        sum = total;
    }
    -----
    Code:
    class values
      {
       public:
         void get_largest_number();
         void get_smallest_number();
         void get_sum_of_number();
         void get_mean_of_number();
       protected:
         int sum = 0;
         int mean = 0;
       };
    This ain't Java. In C++, you can can't initialize class member variables in the class declaration. You need to use a constructor to initialize those variables.
    ---
    class values
    By convention, class names are capitalized.

    Since the class name is 'values' wouldn't it make more sense to make the array a member variable? And if 'sum' and 'mean' are member variables, why aren't 'smallest' and 'largest'?

    Think about how your get_sum_number() function works. Are you going to calculate the sum of the array every time someone needs it? That seems like a waste since it only needs to be calculated once. Or, are you going to calculate the sum once and then store the sum in the member variable 'sum'? If you calculate it once and store the result in the member variable 'sum', how will the user be able to access 'sum' to get its value?

    Also, look at the way you are calculating the sum and mean:
    Code:
    void values::get_sum_number()
    {
         total=0;
         for ( int i = 0; i < 20; i++)
    
            { total +=[i];
            }
    
        sum = total;
    }
    void values::get_mean_number()
    {
    
         for ( int i = 0; i < 20; i++)
    
            { sum +=a[i];
            }
    
        mean = sum / 20;
    }
    Where does a[i] come from? It's not a parameter of the function, it's not a local variable you declared in the function, and it's not a class member.

    Also, if you calculate the sum of an array and store it in the member variable sum, what is the idea behind adding more numbers to sum in the function get_mean_number()?
    Last edited by 7stud; 11-24-2005 at 03:01 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An array of macro functions?
    By someprogr in forum C Programming
    Replies: 6
    Last Post: 01-28-2009, 07:05 PM
  2. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM
  3. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 09:31 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 10:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21