Here is the original code of Visual C++ which I have been trying to manipulate for more than a day.
This is the simplest error-free program which runs correctly.
What I want to do is insert two functions: one for taking input from a user, and another for displaying output.
#include<iostream>
#include<iomanip>
using namespace std;
void main()
{
int i, j,r,c;
int arr[5][5];
cout<< "enter r and c";
cin>> i>> j;
for(r=1;r<=i;r++)
{
for(c=1;c<=j;c++)
{
cout<< "enter elements";
cin>> arr[r][c];
}
}
cout<< " elements are"<< endl;
for(r=1;r<=i;r++)
{
for(c=1;c<=j;c++)
{
cout<<setw(4) <<arr[r][c];
}
cout<< endl<< endl;
}
cin.ignore();
getchar();
}
This is what I tried doing but it’s giving errors.
#include<iostream>
#include<iomanip>
using namespace std;
int input(int i, int j, int arr)
{
int r,c;
for(r=1;r<=i;r++)
{
for(c=1;c<=j;c++)
{
cout<< "enter elements";
cin>> arr[r][c];
}
}
return arr[r][c];
}
void output(int i, int j, int arr)
{
int r,c;
for(r=1;r<=i;r++)
{
for(c=1;c<=j;c++)
{
cout<<setw(4) <<arr[r][c];
}
cout<< endl<< endl;
}
}
void main()
{
int i, j,r,c;
int arr[5][5];
cout<< "enter r and c";
cin>> i>> j;
input(i,j,arr);
cout<< " elements are"<< endl;
output(i,j,arr);
cin.ignore();
getchar();
}
You really need a deeper understanding of C++, pointers, and functions. The following is what you are looking for (of sort), however, you certainly need a much deeper understanding of C++:
The problem is, explaining this would be like teaching you C++ from scratch. Whatever method you are using to learn C++, stick with it. You will get there, you’re just not quite ready. One thing you will find is it’s not always easy to take things to the next level. There is a reason things go and stop where they do when learning. Though, taking that extra step, or attempting to, isn’t bad, you just need to know when it isn’t time, yet.
Edit:
For the sake of completion, here is an example with no pointers: