C++ loop problem

statements within the loop not executed

Page 1 of 1

3 Replies - 1216 Views - Last Post: 30 August 2010 - 10:21 PM Rate Topic: -----

#1 brinda2410   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 29-August 10

C++ loop problem

Posted 30 August 2010 - 09:55 AM

I have written a code for simulation, from which i take a few parameters to solve a genetic algorithm problem.I have managed to bring about a few changes to my old program. The logic seems to be correct but i was not able to obtain the desired output. when i traced the program I found out that the statements within a for loop are not not getting executed.I am not sure of how to correct this error. The code is as below:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class costs
{
	 public:
               int holding_costs[4];
               int shortage_costs[4];
               int lead_times[4];
	           int maxlead_times[4],UB[4],LB[4];
			   int chromosome[20][4];
			   int random_integer[4];
			   int maxDem, minDem;
	       public:
			   void declare()
			   {maxDem=60;
				   minDem=20;}
				void getdata();
				void compute();
				void generate();
};
//to get the holding costs, shortage costs and lead times of different 

installations
void costs:: getdata()
{
for (int i=1;i<=4;i++)
              {
                  cout<<"enter the holding cost for the installation"<<i+1;
                  cin>>holding_costs[i];
              }
	      for (i = 1; i<=4;i++)
              {
                  cout<<"enter the shortage cost for the installation"<<i+1;
                  cin>>shortage_costs[i];
              }   
              //to read the lead times in diff installations
	      for (i=1;i<=4;i++)
              {
                  cout<<"enter the lead time for the installation"<<i+1;
                  cin>>lead_times[i];
              }
}
// to calculate the maximum lead times for each installation and also its 

//upper bound and lower bound
void costs:: compute()
{
int sum=0;
              for (int i=4;i>0;i--)
			  {
				  sum=sum+lead_times[i];
				  maxlead_times[i]=sum;
			  }
			  for(i=1;i<=4;i++)
			  {
				  UB[i]=maxlead_times[i]*maxDem;
				  cout<<endl<<UB[i];
				  LB[i]=lead_times[i]*minDem;
				  cout<<endl<<LB[i];
			  }
}
// generate a 20 chromosomes of length 4, using the upper and lower bound 

//of each installations
void costs:: generate()
{
	srand((unsigned)time(0));
	for(int j=1;j<=20;j++)
	{
	    cout<<endl;
		for(int i=1;i<=4;i++)
	 {
		random_integer[i] = rand()%(UB[i]-LB[i])+LB[i];
//		cout << random_integer[i] <<"  ";
		chromosome[j][i]=random_integer[i];
		cout<<chromosome[j][i]<<endl;
            }
	}
}
// SIMULATION
class simulation: public costs
{
public :
int 

recd_qty[4],on_order_beg[4],beg_inv[4],ini_BO[4],BO_shippd[4],BO_rem_ship

pd[4],inv_pos[4],qty_shippd[4];
int 

tot_qty_shippd[4],tot_qty_shippdArray[50][4],end_inv_pos[4],BO_end[4],on_o

rder_end[4],dem_up[4],HC[4],SC[4],TC[4];
int c_dem[4],d_dem[4],dem[4],dem_up_sup[50];
int TSCC,t;
public:
	void simulate();
};

