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;
}
Related
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;
}
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'm really stuck with my code for Gauss Elimination in C++, I need to return upper triangular matrix but still only thing I get is Segmentation fault. I know there must be some sort of going of allocated memory but I can't find where.
Code:
#include <iostream>
using namespace std;
double ** allocateDynamicArray(int order){
double ** dynArray = new double *[order];
int cols = order+1;
double *pool = new double [order * cols];
for(int i = 0;i < order; i++, pool += cols){
dynArray[i] = pool;
}
return dynArray;
}
void deallocateDynamicArray(double **dynArray){
delete [] dynArray[0];
delete [] dynArray;
}
void addAndPrintArray(double **dynArray, int order){
cout << "Zadejte prvky pole radu " << order << endl;
for(int i=0; i< order; i++){
for(int j=0; j< order+1; j++){
cout << "Pole[" << i << "][" << j << "]: ";
cin >> dynArray[i][j];
}
}
for(int i=0; i< order; i++){
for(int j=0; j< order+1; j++){
if(dynArray[i][j] < 10 && dynArray[i][j] >= 0){
cout << " ";
}
cout << dynArray[i][j] << " ";
}
cout << endl;
}
}
double ** gaussElimination(double** dynArray, int order){
for(int j=1; j<=order; j++) /*Horni trojuhelnikova matice*/
{
for(int i=1; i<=order; i++)
{
if(i>j)
{
double c=dynArray[i][j]/dynArray[j][j];
for(int k=1; k<=order+1; k++)
{
dynArray[i][k] = dynArray[i][k] - c * dynArray[j][k];
}
}
}
}
return dynArray;
}
int main()
{
cout << "Zadejte rad matice: ";
int order;
cin >> order;
double **arr = allocateDynamicArray(order);
addAndPrintArray(arr, order);
gaussElimination(arr, order);
deallocateDynamicArray(arr);
return 0;
}
Can anyone tell me what's wrong?
The problem is that in C/C++ the first element of an array should have index 0, so your
for(int i=1; i<=order; i++)
should be
for(int i=0; i<order; i++)
in the gaussElimination function.
void Graph::sortW ()
{
int length = vertices*vertices;
int array[length];
for (int i = 0; i < length; i++)
{
array[i] = 0;
cout << array[i]; // nothing prints
}
cout << "i";
// convert to 1D array
for (int i = 0; i < vertices; i++) {
for (int j = 0; j < vertices; j++) {
array[i*vertices+j] = matrix[i][j];
cout << array[i*vertices+j] << " "; // nothing prints
}
}
cout << "j";
qsort(array, length, sizeof(int), compare);
// for (int i=0 ; i<25; i++)
// cout << array[i] << endl; // only loop that prints?!
}
I have no idea why the output is ij only. Sort is called in the constructor like so:
// Sort weights using qsort
sortW();
Anyone have any ideas?
I'd suggest you put a
cout << vertices;
at the beginning of your function. My guess ist that vertices is zero?
I'm trying to create a 3D char array with dynamic memory. I create the char*** point in main then pass it to input and everything works fine until the input function returns and i try to repring the same locaton from main. I get "Access violation reading location." Any suggestions?
void input(char ***a, int f, int n)
{
cin >> f;
cin >> n;
a = new char**[f];
for (int i =0; i <f; ++i)
{
a[i]= new char*[n];
for (int j=0; j <n; ++j)
{
a[i][j] = new char[n];
}
}
for (int i =0; i<f; ++i)
{
for (int j=0; j<n; ++j)
{
for (int k = 0; k <n ;++k)
{
a[i][j][k] = '.';
cout << a[i][j][k];}
}
}
cout <<endl << endl<< a[2][5][5]; //test to see is value is '."
}
int main()
{
char ***station = 0;
int floors=0, n=0;
input(station, floors, n);
cout << endl << endl << station[2][5][5];
}