CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 3 of 3

Thread: Really need help with my Array HW

  1. #1
    Join Date
    Dec 2018

    Really need help with my Array HW

    So I need to write a c++ program where:

    1)Read a sequence of integers from a file into an array
    2) Pass that array of integers to a function which will compute and return the average (as a double).
    3) Display the average returned from the function

    I feel like I am overthinking it but here is what I have so far:

    using namespace std;
    int main()
        int n = 0; //n is the number of the integers in the file ==> 12
        int num;
        int arr[100];
        ifstream File;"numbers.txt");
            File >> arr[n];
        for(int i=0;i<12;n++)
            cout << arr[i] << " ";
        cout << "done\n";
        return 0;
    Last edited by VictorN; December 4th, 2018 at 03:27 PM. Reason: add CODE tags

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Hanover Germany

    Re: Really need help with my Array HW

    Well, did you test your code works as expected?
    If it does then what is your next problem in writing the function "which will compute and return the average"?
    if it does not work as expected then what is the problem?

    And BTW, please always use the CODE tags while posting the code snippets!
    Victor Nijegorodov

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012

    Re: Really need help with my Array HW

    The 'usual' way to read numbers from a file into a fixed size array would be:

    const size_t arrsize = 100;  // Size of array
    int arr[arrsize] = {0};
    size_t n = 0;
    for (; (n < arrsize) && (File >> arr[n]); ++n);
    So that you don't overflow the array if more than the expected number of values from the file are tried to be read. Usually, though, in a situation like this then a container such as a vector would be used so that you don't need to care about the actual number of values read from the file.

    Also, you have a code error in your loop to display the values of the array.

    Now you just need to code the function to compute and return the average of an array!
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.6.3)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)