Thread: Adding multidimensional arrays

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    8

    Question Adding multidimensional arrays

    Hello y'all:
    I have been given a problem regarding matrices. Im very new to this but I know that multidimensional arrays cannot be dynamic, so i set mine as follows...oh hell, heres my whole program...i think i have the basics, but i believe im screwing up when it comes to adding the actual numbers, i get an output of zero for everything i choose.
    Any help would be appreciated, thanks in advance...
    #include <iostream.h>
    #include <math.h>

    void choices1(int row1,int row2, int col1, int col2);
    void add(int row1,int row2,int col1,int col2);
    void sub(int row1,int col1,int row2,int col2);
    void mult(int row1,int col1,int row2,int col2);

    int main()
    {
    int choice,row1,row2,col1,col2,add1;

    cout<<"Please enter the number of rows in matrix 1:"<<endl;
    cin>>row1;
    cout<<"Please enter the number of columns in matrix 1:"<<endl;
    cin>>col1;
    cout<<"Please enter the number of rows in matrix 2:"<<endl;
    cin>>row2;
    cout<<"Please enter the number of columns in matrix 2:"<<endl;
    cin>>col2;
    cout<<"Matrix 1 is a "<<row1<<" by "<<col1<<" matrix "<<endl;
    cout<<"Matrix 2 is a "<<row2<<" by "<<col2<<" matrix "<<endl;

    int A[20][20];
    int B[20][20];
    int C[20][20];
    int D[20][20];


    cout<<"Please enter the numbers in matrix 1, starting at top left"<<endl;
    cout<<"and moving to the right"<<endl;
    for (int i=0;i<row1;i++)
    for (int j=0;j<col1;j++)
    cin>>A[i][j];

    cout<<"Please enter the numbers in matrix 2, starting at top left"<<endl;
    cout<<"and moving to the right"<<endl;
    for (int k=0;k<row1;k++)
    for (int l=0;l<col1;l++)
    cin>>A[k][l];

    choices1(row1,row2,col1,col2);
    cin>>choice;

    if(choice=1)
    add(row1,col1,row2,col2);
    cout<<C[row1][col1];
    choices1(row1,col1,row2,col2);
    cin>>choice;

    if(choice=2)
    sub(row1,col1,row2,col2);
    cout<<C[row1][col1];
    choices1(row1,col1,row2,col2);
    cin>>choice;

    if(choice=3)
    mult(row1,col1,row2,col2);
    cout<<D[row2][col1];
    choices1(row1,col1,row2,col2);
    cin>>choice;

    return 0;
    }

    void choices1(int row1,int row2, int col1, int col2)
    {

    if (row1==row2 && col1==col2 && col1==row2)
    {
    cout<<"Addition, Subtraction, and Multiplication are available"<<endl;
    cout<<"Enter 1 for Addition"<<endl;
    cout<<"Enter 2 for Subtraction"<<endl;
    cout<<"Enter 3 for Multiplication"<<endl;
    }
    else if (row1==row2 && col1==col2 && col1!=row2)
    {
    cout<<"Only Addition and Subtraction are available"<<endl;
    cout<<"Enter 1 for Addition"<<endl;
    cout<<"Enter 2 for Subtraction"<<endl;
    }
    else if (row1==row2 && col1==row2)
    {
    cout<<"Only Multiplication is Available"<<endl;
    cout<<"Enter 3 for Multiplication"<<endl;
    }
    else
    cout<<"Try Again"<<endl;
    }

    void add(int row1,int row2,int col1,int col2)
    {
    int A[row1][col1];
    int B[row2][col2];
    int C[row1][col1];

    for (int i=0;i<row1;i++)
    for (int j=0;j<col1;j++)
    {
    C[i][j]=A[i][j]+B[i][j];
    }
    }

    void sub(int row1,int col1,int row2,int col2)
    {
    int A[row1][col1];
    int B[row2][col2];
    int C[row1][col1];

    for (int i=0;i<row1;i++)
    for (int j=0;j<col1;j++)
    {
    C[i][j]=A[i][j]-B[i][j];
    }
    }

    void mult(int row1,int col1,int row2,int col2)
    {
    int sum;
    int A[row1][col1];
    int B[row2][col2];
    int D[row2][col1];

    for(int i=0;i<row2;i++)
    for(int j=0;j<col2;j++)
    {
    sum=0;
    for(int k=0;k<col1;k++)
    sum+=A[i][k]*B[k][j];
    sum=D[i][j];
    }
    }

  2. #2
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    but I know that multidimensional arrays cannot be dynamic, so i set mine as follows...
    No, they can be. What you've done is not legal C++, however some compilers allow it because it's in the latest C standard. To create dynamic multi dimensional arrays you can do this -

    Code:
    int ** ptr;
    	int x,y;
    
    	cin >> x >> y;
    
    	ptr=new int*[x];
    
    	for(int i=0;i<x;i++)
    			ptr[i]=new int[y];
    
    	//Do stuff with arrays
    
    	for(int j=0;j<x;j++)
    		delete [] ptr[j];
    
    	delete ptr;

    you can then access the elements like a multi dimensional array. Alternatively you could use STL vector.

    If you want to add values in these arrays you have to pass these arrays into your functions. So your add function declaration should be something like

    void add(int** first,int** second,int** result,int row1,int row2,int col1,int col2);

    If you do something like this you'll have to allocate memory for an array to hold the result(you don't want local arrays within your functions).
    Last edited by zen; 11-13-2001 at 03:36 PM.
    zen

  3. #3
    Registered User Aran's Avatar
    Join Date
    Aug 2001
    Posts
    1,301
    I'd say for adding multidimesional arrays, do something like this (i didn't read through all of your code, so i don't really know if you did this, but it may be cleaner or whatever. Also, USE CODE TAGS!)

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    
    #define MAX_FRONT 4
    #define MAX_BACK 2
    
    int main()
    {
     int array1[MAX_FRONT][MAX_BACK], array2[MAX_FRONT][MAX_BACK], results[MAX_FRONT][MAX_BACK];
     
     // recieving the values block //
     for (int i = 0; i < MAX_FRONT; i++)
     {
      for (int j = 0; j < MAX_BACK; j++)
      {
       cout << "Input value for " << i << j;
       cin >> array1[i][j];
      }
     }
     for (i = 0; i < MAX_FRONT; i++)
     {
      for (j = 0; j < MAX_BACK; j++)
      {
       cout << "Input value for " << i << j;
       cin >> array2[i][j];
      }
     }
     ////////////////////////////////////////
     
     // adding the arrays //
     for (i = 0; i < MAX_FRONT; i++)
     {
      for (j = 0; j < MAX_BACK; j++)
      {
       results[i][j] = array1[i][j] + array2[i][j];
      }
     }
     
     ////////////////////////////////////////
    
     // printing the results //
     for (i = 0; i < MAX_FRONT; i++)
     {
      for (j = 0; j < MAX_BACK; j++)
      {
       cout << results[i][j] << endl;
      }
     }
    }
    that should do it.. i haven't compiled it though, so there may be some minor erros.

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    8

    thank you

    Thanks alot guys, youve been a great help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Now I need help adding arrays
    By nifear4 in forum C Programming
    Replies: 28
    Last Post: 10-30-2008, 10:18 AM
  2. Multidimensional Arrays
    By jordanguyoflove in forum C Programming
    Replies: 4
    Last Post: 10-16-2008, 06:16 PM
  3. Multidimensional Arrays?
    By CreatedByShadow in forum C++ Programming
    Replies: 7
    Last Post: 01-13-2006, 10:35 PM
  4. Pointers to Multidimensional Arrays
    By kidburla in forum C Programming
    Replies: 10
    Last Post: 10-29-2005, 10:45 PM
  5. Multidimensional arrays in Korn shell
    By Yasir_Malik in forum Tech Board
    Replies: 3
    Last Post: 04-11-2004, 02:16 PM