Avatar of tetoo
tetoo asked on

counting in 2 dimintanal array

I have 2 dimintanal array n * n that got ones and zeroes stored in this array I want to count number of ones in each row and store this numbers in one dimintion array how can I do this I tried to do that like this but didnt work

can you help me with this issue thanks .
int counter;
for(int d=0;d<n;d++)
counter=0; 
 for(int e=1;e<n;e++)		   
   {
     if(connection[d][e]==1)			
	counter++;
         nodeDgree[d]=co;
   }

Open in new window

C++

Avatar of undefined
Last Comment
itsmeandnobodyelse

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
LordOfPorts

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SOLUTION
itsmeandnobodyelse

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
tetoo

thanks guys that was really helpfull ..
itsmeandnobodyelse

A split is fine with me (I would have accepted any other decision as well).

>>>> If you object to this action, you have until 5/31/2008 to post a comment describing your objection.

Don't think you need to wait so long ... ;-)

Regards, Alex
evilrix

Try using std::count, it'll simplify your solution
http://www.cplusplus.com/reference/algorithm/count.html

#include <algorithm>
#include <iostream>
 
int main()
{
	// Test data
	int a[5][5] = {{1,1,0,0,1},{1,0,0,1,1},{0,0,0,0,1},{1,1,0,1,1},{1,0,0,1,0}};
 
	// This is the bit that does the work...
	int cnt[5] = {0};
	for(size_t i = 0 ; i < 5 ; ++i)
	{
		cnt[i] += std::count(a[i], a[i]+5, 1);
	}
 
	// Display result for example purposes
	for(size_t i = 0 ; i < 5 ; ++i)
	{
		std::cout << cnt[i] << std::endl;
	}
}

Open in new window

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Infinity08

Just a question :

>>  for(int e=1;e<n;e++)

Is it intentional that you start at index 1 here, instead of 0 ?
itsmeandnobodyelse

>>>> please do not post after the 'self-close' process has started.
????
Sorry, I've never seen a 'self-close' process before ... but actually isn't it some kind of overkill for a question with two answers only? Again sorry, if I was going to spoil your - slowly - self-closing process once more  ;-) Is it some kind of meditation?