簡體   English   中英

我的程序只讀取一半的數組c ++

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

我需要我的程序從txt讀取數組。 文件,確實如此,但是當我注意數字時,我發現只有一半數組是正確的,其余都是隨機的。 此外,如果我讀取2個不同的數組,則隨機數相同。

有代碼:

    //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

還有功能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

如果它對您來說更有用,那么我將輸出打印數組的函數:

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
}

最后是輸出:(我用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  

第二個數組:

                                     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  

您似乎混淆了rowcolumn尺寸-這部分:

int *matriuTemps[column];

for (int i= 0; i<column;i++){
     matriuTemps[i] = new int [row];
}

應該:

int *matriuTemps[row];           // allocate `row` rows

for (int i= 0; i<row;i++){       // for each row
     matriuTemps[i] = new int [column];
}                                // allocate `column` elements

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

但是在例如rTime您將數組視為row元素的數組,每個元素都是column整數的數組。 因此,您需要混合使用索引,除非rowcolumn相等,否則您將遇到問題。


另請注意,C ++實際上沒有可變長度數組 ,因此您對matriuTemps的定義不適合C ++。

相反,如果要動態數組,則應使用std::vector 也許像

std::vector<std::vector<int>> matriuTemps(column, std::vector<int>(row));

上面的行將matriuTemps定義為一個或多個整數向量,並設置兩個“維度”的大小。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM