Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,576 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,885 people online right now. Registration is fast and FREE... Join Now!




Matrix Addition and Multiplication

 
Reply to this topicStart new topic

Matrix Addition and Multiplication, Need help....

B@LL!ST!C
27 Sep, 2008 - 07:33 AM
Post #1

D.I.C Head
**

Joined: 20 Aug, 2007
Posts: 152


My Contributions
Whew! it good to be back.... been quite gone for a long time..... and being back means i need help guys... as the topic says, i need help in my codes... can't get it right.... i still haven't done the multiplication part cause i can't even get the addition right.... need help please smile.gif

CODE

#include <stdio.h>
int a,b,c,d,e,z,i,j;
int x[50];
int y[50];
char asdf;
main()
{
asdf:
    printf("Enter Number of Matrices: ");
    scanf("%d",&a);
    printf("\n");


    if (a<2)
    {
        printf("Matrices Should Not Be Lower Than 2\n\n");
        goto asdf;
    }

    c=a*a;
    
    for (b=0;b<c;b++)
    {
        
        printf("Enter Element 1: ");
        scanf("%d",&x[b]);
    }
    printf("\n");
        
    for (e=0;e<c;e++)
    {
        printf("Enter Element 2: ");
        scanf("%d",&y[e]);
    }
    
    printf("\n");
    printf("*****Choose*****\n\n");
    printf("Press 1 for Add \n");
    printf("Press 2 for Multiply \n");
    printf("Enter Choice: ");
    scanf("%d",&d);
    printf("\n");


    if (d==1)
    {
        for(i=0;i<x[b];i++);
        {
            for (j=0;j<y[e];j++);
            {
                printf("\n");
                z=x[i]+y[j];
                printf("%d\t",&z);
            }
        }
        
    }
    return 0;
}


This post has been edited by B@LL!ST!C: 27 Sep, 2008 - 07:36 AM
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Matrix Addition And Multiplication
27 Sep, 2008 - 07:55 AM
Post #2

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 8,997



Thanked: 125 times
Dream Kudos: 8625
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Are you getting an error? What does the code do versus what you need/want it to do?
User is offlineProfile CardPM
+Quote Post

B@LL!ST!C
RE: Matrix Addition And Multiplication
27 Sep, 2008 - 08:02 AM
Post #3

D.I.C Head
**

Joined: 20 Aug, 2007
Posts: 152


My Contributions
QUOTE(PsychoCoder @ 27 Sep, 2008 - 08:55 AM) *

Are you getting an error? What does the code do versus what you need/want it to do?


i dont get errors.... the problem is the answer is not correct... the answer i get is 4348072... which i think should be like if i add the 1st matrix "1" and 2nd matrix "2" the answer should be 3.... something like that....

here an example of what happens:
IPB Image

User is offlineProfile CardPM
+Quote Post

B@LL!ST!C
RE: Matrix Addition And Multiplication
27 Sep, 2008 - 08:06 PM
Post #4

D.I.C Head
**

Joined: 20 Aug, 2007
Posts: 152


My Contributions
Anybody? Help please....
User is offlineProfile CardPM
+Quote Post

B@LL!ST!C
RE: Matrix Addition And Multiplication
27 Sep, 2008 - 11:21 PM
Post #5

D.I.C Head
**

Joined: 20 Aug, 2007
Posts: 152


My Contributions
Ok I already got the addition part.... now my problem is the multiplication part.... can anybody pls check of whats wrong with the codes???
CODE

#include <stdio.h>
int a,b,c,d,e,i,j,l,v;
int x[50][50];
int y[50][50];
int z[50][50];
char asdf;
main()
{

    printf("Enter the Size of Matrix");
    printf("\nRows: ");
    scanf("%d",&a);
    printf("Columns: ");
    scanf("%d",&b);
    printf("\n");
    
    printf("Enter Element 1: \n");
    for (c=1;c<=a;c++)
    {
        for (d=1;d<=b;d++)
        {
            scanf("%d",&x[c][d]);
        }
    }
        
    printf("Enter Element 2: \n");

    for (c=1;c<=a;c++)
    {
        for (d=1;d<=b;d++)
        {
            scanf("%d",&y[c][d]);
        }
    }
    
    printf("\n");
    printf("*****Choose*****\n\n");
    printf("Press 1 for Add \n");
    printf("Press 2 for Multiply \n");
    printf("Enter Choice: ");
    scanf("%d",&l);

    printf("\n");

    if(l==1)
    {

    for (c=1;c<=a;c++)
    {
        for (d=1;d<=b;d++)
        {
           z[c][d]=x[c][d]+y[c][d];
            
           printf ("%6d ",z[c][d]);
        }

        
        printf ("\n\n");
    }
    
    }
    else
    {
        for (c=1;c<=a;c++)
        {
            for (d=1;d<=b;d++)
            {
                z[c][d]=0;
                for(v=1;v<=b;v++);
                {
                    z[c][d]=z[c][d]+(x[c][v]*y[v][d]);
                    printf("%d",&z[c][d]);
                    printf("\n\n");
                }
            }
        }
    }



        return 0;
}

User is offlineProfile CardPM
+Quote Post

baavgai
RE: Matrix Addition And Multiplication
28 Sep, 2008 - 04:07 AM
Post #6

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,031



Thanked: 105 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
You're trying to do the calculations and print it at the same time? You can get away with that with a addition, but not multiplication.

Write a function that prints your two dimensional array. Do your calculation, then call that. Meaningful variable names and a few functions, rather than trying to cram it all in one, would probably also make your job easier.

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/3/08 12:10AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month