Thread: loop for summing across an array

  1. #1
    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
    28,413
    What did you try?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    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, 11:20 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM