I'm attempting to create a 3x3 table in C , but the last value of each row is being replaced with the first value of the row above.
#include<iostream>
#include<iomanip>
#include<fstream>
using namespace std;
void fillArray(int *arrayRow1, int *arrayRow2, int *arrayRow3)
{
cout << "Enter the number for row 1 column 1: " << endl;
cin >> arrayRow1[0];
cout << "Enter the number for row 1 column 2: " << endl;
cin >> arrayRow1[1];
cout << "Enter the number for row 1 column 3: " << endl;
cin >> arrayRow1[2];
cout << "Enter the number for row 2 column 1: " << endl;
cin >> arrayRow2[0];
cout << "Enter the number for row 2 column 2: " << endl;
cin >> arrayRow2[1];
cout << "Enter the number for row 2 column 3: " << endl;
cin >> arrayRow2[2];
cout << "Enter the number for row 3 column 1: " << endl;
cin >> arrayRow3[0];
cout << "Enter the number for row 3 column 2: " << endl;
cin >> arrayRow3[1];
cout << "Enter the number for row 3 column 3: " << endl;
cin >> arrayRow3[2];
cout << "Resulting Table:" << endl;
cout << "_______" << endl;
cout << "|" << arrayRow1[0] << "|" << arrayRow1[1] << "|" << arrayRow1[2] << "|" << endl;
cout << "________" << endl;
cout << "|" << arrayRow2[0] << "|" << arrayRow2[1] << "|" << arrayRow2[2] << "|" << endl;
cout << "________" << endl;
cout << "|" << arrayRow3[0] << "|" << arrayRow3[1] << "|" << arrayRow3[2] << "|" << endl;
cout << "_______" << endl;
}
int main(){
int arrayRow1[2], arrayRow2[2], arrayRow3[2];
fillArray(arrayRow1, arrayRow2, arrayRow3);
}
When 1, 2, 3, 4, 5, 6, 7, 8, 9 is entered it resulting the the following output:
________
|1|2|4|
________
|4|5|7|
________
|7|8|9|
________
The expected output is:
________
|1|2|3|
________
|4|5|6|
________
|7|8|9|
________
What is cause this issue?
CodePudding user response:
You are treating arrays as if they have three cells but you defined arrays with only two cells (e.g. arrayRow1[2]
). This causes undefined behaviors which in this case I guess arrays are consecutive, and your indices kind of overlap. Change array definition to arrayRow[3]
to fix the problem.
CodePudding user response:
your int
array rows only hold 2 elements each, so you should say
int arrayRow1[3], arrayRow2[3], arrayRow3[3];
so they can hold 3 values each.
std::array
should be used here, so you don't pass pointers to your function. For more efficient and cleaner code use multidimensional arrays [3][3]
or std::array<std::array<int, 3>, 3>;
in this case:
using table = std::array<std::array<int, 3>, 3>;
void fillArray(table& table)
{
for (int row = 0; row < table.size(); row) {
for (int col = 0; col < table[row].size(); col) {
std::cout << "Enter the number for row " << row 1 << " column " << col 1 << ": " << std::endl;
std::cin >> table[row][col];
}
}
std::cout << "Resulting Table:" << std::endl;
for (int row = 0; row < table.size(); row) {
std::cout << "_______" << std::endl;
std::cout << "|";
for (int col = 0; col < table[row].size(); col) {
std::cout << table[row][col] << "|";
}
std::cout << std::endl;
}
}
int main() {
table table;
fillArray(table);
}
output:
Enter the number for row 1 column 1:
1
Enter the number for row 1 column 2:
2
Enter the number for row 1 column 3:
3
Enter the number for row 2 column 1:
4
Enter the number for row 2 column 2:
5
Enter the number for row 2 column 3:
6
Enter the number for row 3 column 1:
7
Enter the number for row 3 column 2:
8
Enter the number for row 3 column 3:
9
Resulting Table:
_______
|1|2|3|
_______
|4|5|6|
_______
|7|8|9|
read here why you shouldn't use namespace std;