How to sum up all members of array ?

This is a discussion on How to sum up all members of array ? within the C++ Programming forums, part of the General Programming Boards category; hi. I have written a code to calculate the total of the members of an array named "arr". It doesnt ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    1

    How to sum up all members of array ?

    hi. I have written a code to calculate the total of the members of an array named "arr". It doesnt work

    Im sure it is easy so please tell we where im doing wrong:


    Code:
    #include <cstdlib>
    #include<iostream>
    using namespace std;
    
    
    int summ(int *p){
        int res=0;
        while(*p!=NULL){
            res+=*p;
            p++;
            cout << "res="<<res<<"   p="<<*p<<endl;
        }
        return res;
    }
    int main(int argc, char** argv) {
        
        int arr[4]={5,1,2,6};
        
    //    int size=sizeof(arr)/sizeof(int);
    //    for(int i=0;i<size;i++){
    //        arr[i] = arr[i]*2;
    //    }
    //  
    //    cout << "size = " << size<<endl;
        
        int v=summ(arr);
        cout << v;
        
    }

    When i uncommend the part above, it works but i wanna learn how to pass a pointer in functions. So thx a lot in advance.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,341
    int size=sizeof(arr)/sizeof(int);
    int v=summ(arr,size);


    Pass the size of the array as an additional parameter.

    You can't tell the size of an array inside a function just by looking at it.
    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.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,180
    Code:
    #include <cstddef>
    #include <iostream>
     
    template<std::size_t N>
    int summ(int (&p)[N])
    {
    	int res = 0;
    	for (std::size_t i = 0; i < N; i++)
    		res += p[i];
    	return res;
    }
    
    int main(int argc, char** argv)
    {
    	int arr[] = { 5, 1, 2, 6 };
    	int v = summ(arr);
    	std::cout << v;
    }
    C++ does not add anything to the end of an array, so you can't just wander through it and expect some magical value at the end. There isn't one.
    Furthermore, even if there was one, it wouldn't be NULL. That's for pointers, and pointers only. Yet, you shouldn't be using NULL anyway, you should be using nullptr for pointers.
    Size of the array is not needed if initialized.
    Last edited by Elysia; 11-09-2011 at 01:06 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accessing array members
    By kkk in forum C Programming
    Replies: 1
    Last Post: 06-05-2011, 01:56 PM
  2. Trouble Accesing Array members
    By bman900 in forum C Programming
    Replies: 4
    Last Post: 11-06-2010, 08:06 AM
  3. Static array of initialized members?
    By Blackroot in forum C++ Programming
    Replies: 4
    Last Post: 04-13-2009, 09:36 PM
  4. placing struct members into an array
    By droseman in forum C Programming
    Replies: 5
    Last Post: 01-27-2009, 08:18 AM
  5. Treat members as an array? (opengl)
    By Zen_id3b in forum C++ Programming
    Replies: 12
    Last Post: 01-14-2005, 08:04 AM

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