I am again here with this query.
In the following code, I initialize my matrix with all values equals zero and after it, I write this matrix in a text file. Now I want to read that matrix from the file and put all the values in another matrix named arr1[][] and when I print it on the screen I got a blank screen why?
Please help me thanks in advance.
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
int main()
{
int n = 10;
int m = 10;
int arr[10][10];
int arr1[10][10];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
{
arr[i][j] = 0; // here i initialized my matrix with all values zero.
}
ofstream fout;
fout.open("array.txt");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
fout << arr[i][j]; // here i write it into the file
}
cout << endl;
}
fout.close();
ifstream fin;
fin.open("array.txt");
if (!fin)
{
cerr << std::strerror(errno) << "\n"; // handle open errors
}
fin >> n >> m;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
{
fin >> arr[i][j]; // now here i read the matrix and put it into the
// different matrix.
arr1[i][j] = arr[i][j];
}
fin.close();
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cout << arr1[i][j]; // when i print it to the screen i got black screen
// but i wants
// a matrx which written in the file.
}
cout << endl;
}
return 0;
}
Related
I'm tring to read a text file that's made out of 30x100 integers: https://pastebin.com/wemNrdMZ
For some reason it display perfectly if I read the file into a char data type. But when I change the data type to int the display becomes completely messed up: https://imgur.com/k0k11lH
Here's my code:
int const row = 30;
int const colum = 100;
int map[row][colum];
fstream inFile;
inFile.open("map.txt"); //Located at the same location as .cpp file
if (inFile.is_open())
{
for (int i = 0; i < row; i++)
for (int j = 0; j < colum; j++)
{
inFile >> map[i][j];
}
inFile.close();
}
else
{
cout << "file not found" << endl;
}
for (int i = 0; i < row; i++)
{
for (int j = 0; j < colum; j++)
{
cout << map[i][j];
}
cout << endl;
}
}
successful reading if I change from int to char: https://imgur.com/GsXnPUI
I am trying to make the function output numbers in a form of a matrix, not just a line
void SaveMatrix(TMatrix* mat){
ofstream SaveM;
SaveM.open("Matrix.txt", ios::out);
if (SaveM.is_open()){
for (int i=0; i<mat->line; ++i){
for (int j=0; j<mat->column; ++j){
SaveM<< mat->m[i][j]<<" ";
}
}
}else{
cout<<"file is open"<<endl;
}
}
I tried to put this in the second for-cycle, without a result
if(j==mat->column){
SaveM<<endl;
}
The matrix is declared:
struct TMatrix {
double* *m;
int line;
int column;
};
j will never reach mat->column in your inner for loop.
I think you could use something like this:
for (int i = 0; i < mat->line; i++){
SaveM << "[";
for (int j = 0; j < mat->column; j++){
SaveM << mat->m[i][j]);
if (j < sizeMatrix - 1){
SaveM << ", ";
}
}
SaveM << "]" << endl;
}
I am currently working on a program that prints a 5 variable truth table. I am using a 2d array. My code currently produces the table, but says it is corrupt and "the stack around the variable "table" was corrupted. Any help?
#include <iostream>
using namespace std;
int main() {
bool table[5][32];
for (int i = 0; i < 32; i++) {
for (int j = 0; j < 5; j++) {
table[i][j] = ((i >> j)& 1);
}
}
for (int i = 0; i < 32; i++) {
for (int j = 0; j < 5; j++) {
cout << table[i][j] << " ";
}
cout << endl;
}
return 0;
}
This is homework, so I would like to understand it, not just have an answer.
The index is wrong. Only table[0] to table[4] are available, so accessing table[5] to table[31] is illegal.
Try this:
#include <iostream>
using namespace std;
int main() {
bool table[32][5]; // swap 32 and 5
for (int i = 0; i < 32; i++) {
for (int j = 0; j < 5; j++) {
table[i][j] = ((i >> j)& 1);
}
}
for (int i = 0; i < 32; i++) {
for (int j = 0; j < 5; j++) {
cout << table[i][j] << " ";
}
cout << endl;
}
return 0;
}
There is attempt to read out of bound values from array.
If you need 5x32 matrix Use code below:
for (int i = 0; i < 5; i++) { // 32-> 5
for (int j = 0; j < 32; j++) { // 5->32
If you need 32x5 matrix then replace code below:
bool table[32][5]; //it was table[5][32];
I'm trying to make a C++ program start creating an array and takes the values from the user , then print every value + star as much the value is .. Example : the user had entered 5 then the output must be like this
5*****
Input
1
2
3
4
5
6
output
1*
2**
3***
4****
and so on
.. help :(
#include <iostream>
using namespace std;
void main()
{
int arr[10];
for (int i = 0; i < 10; i++)
{
cin >> arr[i];
int x = arr[i];
for (int j = 0; x <= arr[i]; j++)
{
cout<< "*";
}
}
}
And another help please can you give me some useful link to practice on programming to be professional
Your code is wrong. Use the following code:
#include <iostream>
using namespace std;
int main() {
int arr[10];
for (int i = 0; i < 10; i++)
{
cin >> arr[i];
int x = arr[i];
for (int j = 0; j < x; j++){ // your condition was wrong
cout<< "*";
}
cout<<endl; // for better formatting
}
return 0;
}
For edited question
int main() {
int arr[10];
for (int i = 0; i < 10; i++)
{
cin >> arr[i];
}
for (int i = 0; i < 10; i++)
{
int x = arr[i];
cout << x;
for (int j = 0; j < x; j++){ // your condition was wrong
cout << "*";
}
cout << endl;
}
return 0;
}
#include <iostream>
using namespace std;
void main()
{
int nbValues = 10;
int arr[nbValues];
// First recover the values
for (int i = 0; i < nbValues; i++)
{
cin >> arr[i];
}
// Then print the output
for (int i = 0; i < nbValues; i++)
{
int x = arr[i];
cout << x;// Print the number
for (int j = 0; j < x; j++)
{
cout<< "*";// Then print the stars
}
cout << endl;// Then new line
}
}
I am trying to read in a "map" into my program. it's a list of 100 numbers, I want it to be a 10 x 10 array. I'm trying to use a void function to read the file.
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
const int rows = 10, columns = 10, inaccessible = 0, start = 1, victory = 2;
typedef unsigned int world[rows][columns];
void loadWorld (world map[rows][columns]);
int main()
{
cout << "Welcome to my game! Get to the bottom of the volcano to win." << endl;
for (int i = 0; i < rows; i++)
{
for(int j = 0; j < columns; j++)
cout << world[i][j];
}
return 0;
}
void loadWorld (world map[rows][columns])
{
ifstream inData;
inData.open("world.txt");
}
To read it in, do the opposite of what you do with cout:
inData >> map[i][j];
Here you have a naive example. The output is formatted.
#include <iostream>
#define Rows 3
#define Cols 2
int main()
{
int Matrix[Rows][Cols];
//Input
for(int i = 0; i < Cols; i++)
for(int j = 0; j < Rows; j++)
std::cin >> Matrix[j][i];
//Output
for(int i = 0; i < Cols; i++)
{
for(int j = 0; j < Rows; j++)
std::cout << Matrix[j][i] << "\t";
std::cout << "\n";
}
return 0;
}