Here i have a working code, what i want to know is:
When the user inputs elements to the array, i want it to be automatically aligned in form of an matrix.

Instead of

1
2
3
4
5
6
etc....

But just as a matrix, like this

1 2 3
4 5 6

I tried to accomplish that for some time now, and so far not successful.
I tried with escape sequences, but there is no way i can go to a previous line with escape sequence, so that i can use \b .
Google and daniweb searches, didn't help at all, except for a slight information about gotoxy(), so my question is, is it even possible for to do that in run time?
And if yes how? And how can i use gotoxy() (With syntax, it's much appreciated, & what are the arguments for this function)?

// Matrix-Addition.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
{
	return 0;
}

#include<iostream>
#include<conio.h>

using namespace std;

int main()
{
	int a[10][10],b[10][10],c[10][10], r, g, d, e;
	cout<<"Enter the dimensions for the first matrix"<<endl;
	cin>>r;
	cin>>g;
	cout<<"Enter the dimensions for the second matrix"<<endl;
	cin>>d;
	cin>>e;
	if((r==d)&&(g==e))
	{
		cout<<"Enter the elements for the first matrix"<<endl;
		for(int i=0;i<r;i++)
		{
			for(int j=0;j<g;j++)
			{
				cin>>a[i][j];
			}
		}
		cout<<endl<<endl;
		cout<<"In Matrix:"<<endl<<endl;
		for(int i=0;i<r;i++)
		{
			cout<<endl;
			for(int j=0;j<g;j++)
			{
				cout<<a[i][j]<<" ";
			}
		}
		cout<<endl<<endl;
		
		cout<<"Enter the elements for the second matrix"<<endl;
		for(int i=0;i<r;i++)
		{
			for(int j=0;j<g;j++)
			{
				cin>>b[i][j];
			}
		}
		cout<<endl<<endl;
		
		cout<<"In Matrix:"<<endl<<endl;
		for(int i=0;i<r;i++)
		{
			cout<<endl;
			for(int j=0;j<g;j++)
			{
				cout<<b[i][j]<<" ";
			}
		}
		for(int i=0;i<r;i++)
		{
			for(int j=0;j<g;j++)
			{
				c[i][j]=a[i][j]+b[i][j];
			}
		}
		cout<<endl<<endl;
		
		cout<<"The matrix after addition is "<<endl;
		for(int i=0;i<r;i++)
		{
			cout<<endl;
			for(int j=0;j<g;j++)
			{
				cout<<c[i][j]<<" ";
			}
		}
	}
	else
	{
		cout<<"The dimensions entered mismatch, addition impossible";
	}
	getch();
	return 0;
}

You are explicitly asking the user for the dimensions, so what is the problem? You have to assume either row-major or column-major (i.e. for:

1 2
3 4

row major ordering would be (1,2,3,4) where column major would be (1,3,2,4) ), but once you specify how you expect the input there is no reason to need line break characters, etc.

Also, please please please change variable names like 'd', and 'e' to "numberOfRows", etc.

David

You are explicitly asking the user for the dimensions, so what is the problem? You have to assume either row-major or column-major (i.e. for:

1 2
3 4

row major ordering would be (1,2,3,4) where column major would be (1,3,2,4) ), but once you specify how you expect the input there is no reason to need line break characters, etc.

Also, please please please change variable names like 'd', and 'e' to "numberOfRows", etc.

David

Thats not what i meant, i want the inputs to be aligned in the form of a matrix, as the user inputs each element in the run time....Hope i made it more clear this time. Anyway thanks for the suggestions.....

It looks like you are reading in all of the values, then only writing out the matrix once. What you have looks good, but I would suggest making it into a function, and using variable names that are descriptive:

void PrintMatrix(int matrix[10][10]; const int numberOfRows, const int numberOfColumns)
{
for(int row = 0; row < numberOfRows; row++)
{
  cout<<endl;
  for(int column = 0; column < numberOfColumns; column++)
  {
    cout<<a[i][j]<<" ";
  }
}
}

It looks like you are reading in all of the values, then only writing out the matrix once. What you have looks good, but I would suggest making it into a function, and using variable names that are descriptive:

Let me make this clear,

Presently the console screen will be like this ( Not complete)

Enter the dimensions for the first matrix

\\user inputs two values
2
4

Enter the dimensions for the second matrix

\\user inputs two values
2
4

Enter the elements for the first matrix
\\user inputs 8 elements

1
2
3
4
5
6
7
8

In Matrix:

1 2 3 4
5 6 7 8

Enter the elements for the second matrix

1
2
3
4
5
6
7
8

In Matrix:

1 2 3 4 
5 6 7 8

But i want the elements to be aligned in the form of a matrix just as the user enters each element, not print it after getting all the elements.

So the expected console screen is (Incomplete)

Enter the dimensions for the first matrix

\\user inputs two values
2
4

Enter the dimensions for the second matrix

\\user inputs two values
2
4

Enter the elements for the first matrix
\\user inputs 8 elements

In Matrix:

1 2 3 4 \\Here as the user inputs "1" it goes to the (row 1, column 1) Position, Next when user types "2" it goes to (r 1, c 2) position. Like wise, the rest..... 
5 6 7 8

Enter the elements for the second matrix

In Matrix:

1 2 3 4 \\Same as above....
5 6 7 8

So basically
User don't enter elements like this

Enter the elements for the second matrix

1
2
3
4
5
6
7
8

But like this (want to know if this is possible)

Enter the elements for the second matrix

In Matrix:

1 2 3 4 
5 6 7 8

So is my question clear? Thanks for spending some time for me.....

Posted my code, because i wanted to know what changes in code can accomplish this.....

You can simply enter your input separated by spaces. It need not be \n character always.
For cin>>i>>j; you can actually enter two numbers separated by space.

Or, you want a space to be inserted when you press enter, and wait for the next input?

You can simply enter your input separated by spaces. It need not be \n character always.
For cin>>i>>j; you can actually enter two numbers separated by space.

I knew that and that's not what i want.

Or, you want a space to be inserted when you press enter, and wait for the next input?

Yes, i want the space to be inserted when you press enter, without jumping to the next line. And when the maximum number of columns in a row is reached, it should jump on to the next line ie. the new row....

Thanks for the help in advance..

Yes, i want the space to be inserted when you press enter, without jumping to the next line. And when the maximum number of columns in a row is reached, it should jump on to the next line ie. the new row....

Assuming you want the console to auto-format itself according to the size of your matrix, this cannot be done without taking control of the console in a non-standard way. It's not terribly difficult, but I'd question the practicality of replacing the console with your own custom shell for the sole purpose of formatting matrix input.

Here's a very naive example of the concept:

#include <cctype>
#include <iostream>
#include <sstream>
#include <string>
#include <conio.h>

using namespace std;

string raw_gets()
{
    string result;
    int ch;
    
    // Naive algorithm, use at your own risk
    while (!isspace(ch = getch())) {
        result += (char)ch;
        cout << (char)ch;
    }
    
    return result;
}

int main()
{
    int mat[2][4];
    
    cout << "Enter the matrix:\n";
    
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 4; j++) {
            stringstream conv(raw_gets());
            int x;
            
            conv >> x;
            mat[i][j] = x;
            
            cout << (j < 3 ? ' ' : '\n');
        }
    }
    
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 4; j++)
            cout << mat[i][j] << ' ';
            
        cout << '\n';
    }
}
commented: Thank U very much +3