void simulation::simulate()
{
srand((unsigned)time(0));
for (int j=1;j<=20;j++)	//selection of chromosome
{
   cout<<endl;
		for (int i=1;i<=50;i++)//no of days for the simulation 
		{
		   for(int k=4;k<=1;k--)//selection of gene(installation)"[size="7"]FOR LOOP NOT EXECUTED[/size]"
                           // various columns of the simulation table are given below
			{//column 9
			if(k==4)
				{
                                                       	c_dem[k]=rand()%60+20;
					dem[k]=c_dem[k];
					cout<<c_dem[k];
				}
			else
				d_dem[k]=dem[k+1];
					

		//column 2
				if(i<lead_times[k])
				{
					recd_qty[k]=0;
				}
				else
				{
					if(k==1)
					

recd_qty[k]=dem_up[i-lead_times[k]];
					else
					

recd_qty[k]=tot_qty_shippdArray[i-lead_times[k]][k];
				}
	     //column 3,4,5
				if(i==1)
				{
					on_order_beg[k]=0;
					

beg_inv[k]=chromosome[j][k];
					ini_BO[k]=0;
				}
				else
				{
					

on_order_beg[k]=on_order_end[k]-recd_qty[k];
					

beg_inv[k]=end_inv_pos[k]+recd_qty[k];
					ini_BO[k]=BO_end[k];
				}
	      //column 6
	      if(i==1)
		    BO_shippd[k]=0;
		     else
		     {
			 if(beg_inv[k]<ini_BO[k])
			 {
			  BO_shippd[k] = beg_inv[k];
			  }
			  else
			  {
			      BO_shippd[k]=ini_BO[k];
			  }
		     }
		     //column 7
		     if(i==1)
		     {
			    BO_rem_shippd[k]=0;
		     }
		     else
		     {
			 BO_rem_shippd[k]=ini_BO[k]-BO_shippd[k];
		     }
		     //column 8
		     inv_pos[k]=beg_inv[k]-BO_shippd[k];
		     //column 10
		     if(inv_pos[k]<dem[k])
		     qty_shippd[k]=inv_pos[k];
		     else
		     qty_shippd[k]=dem[k];
		     //column 11
		     tot_qty_shippd[k]=qty_shippd[k]+BO_shippd[k];
		     if(k!=4)
		     tot_qty_shippdArray[i][k-1]=tot_qty_shippd[k];
		     //column 12
		     end_inv_pos[k]=inv_pos[k]-qty_shippd[k];
		     //column 13
		     BO_end[k]=dem[k]-qty_shippd[k];
		     //column 15
		     

dem_up[k]=chromosome[j][k]+BO_end[k]-on_order_beg[k]-end_inv_pos[k];
		     if (k==1)
		     {
			     dem_up_sup[i]=dem_up[k];
		     }
		     //column 14
		     on_order_end[k]=dem_up[k]+on_order_beg[k];
		     //column 16
		     HC[k]=end_inv_pos[k]*holding_costs[k];
		     //column17
		     SC[k]=BO_end[k]*shortage_costs[k];
		     //column 18
		     TC[k]=HC[k]+SC[k];

		     t=TC[k]+t;
		     TSCC=TSCC+TC[k];
		     }
		     t=0;
		     }
		     }
		    cout<<"\ntscc:"<<TSCC;
}     
                     
int main()
{
    simulation c;
    c.declare();
    c.getdata();
    c.compute();
    c.generate();
    c.simulate();
    getch();
    return 0;
}



The for loop which is not executed is indicated in the program itself.Requesting your help!

Is This A Good Question/Topic? 0
  • +

Replies To: C++ loop problem

#2 Kilorn   User is offline

  • XNArchitect
  • member icon



Reputation: 1361
  • View blog
  • Posts: 3,534
  • Joined: 03-May 10

Re: C++ loop problem

Posted 30 August 2010 - 09:57 AM

One problem is that you're using the same control variable in all your nested loops. For each loop that you put inside another loop, you need to use a different control variable.

for (int i = 0; i < 2; i++)
{
    for (int x = 0; x < 2; x++)
    {
        for (int y = 0; y < 2; y++)
        {
            //do something...
        }
    }
}


This post has been edited by Kilorn: 30 August 2010 - 10:56 AM

Was This Post Helpful? 0
  • +
  • -

#3 taylorc8   User is offline

  • B&

Reputation: 150
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: C++ loop problem

Posted 30 August 2010 - 10:51 AM

for(int k=4;k<=1;k--)



I think this is your homework. However, it's so obvious..

k = 4
k <= 1
k--

k is 4, not less than or equal to 1.
Was This Post Helpful? 0
  • +
  • -

#4 Guest_Brinda2410*


Reputation:

Re: C++ loop problem

Posted 30 August 2010 - 10:21 PM

Thank you!!! That solved the loop problem
Was This Post Helpful? 0

Page 1 of 1