Thread: Calculate the sum of all integers in an array using a function and return the sum?

  1. #16
    Registered User
    Join Date
    Oct 2016
    Posts
    17
    Quote Originally Posted by Xupicor View Post
    Ouch!
    Yes, that seems to be pretty common reaction. I just don't understand why book authors and class teachers keep teaching C++ that way if it's a bad idea. Very few people have said that it's a good idea to start out in C++ this way.

    Quote Originally Posted by Xupicor View Post
    I mean, sure, do learn how bare arrays and pointers work. You want to know that. However, switch to modern idioms as soon as you can - maybe a more modern book would suit you better. std::vector, std::string, algorithms, etc, are pretty cool.
    Aren't all programs just algorithms? I get the feeling that only some specific kind of programs that solve a common type of problem are referred to as algorithms. They usually would have a fancy name too, like bubble sort, linear search, binary search, etc.

    So the kind of algorithms that you're referring to here are the ones that are already implemented in C++ standard library, that are ready and waiting to be used? I know I had to implement binary search recently in my program. I read a bit about it, looked at some pseudo code and managed to get it working. But then I came across articles discussing how to use binary search from the library. This is the kind of algorithms you're referring to here, right?

    I don't think I'm ready for this just yet. But I will be exploring it soon enough. The text book I have right now ain't much. It's like a tour more than anything, and it's very thin. It's less than 300 pages, and a lot of that has gone to pictures, illustrations, and some code examples. But I have purchased the PPP book by Bjarne. I can't wait to sink into it. I will read it page by page and not skip anything. I hope to finish it by the next October. I think it will take me that long. I don't expect programming to be easy, and especially not with C++. But I am a patient person.

    Quote Originally Posted by Xupicor View Post
    Yes, it's required. Mostly. But there is a way to have it done with limited max amount of parameters, similar to how you envisioned it in that post - and not use the variadic function. To do that you'd use default arguments or function overloading. With the former you have one function that has many default arguments, with the latter you have many functions with different number of arguments.
    I can barely handle one function! If I had as many functions as I have arguments and they were all like nested and everything, my head would explode! The former sounds easier. Besides, that sounds a lot like the way command line tools work where some of them will do stuff even if you don't specify any arguments (options or flags). They just assume some defaults. Is that how this works too?
    Last edited by cozySam; 10-09-2016 at 05:05 PM.

  2. #17
    Registered User
    Join Date
    Oct 2016
    Posts
    17
    Quote Originally Posted by MutantJohn View Post
    So accumulate just takes the beginning and end of the range, sums it all up and then returns it to you. std::vector handles all the things like allocation of memory and whatnot. You can also use std::array with std::accumulate as well! This is where you can really start to see the fundamental power in C++ of iterators and algorithms implemented in terms of iterators.
    You probably meant to say this:

    This is where you can really start to see the fundamental power of iterators in C++ and the algorithms implemented in terms of iterators.
    But yeah, that is pretty useful and cool stuff! I know I can learn it, but I want to make sure to take it in baby steps. I don't want to jump in the deep end of the pool before learning to swim. I understand some parts of it but not all of it. Thanks for showing this to me though! It's always interesting to see how other people solve the same problem.

    However, I don't mean to always use consecutive numbers like 1, 2, 3, 4, 5 in my array, or list, or whatever you want to call it. This was just a practice example for me, I may go a different direction later on, and maybe use a difference sequence of numbers. Would accumulate work for something like 1, 2, 4, 6, 8, and so on? I was unable to test it because I got an error on line 11 (expected nested-name-specifier before 'integral').

    I don't mean to ask too many questions about this and divert too much from the topic. We can leave some of that juicy stuff for later!

  3. #18
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Aren't all programs just algorithms?
    We're getting a bit philosophical here, but if a program is just one big algorithm solving a bigger (if well stated) goal, then that program can be composed of smaller algorithms with smaller goals. Bubble sort will put an arrays elements in order by a particular series of steps. It is also true that bubble sort is slower on average than many sorts. That isn't to say that it is bad, but bubble sort has steps and qualities unique to it. So it can be more productive to think of an algorithm, especially a named algorithm, as a specific way to achieve a general, small goal, like searching and sorting. You'll learn a lot of algorithms and invent your own in the course of your study.

    So the kind of algortihms that you're referring to here are the ones that are already implemented in C++ standard library, that are ready and waiting to be used?
    Yes. The great thing about it is that you don't have to write your own if you use the STL versions. It is practically guaranteed to work as long as it is written correctly, all other things being equal.

    Besides, that sounds a lot like the way command line tools work where some of them will do stuff even if you don't specify any argments (options or flags). They just assume some defaults. Is that how this works also?
    I do enjoy that you think of it that way, but no. It all comes down to an array of strings that main() receives. Inside of the program, the array is parsed, converted, bleeped, blorped or whatever in order to make the magic happen. Defaults are generally defaults only because main() was programmed that way.

    If you are interested in learning command line arguments now, read something like this: A Little C Primer/C Command Line Arguments - Wikibooks, open books for an open world

    Now, some of the code on that page might look like a clusterfluck. It can be helpful to write functions to work with argv and argc. As a source of inspiration, there are cool functions or classes out there to help people code what they want out of main(), like getopt(). Unfortunately, getopt() is platform dependent though... as cool of a function it is, Windows does not support the POSIX standard. Otherwise it would have it.

  4. #19
    Nasal Demon Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    179
    Indeed, by "algorithms" I meant the standard <algorithm> header and its goodies, and by extension the rest of the more modern parts of C++ standard library you'd in the past address as STL (standard template library). Sorry for the confusion, I assumed it was obvious, but I can well understand why it wasn't. My bad.

    An example of a function with default arguments:
    Code:
    #include <iostream>
    
    int sum5(int v1 = 0, int v2 = 0, int v3 = 0, int v4 = 0, int v5 = 0) {
        return v1 + v2 + v3 + v4 + v5; // possible overflow... oh who cares
    }
    
    int main() {
        using std::cout;
        cout << sum() << "\n"; // 0
        cout << sum(1, 1, 1, 1); // 4
        cout << sum(1, 2, 3, 4, 5, 6); // error! too many arguments!
    }
    It works, but the usefulness of that particular piece of code is... Debatable. ; ) Default parameter values are useful from time to time, but as with anything fun - be careful not to overuse it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 01-10-2016, 01:23 AM
  2. Replies: 3
    Last Post: 03-26-2012, 10:50 AM
  3. How to return an array from a function
    By millsy5 in forum C Programming
    Replies: 3
    Last Post: 01-26-2010, 11:17 AM
  4. Program to calculate change return.
    By OrAnGeWorX in forum C Programming
    Replies: 15
    Last Post: 11-17-2008, 09:49 AM
  5. function return array
    By dukysta in forum C Programming
    Replies: 4
    Last Post: 06-10-2007, 09:49 AM

Tags for this Thread