简体   繁体   English

我的程序只读取一半的数组c ++

[英]My program just reads half an array c++

I need my program to read an array from txt. 我需要我的程序从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. 此外,如果我读取2个不同的数组,则随机数相同。

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: 还有功能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 xx, the column here the values start to be random, From there to the right are all random) first Array 最后是输出:(我用xx标记,这里的值开始是随机的,从那里到右边都是随机的)第一个数组

                                 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  

You seem to have got your row and column dimensions mixed up - this part: 您似乎混淆了rowcolumn尺寸-这部分:

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

In the main function you declare matriuTemps to be an array of column elements, each element being a pointer to int`. main函数中, you declare matriuTemps you declare to be an array ofelements, each element being a pointer to to be an array of elements, each element being a pointer to int` elements, each element being a pointer to

But in in eg rTime you treat the array as an array of row elements, each element being an array of column integers. 但是在例如rTime您将数组视为row元素的数组,每个元素都是column整数的数组。 So you mix up the indexes, and unless both row and column are equal you will have problems. 因此,您需要混合使用索引,除非rowcolumn相等,否则您将遇到问题。


Also note that C++ doesn't actually have variable-length arrays , making your definition of matriuTemps not proper C++. 另请注意,C ++实际上没有可变长度数组 ,因此您对matriuTemps的定义不适合C ++。

Instead, if you want dynamic arrays, you should use std::vector . 相反,如果要动态数组,则应使用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". 上面的行将matriuTemps定义为一个或多个整数向量,并设置两个“维度”的大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM