Guyz I have again got a problem with functions. "" Write a function to input matrices"" Or how can I input using fuctions ?? is it possble ??

so far I have developed a few lines.

//fuction to input a 2D-array.
int multi_array(int r , int c)//r is number fo rows and c is number of colums
  {
  int j=0,i=0,arr[r][c];
    for(i=0;i<r;i++)
      {
       for(j=0;j<c;j++)

plz help me asap.

Recommended Answers

All 16 Replies

Is the matrix a given size or can the user determine how big it can be?

declare a 2 dim array.
inside the inner loop, as user for i/p and keep storing in each element of the array.

--post the code inside code tags

as user for i/p ?? what does that mean

@"niek_e" = The array size is undefined/

my problem is how do I input through a function.

can anyone gimme the specific code line.

as user for i/p ?? what does that mean

He means 'ask user for input'

@"niek_e" = The array size is undefined/

Ok. How does the user input the data? Is there some sort of delimiter between numbers or...?? Please give your specific requirements.

You have a error in your code: int arr[r][c]; You can only do this if 'r' and 'c' are const int . If you want to create a array dynamicly you should look at new and delete (or malloc() and free())


Niek

as user for i/p i meant 'ask' user for i/p

btw if matrix size is undefined at compile time then simple array will not work as you wont know the dimensions..

sorry for the repetition, i guess i didnt refresh for long :) ...

I am posting the actual question

Write a menu driven program to execute the following functions:
* Function to input a matrix
*Function to display a matrix
*Function to find the product of two matrices.
*Function to return the sum of left diagonal of the matrix.

it doesn't say what are the number of rows or columns in the matrix...

I suppose we are to define it in the program not function.

Great. So finally you answer my question if it has to by user-defined or not. This makes your life a lot easier, because dynamicly allocating 2-d arrays isn't really simple.

so now you can do something like this:

#include <iostream>


int main()
{
    const int ASIZE = 3;
    
    int matrix[ASIZE][ASIZE];
    
    for (int x =0; x < ASIZE; x++)
    {
        for (int y =0; y < ASIZE; y++)
        {
               // do stuff here
        }
    }

    return 0;   
}

thanx but can you please help me with the placement of

cin>>

Perhaps at the line where I said : //do stuff here

but that will mean that the user will have to edit the function every time.

but that will mean that the user will have to edit the function every time.

What do you mean? Editing what function?

Here's what I meant:

#include <iostream>


int main()
{
    const int ASIZE = 3;
    
    int matrix[ASIZE][ASIZE];
    
    for (int x =0; x < ASIZE; x++)
    {
        for (int y =0; y < ASIZE; y++)
        {
            std::cout << "enter value for x,y = " << x << "," << y << std::endl;
            std::cin >> matrix[x][y];
        }
    }

    return 0;   
}

If you run this, you'll understand how this nested loop works. After you understand this code, printing the matrix should be easy.

thanx.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.