Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

C++ matrix

Posted on 2001-06-02
8
Medium Priority
?
275 Views
Last Modified: 2008-02-01
I've multiplied 2 matrices
eg
6 1.5       8 7
4 1     *   6 4

my results were
57 48
38 32

what i am trying to do now is find the average of
57 + 48 = 105 / 2 = 52.5 and
38 + 32 = 70 / 2 = 35

any ideas?
0
Comment
Question by:bling2000
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 3

Expert Comment

by:SEve
ID: 6149917
so what the problem???
0
 
LVL 30

Expert Comment

by:Axter
ID: 6149929
To find the average you just add the items, and devide them by the quantity of items.

52.5 + 35 = 87.5
87.5 / 2 = 43.75
The average is 43.75

Sum(items)/QtyItems=Average
0
 
LVL 22

Expert Comment

by:nietod
ID: 6150192
What exactly is your question?    Obvipously you know how to do averages already as that is in the question.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:bling2000
ID: 6150641
sorry maybe i wasn't so clear on what i wanted

for instance after i mulitply the two matrices i end up with

57 48
38 32

and lets say that these numbers fit into sales figure chart

 north region
plant x  plant y
57        48       roller blades
38        32       golf clubs

what i want for my results is
57 + 48 = 105 / 2 = 52.5 and
38 + 32 = 70 / 2 = 35

so my sales chart will look like this

 north region
plant x  plant y
57        48       roller blades
38        32       golf clubs

northern region average cost roller blades $52.5
northern region average cost golf clubs $35

thanks i hope this helps clear any confusion
0
 
LVL 22

Expert Comment

by:nietod
ID: 6150647
Without more information we really can't help.  
How is the data recorded in the program?  In an array?  In an array class?  in seperate variables?  will there always be two numbers to average?

The general idea is you add yup the number to be averaged, then divide them by the number of items you are averaging. like

int Array[5] = {10,11,12,13,14};
int ArraySize = 5;
int Average = 0;

for (int i = 0; i < AraySize; ++i)
   Average += Array[i];
Average /= ArraySize;
0
 

Author Comment

by:bling2000
ID: 6151163
this is my class

class Matrix
{
     private:
          int size;
          float Val[50][50];
     public:
          void MakeEmpty(int n);
          void StoreValue(int, int, float v);
          Matrix operator-(Matrix &mat1);
          Matrix operator+(Matrix &mat1);
          Matrix operator*(Matrix &mat1);
          void PrintMatrix();
          void PrintHeader();
};

EastHour.MakeEmpty(2);
EastCash.MakeEmpty(2);
WestHour.MakeEmpty(2);
WestCash.MakeEmpty(2);

these above statements call the function makeempty which initializes the matrix

void Matrix::MakeEmpty(int n)
{
     size = n;
     for(int i=0; i < size; i++)
     for(int j=0; j < size; j++)
          Val[i][j] = 0.0;
}

EastHour.StoreValue(0,0,6);
EastHour.StoreValue(0,1,1.5);
EastHour.StoreValue(1,0,4);
EastHour.StoreValue(1,1,1);

EastCash.StoreValue(0,0,8);
EastCash.StoreValue(0,1,7);
EastCash.StoreValue(1,0,6);
EastCash.StoreValue(1,1,4);

then these above statements call the the function storevalue to store values

void Matrix::StoreValue(int i,int j, float v)
{
Val[i][j]=v;
}

then i call my overload multiply fuction
result = eashhour * eashcash
in my function i initialize my result(result.makeempty)
then after my for loops i return result which = 57 48 38 32

the problem is that my array(matrix) now contains 57 48 38 32 and what i want to do is get the average of 57 and 48(52.5), then get the average of 38 and 32(35).
0
 
LVL 22

Expert Comment

by:nietod
ID: 6151228
I assume this is homework.   We can't give you answers to homework, but we can give you assistance, like your teacher woudl do.

Your matrix has only one varaible, "size", to control the dimensions.  That doesn't seem very logical as then you have to have square matrixes.   You probably want to have two varaibles that control the size.

The logic of how you calculate the average is exactly like I posted above.  i.e. like

float Average = 0;

for (int i = 0; i < Size; ++i)
   Average += Val[0][i];

Average /= Size;

which calcualtes the average for the first row.  However, I'm not sure how you want to employ that in your class.  The probelm is that you have multiple rows, so there are multiple values to be calculated.  I'm not sure how you want to store them or how you want to design your interface so that only a specific one would be calculated.  Perhaps you want to do something like

float Matrix::CalcAverage(int RowNumber);

where RowNumber indicates which row to calculate for.
0
 

Accepted Solution

by:
bhargav052901 earned 200 total points
ID: 6151462

 hai,

   you declared as two dimensional maitrx. so easily you can get the answer. in your class you declared a variable
"size". will it specify the matrix should be square matrix.
 

 after you got the product of that matrix.
 
declare a matrix    "a[size][1]" to pop your answer into
  that matrix.


  for(i=0;i<size;i++)
   {
    for(j=0;j<size;j++)
     {
       x+=b[i,j];
      }
    a[i,0]=x/size;
   }
       
       
 i think you got.

bhargava
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question