Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7789297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:15:23+00:00 2026-06-01T21:15:23+00:00

I am newbie to programming and I am trying to pass an array into

  • 0

I am newbie to programming and I am trying to pass an array into a function and add all the elements together and return the sum. The problem is that I am getting a garbage value for the sum. I have researched on how to pass arrays to functions and I do not know if I’m supposed to use a pointer to pass arrays. I am not good with pointers anyways.

Here is my code

#include <cmath>
#include <cstdlib>

using namespace std;
float mean(int);
int sum(int ARRZO[5]);

int total;

int main()
{

    int ARRZ[5];
    char *inname = "example.txt";
    ifstream infile(inname);

    if (!infile) {
        cout << "There was a problem opening file " << inname << " for reading." << endl;
        return 0;
    }
    cout << "Opened " << inname << " for reading." << endl;
    for(int i=0; i<11; i++)
    {
        while (infile >> ARRZ[i]) 
        {
            cout << "Value from file is " << ARRZ[i] << endl;
        }
    }

    total=sum(ARRZ);
    cout<<"the sum of the elements in the array is"<<total<<endl;

    system("PAUSE");

    return 0;
}


int sum(int ARRZO[])
{
    int sumz=0;
    for (int i=0; i<5; i++)
    {
        sumz+=ARRZO[i];
        cout<<ARRZO[i];
    }
    cout<<sumz<<endl;
    return sumz;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T21:15:24+00:00Added an answer on June 1, 2026 at 9:15 pm

    I’m not sure what you think this pair of nested loops is supposed to do:

    for(int i=0; i<11; i++)
    {
        while (infile >> ARRZ[i]) 
        {
            cout << "Value from file is " << ARRZ[i] << endl;
        }
    }
    

    But (as @aliexisdm pointed out) the inner loop reads the entire content of the file. What he didn’t (at least directly) point out is that you’re reading every one of those values into the first element of your array. Then you’re getting back to the outer loop, incrementing i, and trying to read the file again — but since the stream’s failbit has been set, all your subsequent attempts at reading are guaranteed to fail.

    After that, you add up the 5 items in the array, but since you haven’t read anything into 4 of them (and never initialized its contents) you end up with the last item you read from the file + 4 garbage values, giving still further garbage as the result (well, usually anyway — you really have undefined behavior, so the program could crash and burn instead, but with most current computers, you’ll just get some meaningless number).

    I, however, would advise changing the program a bit more than just removing one loop and incrementing in the loop that’s left. Instead, I’d remove all the (explicit) loops, and make some attempt at making real use of what the standard library provides.

    You can read the numbers from the file in one fell swoop:

    std::ifstream infile(inname);
    
    std::vector<int> ARRZ ((std::istream_iterator<int>(infile)),
                            std::istream_iterator<int>());
    

    Then you can sum them all with std::accumulate:

    int sum = std::accumulate(ARRZ.begin(), ARRZ.end(), 0);
    

    Finally, you can print out the result:

    cout << "The sum of the elements in the array is: " << sum << "\n";
    

    Since, however, you only read the values from the file to add them together, you don’t really need to store them at all. You could just add them together and print out the result:

    cout << "The sum of the elements in the file is: " 
         << std::accumulate(std::istream_iterator<int>(infile),
                            std::istream_iterator<int>(), 0);
    

    The whole job reduced to one step…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I`m a JavaScript/programming newbie trying to figure out the new bookmarklet that's been released
I'm a newbie into Android programming... Here's what I'm trying to do for a
I am a newbie to AS400-Java programming. I am trying to create my first
I'm a complete newbie to Windows and COM programming, trying to use com4j in
I'm a newbie to scala. I'm trying an example from the book Programming Scala.
I'm newbie to Game programming. I'm trying to develop a simple shooting game. The
guys, I'm a programming newbie trying to improve the procedure bellow in a way
Am a newbie to iOS programming. This is what am trying to do: The
Hi I'm a newbie in Android Programming. I'm trying to build an activity which
I'm a newbie both on event/callback style programming and NodeJS. I'm trying to implement

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.