C++/c++

Advertisement


Question
how can i check the sum of all rows are equal in matrix. tell me code.

Answer
Hi Rabia

int a[][]; // where a is the two dimensional array
int counter, sumOfFirstRow;
int flag = 1;
for(int i = 0 ; i < n ; i++) // where n is the number of rows
{
   counter = 0;
   for(int j = 0 ; j < m ; j++) // where m is the number of columns
   {
       counter += a[i][j] //adding up the value of the row in a variable
   }

   if(i == 0){//Save the sum of the first row for future matching
       sumOfFirstRow = counter;
   }else if(counter != sumOfFirstRow){//check from the second to last
         //row with the sum of first row
        flag = 0; // if mismatched flag will turn to 0 from 1
   }
}

if(flag == 1)
cout<<"Sum of all the rows are same";
else
cout<<"Sum of all the rows doesnot match";

Please check the algorithm in case of any problem please feel free to get back to me.
You can also check for such proigrams in my personal website http://www.scodz.com

regards
Joydeep Bhattacharya
http://www.scodz.com

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Joydeep Bhattacharya

Expertise

TSR, Socket and Very Complex Mathematical Problem

Experience

Worked on many OOP projects with design patterns

Organizations
http://www.scodz.com

Publications
http://www.scodz.com

Education/Credentials
Master of Computer Applications

Awards and Honors
Being a proud owner of a website of programming called http://www.scodz.com

Past/Present Clients
http://www.funforu.com http://www.dhoondho.com http://analysingc.50webs.com

©2016 About.com. All rights reserved.