Assuming you want the console to auto-format itself according to the size of your matrix, this cannot be done without taking control of the console in a non-standard way. It's not terribly difficult, but I'd question the practicality of replacing the console with your own custom shell for the sole purpose of formatting matrix input.

Here's a very naive example of the concept:

#include <cctype>
#include <iostream>
#include <sstream>
#include <string>
#include <conio.h>

using namespace std;

string raw_gets()
{
    string result;
    int ch;
    
    // Naive algorithm, use at your own risk
    while (!isspace(ch = getch())) {
        result += (char)ch;
        cout << (char)ch;
    }
    
    return result;
}

int main()
{
    int mat[2][4];
    
    cout << "Enter the matrix:\n";
    
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 4; j++) {
            stringstream conv(raw_gets());
            int x;
            
            conv >> x;
            mat[i][j] = x;
            
            cout << (j < 3 ? ' ' : '\n');
        }
    }
    
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 4; j++)
            cout << mat[i][j] << ' ';
            
        cout << '\n';
    }
}

Thank you very much. You were right on the nail. That's what i wanted. But why did you ask me to use at my risk? Anyway i need to work out the logic from this. Thanks for the code.......

But why did you ask me to use at my risk?

Because it's example code and not robust enough to be usable in any kind of real program.

Because it's example code and not robust enough to be usable in any kind of real program.

