loop for summing across an array

This is a discussion on loop for summing across an array within the C++ Programming forums, part of the General Programming Boards category; I'm using a ADMB for nonlinear modelling based on C++ language Say I have this matrix : index# age indicator ...

  1. #1
    JRo
    JRo is offline
    Registered User
    Join Date
    Dec 2007
    Posts
    3

    loop for summing across an array

    I'm using a ADMB for nonlinear modelling based on C++ language

    Say I have this matrix :
    index# age indicator
    1 0 0
    2 0 0
    3 1 0
    4 1 0
    5 2 0
    6 2 1

    I want to get it into
    age positives(=sum of indicator values for each age class) total(=count of indivivuals in each age class)

    0 0 2
    1 0 2
    2 1 2

    I have not been able to get a loop to work. any suggestion appreciated. thanks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,594
    What did you try?
    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
    JRo
    JRo is offline
    Registered User
    Join Date
    Dec 2007
    Posts
    3
    Code:
    {
    	int i;
    	minage=min(column(data,2));
    	maxage=max(column(data,2));
    	
    	for(i=minage;i<=maxage;i++)
    	{ 
    		new(i)=sum(data(i,indicator));
    	}
    	//cout<<new<<endl;
    		
    	
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,594
    new is a C++ keyword, so at a glance that will not work.

    There is a lazy approach: create two maps, one to map age to positives, the other to map age to total. Loop through the matrix sum the positives and increment totals for each age. When you are done, the two maps contain just the results that you need.
    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

  5. #5
    JRo
    JRo is offline
    Registered User
    Join Date
    Dec 2007
    Posts
    3

    maps

    that explains one of my error messages. I need the data in a single matrix form for input into another loop(I have this one working) to estimate pred pos for each age class.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Have problems with copying my array!
    By AvaGodess in forum C Programming
    Replies: 11
    Last Post: 09-25-2008, 12:56 AM
  2. loop the loop - feeling foolish
    By estos in forum C Programming
    Replies: 2
    Last Post: 04-07-2007, 02:45 AM
  3. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  4. Initializing a 3D array w/o a loop.
    By funkydude9 in forum C++ Programming
    Replies: 5
    Last Post: 03-04-2004, 10:20 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM

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