Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,548 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,557 people online right now. Registration is fast and FREE... Join Now!




Pls help with this c++ pgm

 
Reply to this topicStart new topic

Pls help with this c++ pgm, for loop not executing enough no. of times

prads
29 Oct, 2007 - 12:58 PM
Post #1

D.I.C Head
**

Joined: 22 Oct, 2007
Posts: 111


My Contributions
Hello,
I have written the below code, which should rotate within the first "for" loop 50 times but dont know for what reason its iterating only 49 times..? Can anyone pls find out what the mistake is and reply with correction. U can also run this on ur compiler and check!

//[code]

#include <iostream.h>
int main()
{
//clrscr();
float earlylatespc=0.5,remcodephase=0,tcode;
int blksize=50,i_ecode=0,count1=0,tcode2,cacode[15]={1,2,1,9,4,5,4,5,7,7,8,7,5,4,7};

int earlycode[50];
float codephasestep,check;
float ptcode[1000];
int promptcode[50],i_pcode=0,i_tcode=0;

codephasestep=(float)1023/(float)38192;
check=((blksize-1)*codephasestep+remcodephase);
cout<<"check is"<<check<<endl;
cout<<endl;
for(tcode=remcodephase;tcode<=check;)
{
ptcode[i_tcode]=tcode;
tcode2=ceil(tcode)+1;
promptcode[i_pcode]=cacode[tcode2]; //check here later coz may be u should make it cacode[tcode2-1];
i_pcode+=1;
count1+=1; //to check no of iterations
i_tcode+=1;
tcode=tcode+codephasestep;
cout<<"tcodes are"<<tcode<<endl;
}
cout<<endl;
cout<<"final tcode"<<tcode<<endl;
cout<<"final count"<<count1<<endl;
cout<<endl;
for(i_pcode=0;i_pcode<count1;i_pcode++)
{
cout<<promptcode[i_pcode]<<endl;

}
cout<<endl;
for(i_tcode=0;i_tcode<count1;i_tcode++)
{
cout<<ptcode[i_tcode]<<endl;

}
cout<<"count"<<count1<<endl;
cout<<"the 50th term is"<<ptcode[blksize]<<endl;
//remCodePhase = (tcode(blksize) + codePhaseStep) - 1023.0;


remcodephase = (ptcode[blksize] + codephasestep) ;//- 1023.0; // taken as ptcode[] instead of tcode[]
//He wants the last term
cout<<"remcodephase is"<<remcodephase<<endl;


getchar();
return 0;
}

If everything works fine, the output of remcodephase should be "1.3392857"
Thanks,
prads

User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Pls Help With This C++ Pgm
29 Oct, 2007 - 01:33 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,655



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
In the future please make sure your code tags are working (not commented out). The problem with it going only 49 times is because that is what you are telling it to do. check=((blksize-1)*codephasestep+remcodephase); Notice the minus one on the size... 50 - 1 is 49. So your value of check is based on codephasestep being multiplied 49 times. So get rid of the -1 and you will get your 1.33929 value for check and you get the 50 iterations.

Enjoy!

"At DIC we be coding ninjas!" decap.gif

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 09:37PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month