Thread: Array average program

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    225

    Array average program

    I have a program that averages numbers. It works perfectly, but I need it to work with multiple functions and arrays. I'm stuck on figuring out how to get it to work like that, and I need some help. My current code is:
    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    int main( ) {
        float n;
        float average;
        float o;
        float value = 0;
       cout << "What size array? ";
       cin >> o;
        n = o;
        for(float i = 0; i < n; ++i) {
            cin >> value;
            average += value;
        }
        average /= n;
        cout << "The average of these " << o << " numbers is " << fixed << setprecision(2) << average << ".";
         }
    Can anyone help?

  2. #2
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    Any help?

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    582
    I'm afraid your question isn't clear enough -- I just don't know what you're asking!

    I gather from your original post that you aren't quite sure what you're asking either -- so could you describe what you want the program to do, and maybe a little bit about why? Some example output of what you want would be helpful too. I'm sure someone will be along soon with all the answers.

  4. #4
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    At first I thought the OP meant that the user would be able to pass user-defined functions or something to an averaging class but I really think the OP just means that their assignment is to use multiple functions to process the same functionality instead of it all living in main().

  5. #5
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    For example:
    Code:
    What size array? 5
    5 
    6 
    7 
    8 
    9
    The average of these 5 numbers is 7.00.
    but using arrays.

  6. #6
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    What I'm expecting is
    Code:
    What size array? 5
    5 
    6 
    7 
    8 
    9
    The average of these 5 numbers is 7.00.
    but using arrays.
    edit: sorry internet messed up, double post

  7. #7
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Okay, cool.

    First off, you need to determine how your array will function.

    Can you use std::vector? If so, that's awesome and will likely simplify many things.

  8. #8
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    I can't use std::vector.

  9. #9
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Right. I figured.

    Okay, then you need to emulate how std::vector works.
    Code:
    int size = 128;
    
    int data[size] = { 0 };
    
    /* read in user's proposed array size.
    If (proposed_size > size) { handle_errors_here(); } */
    
    // this will be used to mark the end of an array
    int back = 0;
    
    // begin cin-loop
    // remember, proposed_size must be less than size
    for (int i = 0; i < proposed_size; ++i)
    {
        data[back] = cin something
        ++back;
    }
    This way, you emulate std::vector and its push_back() method which would've conveniently solved your problem as well.

    What I'm (other users may disagree with my solution) proposing you do is what I have above. Allocate an array and read into it like you would std::vector.

    This way you can then easily iterate the array and calculate the average from there.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,397
    The problem with MutantJohn's suggestion is in the details. This:
    Code:
    int size = 128;
     
    int data[size] = { 0 };
    relies on the language feature known as "variable length arrays". This feature is non-standard. It is provided as a language extension by g++ and maybe other compilers, but since it is non-standard, it is not guaranteed to be available. The right approach is to use std::vector. Since you cannot use std::vector, you should either assume a maximum size for the array (and perhaps enforce it by checking the input) and then only use the portion thereof as requested by the user, or use dynamic memory allocation.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    100 will be my maximum size then.

  12. #12
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    Any help? I need this done quickly, so I need more help.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,397
    Create the array of 100 elements, then only use n elements, where n is what the user entered as the size of the array.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Could you use std::array? It's a pretty simple class, you can use template arguments to make passing it to functions more agnostic (although my example might not be the best in practical use, see the average function comment):

    Edit: Nevermind, I misunderstood the nature of the problem, and gave an unrelated answer.
    Last edited by Alpo; 02-08-2015 at 11:41 AM.
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

  15. #15
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    Not working:
    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    float Mean(float test[8], float test2[9] ) {
          float value = 3;
          test[8] = 9;
          test2[9] = 10;
      //    cin >> test[9];
        //  cin >> test2[8];
        //  float thing = 2.5;
          return value;
          
    }
    int main( ) {
        float n;
        float average;
        float o;
        float value = 0;
        float array[100];
      //  float array1[7];
        //float array2[15];
        
       cout << "What size array? ";
       cin >> o;
        n = o;
        for(float i = 0; i < n; ++i) {
            cin >> array[i];
            average += array[i];
        }
        average /= n;
      //  Mean(array1, array2);
        cout << "The average of these " << o << " numbers is " << fixed << setprecision(2) << average << ".";
         }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array average help
    By internalbalance in forum C Programming
    Replies: 15
    Last Post: 03-24-2014, 02:08 PM
  2. Average of an Array
    By Peteski in forum C Programming
    Replies: 19
    Last Post: 01-05-2012, 04:38 PM
  3. What's wrong with my Average Array program?
    By special1zed in forum C Programming
    Replies: 12
    Last Post: 03-15-2009, 11:01 PM
  4. average of an array
    By mrsirpoopsalot in forum C Programming
    Replies: 20
    Last Post: 09-16-2006, 04:32 PM
  5. average in an array
    By s_ny33 in forum C++ Programming
    Replies: 2
    Last Post: 04-22-2005, 01:03 PM