I've nested loops and would like to separate them

Hi I've a loop inside another loop and would like to separate them into two different loops and keep the same functionality, since I'm having trouble with probably memory allocation

Here are the code:


ofstream myfile1;
ofstream grid;
myfile1.open ("fine-time.txt");
grid.open ("grid.txt");
//myfile << "Writing this to a file.\n";

double temp=0;
myfile1 << setw(16) ;
for (int k=1; k<nz+1;k++){
for (int j=1; j<ny+1;j++){
for (int i=1; i<nx+1;i++){
index= (k-1) * nx * ny +(j-1) * (nx) + (i-1) ;
/*if(Final_T[index]==inf){
Final_T[index]=1.;
}*/
myfile1 <<Final_T[index]<< setw(16) ;
// save fine scale time
time_orig[index]= Final_T[index];
//myfile1 <<F[index]<< setw(16) ;
temp=temp+Final_T[index];

}

myfile1 << endl ;
}
}


//variables for tempery values and to count # of grids
double temp1,val=0.0;
int count=0;

//sorting Final_T array using buble sort algorithm
for(i=0;i<npixels;i++){
for(j=0;j<npixels-1;j++){

if(Final_T[j]>Final_T[j+1]){

temp1=Final_T[j+1];
Final_T[j+1]=Final_T[j];
Final_T[j]=temp1;
}


}
}

//write in to gird.txt
for(i=0;i<npixels;i++){

if(val<Final_T[i]){
grid<<Final_T[i-1]<<" --> "<<count-1<<endl;
val=Final_T[i];
}
count++;

}
grid<<Final_T[i-1]<<" --> "<<count<<endl;





myfile1.close();
grid.close();
cout<<"Summation-time = ";
cout<<temp/1000;


You can't separate nested loops, that's the point of nesting them. The problem must be somewhere else in your code - trying to un-nest the loops will not help. You can cure pain by removing the brain, but that doesn't help.
My friend I made the modification with the help of my mate

The modified one is the one above:

Here is the original:

I think the brain will not be removed, and the loops can be un-nested


------------------------------------------------
ofstream myfile1;
myfile1.open ("fine-time.txt");

//myfile << "Writing this to a file.\n";

double temp=0;
myfile1 << setw(16) ;
for (int k=1; k<nz+1;k++){
for (int j=1; j<ny+1;j++){
for (int i=1; i<nx+1;i++){
index= (k-1) * nx * ny +(j-1) * (nx) + (i-1) ;
/*if(Final_T[index]==inf){
Final_T[index]=1.;
}*/
//cout<< "End of file"<<endl;
myfile1 <<Final_T[index]<< setw(16) ;
// save fine scale time
time_orig[index]= Final_T[index];
//myfile1 <<F[index]<< setw(16) ;
temp=temp+Final_T[index];

}

myfile1 << endl ;
}
}
myfile1.close();
cout<<"Summation-time = ";
cout<<temp/1000;
///cout<< "End of file"<<endl;
system("pause");



//

Last edited on
Topic archived. No new replies allowed.