generating a matrix based on 2 numbers from a file

hi, im new to programming and i find this problem difficult, I hope you can help me with it.

generate a quadratic matrix with the size of the first number, with all the elements equal to 0, except for the element on the first line, in the column represented by the second number read from the file. This element will have the value 1. Display this matrix

The numbers are id= 4 id2 = 3
Last edited on
this is what i managed to do so far

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{ int a[10][10],i,j;
string line_;
ifstream file ("yes.txt");
int id;
int id2;
if(file.is_open())
{
while(file >> id >> id2)
{




}
for(i=0;i<id;i++)
{
a[0][id2-1]=1;
}

for(i=0;i<id;i++)
for(j=0;j<id;j++){
if(a[i][j]!=1)
a[i][j]=0;
}
for(i=0;i<id;i++){
for(j=0;j<id;j++)
cout<< a[i][j]<< " ";
cout<< endl; }
file.close();


return 0;
}
}




this is the result i got https://gyazo.com/59791254032e988df069c5a4d359bc19
I don't understand why is there a 1 on the last row
Assuming the column number starts at 1, then consider:

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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {
	const size_t MAXARR {10};

	//ifstream file("yes.txt");

	//if (file.is_open()) {
		int id {}, id2 {};
		int a[MAXARR][MAXARR] {};

		//if (file >> id >> id2) {
		if (cin >> id >> id2 && id > 1 && id < MAXARR && id2 >= 1 && id2 < MAXARR) {
			a[0][id2 - 1] = 1;

			for (int i = 0; i < id; ++i) {
				for (int j = 0; j < id; ++j)
					cout << a[i][j] << " ";

				cout << endl;
			}
		}
	//}
}



4 3
0 0 1 0
0 0 0 0
0 0 0 0
0 0 0 0


which shows the required one element set.
thank you very much
Topic archived. No new replies allowed.