How can I make 3*3 array?

Dear Friends,
Would you please help me with the code below?
I want to make a 3*3 array. I mean 9 times the code ask me the t and it calculate the acc and put it in 3*3 dimension matrix

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
  #include <iostream>

using namespace std;

int acc(int x)
{
return (10*x);
}

int main() {

     int t;
     const int i=3;
     const int j=3;
     int num [r] [c];
     for (r=0; r<=i; ++r)
         for (c=0; c<=j; ++c)
         {
    cout<<"enter t=";
    cin>>t;
    acc(t);
    cout<<"acc="<<num [r] [c]<<"\n";
         }

    return 0;

}
"r" and "c" are not declared variables, You can declare each inside the double for loop.

What are you trying to get your acc(int) function to do?
Right now, calling it on like 21 doesn't do anything (do you see why?), because you do nothing with the return value that it gives. This means that the t value you're receiving from the console input isn't being used for anything useful.

One common way to iterate through a 2D array would be something like this
1
2
3
4
5
6
const int Y = 5;
const int X = 7;
int arr[Y][X];
for (int i = 0; i < Y; i++)
    for (int j = 0; j < X; j++)
        cin >> arr[i][j];


If you're trying to get t, and then use the "acc" of that value, you'd do something like

1
2
3
4
5
6
7
8
for (int i = 0; i < Y; i++)
{
    for (int j = 0; j < X; j++)
    {
        cin >> t;
        arr[i][j] = acc(t); // the return value of acc() is now being used for something useful!
    }
}

Edit: fixed code
Last edited on
Thanks for your comment.
I want 9 times this code ask me the t and then calculate the acceleration by
equation 10*t and save it in or show it in 3*3 matrix or array. I corrected the code as you told. But now while running it gives error and closes my compiler. why?

#include <iostream>

using namespace std;

int acc(int x)
{
return (10*x);
}

int main() {

int t;
int r;
int c;
const int i=3;
const int j=3;
int num[r][c];
for (r=0; r<=i; ++r)
for (c=0; c<=j; ++c)
{
cout<<"enter t=";
cin>>t;
num[r][c]=acc(t);
cout<<"acc="<<num[r][c]<<"\n";
}

return 0;

}

Seems like you're getting your r,c,i, and j mixed up.

You are trying to declare an array num with dimensions [r][c], but r and c are not declared yet, and are also not const (an array size can't be a variable number). You probably meant to make it int num[i][j];

You also want <, not <= for your for loops.
If an array has a size of 8, accessing array[8] is out of bounds.
Last edited on
Dear Friend,
I corrected some parts of the code but the out put is not in the way I want. Would you please help me?
Now I want to give t, 9 times and then the output be like a 3*3 matrix for example:
t= 2 t=3 t=4 t=5 t=6 t=7 t=8 t=9 t=10

out put:
20 30 40
50 60 70
80 90 100

Is there any way the code read the t numbers from a text file in my computer desktop and do calculations and then save the output in matrix shape in another text file in desktop?

the code:

#include <iostream>

using namespace std;

int acc(int x)
{
return (10*x);
}

int main() {

int t;

int i=3;
int j=3;
int num[i][j];
for (i=0; i<3; i++){
for (j=0; j<3; j++)
{
cout<<"enter t=";
cin>>t;
num[i][j]=acc(t);
cout<<num[i][j]<<"\t";
}
cout<<"\n";
}
return 0;

}
Last edited on
to do the manipulations, do you really need to store the numbers in a 2D array, if not, you can just use a 1D array or vector. Then after doing your stuff, print the results to the file in a matrix form.

If you explicitly need to use a 2D array to do your manipulations, here is the code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

int acc(int x)
{
    return (10*x);
}

int main()
{
    const int n = 9;  //reading 9 items from file, assume the file has upto 9 items
    int array[3][3];
    
    ifstream inputFile("C:/users/..../Desktop/input.txt");  //ive not checked if the file exists
    ofstream outputFile("C:/users/.../Desktop/output.txt");
    
    //read from file
    int a;
    for(int i = 0; i < 3; i++)
    {
        for(int j = 0; j < 3; j++)
        {
            inputFile<<a;
            array[i][j] = acc(a);
        }
    }
    
    //print to file
    for(int i = 0; i < 3; i++)
    {
        for(int j = 0; j < 3; j++)
        {
            if(i%3 == 0)
                cout<<endl;
            outputFile<<array[i][j]<<setw(4);
        }
    }
    return 0;
}
Dear Friend,
Thanks for your comment. Yes I need to get a text file with 2D array.
I run the code you wrote but I received error in lines 13 and 25.
To run it I made a text file in desktop and wrote 9 numbers in it as below and then saved and closed it.
2
3
4
5
6
7
8
9
10
Make sure your file is being opened correctly. The file paths that shadowCODE wrote were just example paths, you need to replace them with your own if you didn't.

If the file is in the same folder as your executable file, you don't need to give an absolute path, and you could just open inputFile("input.txt");

add this:
1
2
3
4
if (!inputFile.is_open() || !outputFile.is_open())
{
    cout << "Error: Cannot open file." << endl;
}


Also post the code again if it's different than the above.
Last edited on
Thanks for your comment. There is still error in lines 13 and 25
1-I do not know why in line 13 of the code above why there is n=9 but we have not used the n at all in the code?

2- After I create the txt file with the name of input and writing 9 numbers in it, should I define it to the compiler or it will detect it, I mean should I change some settings??

3- Between which lines should I add these 4 lines?

4- should I add open and close to the code above?

Thanks

Last edited on
Topic archived. No new replies allowed.