your opinions???

This is a discussion on your opinions??? within the C++ Programming forums, part of the General Programming Boards category; Hi.. In my simple program, i want to accept array of numbers of any size and have to return its ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    17

    your opinions???

    Hi..
    In my simple program, i want to accept array of numbers of any size and have to return its sum.
    Whether i should accept that array in the defined function of sum which is:
    Code:
    void sum(int ar[], const int size)
    or this accepting array should be in main??



    ~~>Please i'm new to c++

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,754
    If by "accept" you mean "read input so as to populate the array", then I would say that the code for that should not be in the sum function. Perhaps it could go into the main function, or perhaps you could create another function for that.

    By the way, since it looks like you are going to use a dynamic array, look into the use of a std::vector<int> instead.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    17
    Hello laserlight..
    Yes i want to get input from the user, and i wrote the code as you said that it should be in the main function.
    This is what i've done yet.

    Code:
    #include <iostream.h>
    #include <conio.h>
    void sum(int a[],const int size);
    void main()
    {
       int ar[];
       const int size;
       for(int i=0 ;i<size; i++)
       {
        cin>>ar[i];
        }
         sum();
         getch();
        }
    
    
    void sum(int a[], const int size)
    {
     int s=0;
     for(int i=0;i<size;i++)
     {
      s=s+a[i];
      }
      cout<<s;
      }
    And i'm getting errors in it.
    Will you tell what should i do?

    Where i'm calling function "sum()" i've a confusion here.
    Last edited by Aash; 08-24-2010 at 05:48 AM.

  4. #4
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425

    no size

    You should really use vectors here as suggested, but in any case you declare your const int size, but it is not assigned any value, also in sum() you are creating a local variable anyway, you are not passing your original array in, so even if it did have any size and contents in main(), this would be irrelevant in sum()
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    17
    thanks for the reply rogster001
    What if i assigned arrays size of 5 and after summing in the sum function, can i call it in main as sum(ar[5])??
    Can i do it in this way if i don't want to use vectors??

    Code:
    #include <iostream.h>
    #include <conio.h>
    int sum(int a[5]);
    int main()
    {
       int ar[5];
    
       for(int i=0 ;i<=5; i++)
       {
        cin>>ar[i];
        }
        sum(ar[5]);
         return 0;
        }
    
    
    int sum(int a[5])
    {
     int s=0;
     for(int i=0;i<=5;i++)
     {
      s=s+a[i];
      }
         return s;
      }
    Last edited by Aash; 08-24-2010 at 06:47 AM.

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,709
    Well there are two problems with your understanding of arrays. First, you wrote for (i = 0; i <= 5; i++) as the loop, which will process six elements when there are only five. Recall that arrays are numbered from 0 to n-1 in C++, so perhaps you can fix your array accordingly.

    Second, sum(arr[5]); is very wrong. arr[5] is an expression which evaluates to a single integer. Not only that but if we look at the prototype for sum, it is this:

    Code:
    int sum(int a[5]);
    Did you know that is equivalent to this?

    Code:
    int sum(int *a);
    Basically 'a' is a pointer that you will use to access all of arr in main. There is no real information about the length of the array being passed to this function, and the compiler will not make sure that an array of five elements is being passed. When dealing with functions that process arrays, the usual thing is to pass a pointer and a length variable.

    If you're not going to use more than five elements or so though, there's nothing wrong with what you have. Arrays are not dynamic structures. What you do is use new[] and delete[] to create dynamic arrays with pointers. But C++ also has an even easier alternative called vector.

    Code:
    while (cin >> temp && temp != -1) myvector.push_back (temp);
    int theSum = sum (reinterpret_cast<int*> (&myvector[0]) , myvector.size ());
    And you've got an array sum(int *a, size_t size) can work with, except that vector manages the array's memory and cleans up automatically after itself.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    17
    thanks..
    I think i've to grip on my concepts. I've tried book "How to program C++" by deitel (6th edition)
    would you please tell me which one is the best book for C++??
    so that i can clear all my concepts about functions,vectors e.t.c as i've not used vectors before. I'm new for this.
    any help will be appreciated..

  8. #8
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,709
    I've heard of the Deitel book, so just read what you have. I know it covers vectors, so you might want to read ahead to where the book starts covering it. If you're interested in other books or what we think about your book, please search in the C++ Book Recommendations thread.

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    > #include <iostream.h>
    > #include <conio.h>
    Lemme guess, TurboC user right?

    You know, this is well over a decade past being obsolete.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    17
    hi..
    I'm using Borland C++ 5.02 as recommended to me in my college. I'm in third semester and i'm new to c++. I shouldn't use it??
    Will you please guide me Salem??

  11. #11
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425
    if your course is structured around the borland then its hard to tell you to change but you may become reliant on library functions that you wont find elsewhere as you advance and portability issues also as i understand it, maybe it was suggested as its free but its over ten years old and there are better free alternatives like my pref codeblocks but you will find others in help pages
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    If you begin a program with
    Code:
    #include <iostream>
    using namespace std;
    Does it complain at all?

    If it does complain, then you might want to ask your tutors why you're being taught something which is obsolete.

    One of the points of going to college is to come out at the front of the technology curve, not way behind.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  13. #13
    Registered User
    Join Date
    May 2010
    Posts
    17
    @rogster001: Ok. I want to ask you about best book for c++ for the newbie like me.
    Is Summita Arrora's C++ book is best one for absolute beginners??
    I urgently needed the best book for C++ because i think i'm totally ignorant to C++.

    @Salem: My teacher didn't tell me to use using namespace std;. I just add #include <iostream.h> and #include <conio.h>.
    Last edited by Aash; 08-25-2010 at 10:55 AM.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,754
    You can read the book recommendation thread, but for a beginner I would recommend either Accelerated C++ by Koenig and Moo or You Can Do It!: A Beginners Introduction to Computer Programming by Glassborow.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    May 2010
    Posts
    17
    @laserlight: Thankyou. I'll try both..

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Opinions requested
    By jeffcobb in forum Programming Book and Product Reviews
    Replies: 8
    Last Post: 12-26-2009, 03:30 PM
  2. Replies: 6
    Last Post: 10-31-2009, 04:10 AM
  3. Opinions?
    By Decrypt in forum C++ Programming
    Replies: 1
    Last Post: 09-18-2005, 05:29 PM
  4. Favors and Opinions
    By sean in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 08-01-2003, 10:04 PM
  5. Need some opinions
    By Fool in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 12-17-2001, 06:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21