Ok! Understood! But does that mean such a automatic aligning of the matrix in CONSOLE Application is impossible, No other robust method?

But does that mean such a automatic aligning of the matrix in CONSOLE Application is impossible, No other robust method?

No, it just means I had neither the time nor the inclination to write a robust function for you when I think the whole concept is stupid. raw_gets() is a good starting point for adding the necessary checks and features to make it robust, but I'll leave that up to you.

No, it just means I had neither the time nor the inclination to write a robust function for you when I think the whole concept is stupid. raw_gets() is a good starting point for adding the necessary checks and features to make it robust, but I'll leave that up to you.

Thank You very much for your help. I will try to carry on from here. Still, is such an concept all that useless? Got the idea, when one of my colleague (No touch with C++), ran my program and was quiet confused with the matrix. So just wanted to make it user-friendly.

Anyway Thanks again.

Thank You very much for your help. I will try to carry on from here. Still, is such an concept all that useless? Got the idea, when one of my colleague (No touch with C++), ran my program and was quiet confused with the matrix. So just wanted to make it user-friendly.

Anyway Thanks again.

Yes it might be fun and user friendly but only for a while... It's not like your gonna use the program or use it as a basis for another program years for now
....then again who knows I could be wrong ;)

Instead you could also show the user the position they're inputting the number like e.g
"Enter number for row 1 column 1:"
To make it a bit more user friendly without going to all the trouble your doing right now

Just a suggestion :)

Yes it might be fun and user friendly but only for a while... It's not like your gonna use the program or use it as a basis for another program years for now
....then again who knows I could be wrong ;)

Instead you could also show the user the position they're inputting the number like e.g
"Enter number for row 1 column 1:"
To make it a bit more user friendly without going to all the trouble your doing right now

Just a suggestion :)

Suggestion Taken into account Mam. You seems to be right. Thanks for posting.

Still, is such an concept all that useless?

Yes. If you want GUI behavior, write a GUI. Forcing the console to act like a GUI is awkward and confusing to users.

Yes. If you want GUI behavior, write a GUI. Forcing the console to act like a GUI is awkward and confusing to users.

Oh! Like that! Now i understood. Well! Just took the adventurous route. And you were really helpful.

Assuming you want the console to auto-size itself according to the size of your matrix, this can be done without taking control of the console in a non-standard. It is not difficult, but I would question the value of replacing the console with your own custom shell for the sole purpose of the matrix input format.

Assuming you want the console to auto-size itself according to the size of your matrix, this can be done without taking control of the console in a non-standard. It is not difficult, but I would question the value of replacing the console with your own custom shell for the sole purpose of the matrix input format.

Negation Narue's Post? Well that's not what i wanted. I didn't want the console to auto re size, just wanted the matrix to self align. Well i got the code. Any further help is welcome anyway....

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.