Reputation: 84
I need my program to read an array from txt. file, It apperantly does, but when I paid attention to the numbers, I discovered that only half array is correct, the rest are random. Furthermore, if I read 2 diferent arrays, the random numbers are the same.
There is the code:
//List of Biblio.
#include <cstdlib>
#include<stdio.h>
#include <time.h>
#include <iostream>
#include <vector>
using namespace std;
FILE *fp;
//Functiotns declaration.
//
void dimensions(int &,int &,int &);
//
void rTime (int** ,int,int,int &); // The first int, it's an Array!
//
void rTimeprint (int** ,int,int,int);
//
void spt (int**,vector<int> & ,int,int); // The first int it's an Array!
//
int block (int, int,int, int,int,int);
//
int neh (int,int,int,int,int,int,int);
//
static int swap (int, int,int,int,int,int,int,int,double,double,float);
//
//A inserta, hi ha una matriu (1st int) i vector (3rd int)
static int inserta (int,int,int,int,int,int,int,int, double,double,float );
//
static int sa (double,double,float);
//Array varaibles;
int column, row, nombreProblemes;
int main() {
// Open the file where's the array;
fp = fopen("entrada.txt", "r");
dimensions(column, row, nombreProblemes);
// Create the Array:
int *matriuTemps[column];
for (int i= 0; i<column;i++){
matriuTemps[i] = new int [row];
}//end of loop Array[][] creator
// Function read time (array)
rTime(matriuTemps, column, row,problema);
//Function print the Array
rTimePrint(matriuTemps, column,row,problema);
}//End of main
And there is the function rTime:
void rTime(int *matriuTemps [],int column, int row, int &Problema){
fscanf(fp, "%d",&Problema);
for (int i= 0; i<row; i++){//Outter loop (row)
for (int j=0; j<column;j++){//inner loop (column))
matriuTemps[i][j]=0;
fscanf(fp,"%d",&matriuTemps [i][j]);
}//inner loop
}//outer loop
If it is more infor for you i put the function which prints the array:
void rTimeprint (int *matriuTemps[20] ,int column,int row,int problema){
cout<<"\nAquest és la matriu de temps del problema: "<< problema <<endl<<endl;
for (int i= 0; i<row; i++){//Outter loop (row)
for (int j=0; j<column;j++){//inner loop (column))
cout<< matriuTemps[i][j]<<"\t";
}//inner loop
cout << endl;
}//outer loop
}
And finally the Output: (i mark with x x, the column here the values start to be random, From there to the right are all random) first Array
x
x
54 83 15 71 77 36 53 38 27 87 76 91 14 29 12 77 32 87 68 94
79 3 11 99 56 70 99 60 16 89 49 15 89 45 60 23 66 58 31 68
16 89 49 15 89 45 60 23 66 58 31 68 78 91 13 59 58 56 20 85
66 58 31 68 78 91 13 59 58 56 20 85 53 35 53 41 69 13 86 72
58 56 20 85 53 35 53 41 69 13 86 72 8 49 47 87 58 18 68 28
Second Array:
x
x
0 0 0 0 0 0 0 0 0 87 76 91 14 29 12 77 32 87 68 94
45 33 11 99 56 0 87 60 16 89 49 15 89 45 60 23 66 58 31 68
12 89 40 15 89 45 60 23 66 58 31 68 78 91 13 59 58 56 20 85
46 57 1 68 78 95 78 53 58 56 20 85 53 35 53 41 15 13 86 72
52 56 20 95 3 15 78 42 15 13 86 72 8 49 47 87 58 18 68 28
Upvotes: 0
Views: 92
Reputation: 409176
In the main
functionyou declare
matriuTempsto be an array of
columnelements, each element being a pointer to
int`.
But in in e.g. rTime
you treat the array as an array of row
elements, each element being an array of column
integers. So you mix up the indexes, and unless both row
and column
are equal you will have problems.
Also note that C++ doesn't actually have variable-length arrays, making your definition of matriuTemps
not proper C++.
Instead, if you want dynamic arrays, you should use std::vector
. Maybe something like
std::vector<std::vector<int>> matriuTemps(column, std::vector<int>(row));
The above line defined matriuTemps
to be a vector or vectors of integers, and set the size of both "dimensions".
Upvotes: 1
Reputation: 212979
You seem to have got your row
and column
dimensions mixed up - this part:
int *matriuTemps[column];
for (int i= 0; i<column;i++){
matriuTemps[i] = new int [row];
}
should be:
int *matriuTemps[row]; // allocate `row` rows
for (int i= 0; i<row;i++){ // for each row
matriuTemps[i] = new int [column];
} // allocate `column` elements
Upvotes: 1