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 7550535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:14:18+00:00 2026-05-30T10:14:18+00:00

lets say I want to iterate through an array of doubles and sum them.

  • 0

lets say I want to iterate through an array of doubles and sum them. I have two ways to do this.

A)

double sum (double * series, int size) {
    double sum = 0.0;
    for (int i = 0; i < size; i++) {
        sum += *series++;
    }
    return sum;
}

B)

double sum (double * series, int size) {
    double sum = 0.0;
    for (int i = 0; i < size; i++) {
        sum += series[i];
    }
    return sum;
}

which is better and why / when should I use one over the other.

  • 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-05-30T10:14:19+00:00Added an answer on May 30, 2026 at 10:14 am

    This is a question of readability, it should not affect performance. I think B is the most readable, and therefore preferable.

    I could also propose a third variant, which is range-based (note the begin and end parameters):

    double sum (double* begin, double* end) {
        double sum = 0.;
        for (double* it = begin; it != end; ++it) {
            sum += *it;
        }
        return sum;
    }
    

    This is idiomatic C++ in many cases and generalizes more easily. That is not to say that it is always preferable, it is just another variant in a question about readability and maintainability.

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

Sidebar

Related Questions

Let's say I have a bunch of items and I want to iterate through
Lets say I want to sort some items and need to sort them by
I would appreciate any help on this issue. Lets say I want to load
Lets say I have a lat lng coordinate and I want to place that
Lets say I have the following two PHP arrays that contain integers: $foo =
Lets say I have a list: listOfStuff =([a,b], [c,d], [e,f], [f,g]) What I want
I have two objects that are derived from same the base class. Lets say
lets say I have a vector of pair<int,int> . Now I want to extract
Lets say I want to implement a smart pointer a_ptr which can be compared
Lets say I want to define 2 styles. .color_red { color: Red; } .banner

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.