473,503 Members | 1,691 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

For loop not executing proper number of times

25 New Member
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>

Expand|Select|Wrap|Line Numbers
  1. #include <iostream.h>
  2. int main()
  3. {
  4.     //clrscr();
  5.     float earlylatespc=0.5,remcodephase=0,tcode;
  6.     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};
  7.     int   earlycode[50];
  8.     float codephasestep,check;
  9.     float ptcode[1000];
  10.     int promptcode[50],i_pcode=0,i_tcode=0;
  11.  
  12. codephasestep=(float)1023/(float)38192;
  13. check=((blksize-1)*codephasestep+remcodephase);
  14.             cout<<"check is"<<check<<endl;
  15.             cout<<endl;
  16.             for(tcode=remcodephase;tcode<=check;)
  17.             {
  18.                                 ptcode[i_tcode]=tcode;                                                                                          
  19.             tcode2=ceil(tcode)+1;
  20.             promptcode[i_pcode]=cacode[tcode2];     //check here later coz may be u should make it cacode[tcode2-1];                                
  21.            i_pcode+=1;
  22.            count1+=1; //to check no of iterations
  23.            i_tcode+=1;
  24.             tcode=tcode+codephasestep;
  25.             cout<<"tcodes are"<<tcode<<endl;
  26.             }
  27.             cout<<endl;
  28.             cout<<"final tcode"<<tcode<<endl;  
  29.             cout<<"final count"<<count1<<endl;  
  30.             cout<<endl; 
  31.             for(i_pcode=0;i_pcode<count1;i_pcode++)
  32.             {
  33.               cout<<promptcode[i_pcode]<<endl;                       
  34.  
  35.             }
  36.             cout<<endl;
  37.             for(i_tcode=0;i_tcode<count1;i_tcode++)
  38.             {
  39.               cout<<ptcode[i_tcode]<<endl;                       
  40.  
  41.             }
  42.             cout<<"count"<<count1<<endl;
  43.             cout<<"the 50th term is"<<ptcode[blksize]<<endl;
  44.             //remCodePhase = (tcode(blksize) + codePhaseStep) - 1023.0;
  45.  
  46.  
  47.             remcodephase = (ptcode[blksize] + codephasestep) ;//- 1023.0;         // taken as ptcode[] instead of tcode[]
  48.                                                                                   //He wants the last term
  49.     cout<<"remcodephase is"<<remcodephase<<endl;
  50.  
  51.  
  52.     getchar();
  53.     return 0;
  54.     }
If everything works fine, the output of remcodephase should be "1.3392857"
Thanks,
prads
Oct 29 '07 #1
1 1438
beacon
579 Contributor
Have you used any breakpoints to determine what the values of some of your variables are to help you debug this? I didn't compile it, but just from taking a glance at it, and keep in mind that I haven't been using C++ for very long, it would appear as though perhaps your variables either haven't been initialized to the value that is needed to obtain the 50th position.

If you initialized to zero, remember that the 50th position will actually be the 49th memory location. If you initialize to 1 then 50 will be the 50th position. I could be way off base, but I'm definitely not trying to be condescending if you knew that already.

If you walk through your program using the breakpoints, you may notice that one of your values should be something else at a given location and that is what is throwing off the calculation.

Hope this helps.
Oct 29 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

47
12023
by: Mountain Bikn' Guy | last post by:
Take some standard code such as shown below. It simply loops to add up a series of terms and it produces the correct result. // sum numbers with a loop public int DoSumLooping(int iterations) {...
50
16614
by: Mountain Bikn' Guy | last post by:
Take some standard code such as shown below. It simply loops to add up a series of terms and it produces the correct result. // sum numbers with a loop public int DoSumLooping(int iterations) {...
8
1669
by: Microsoft | last post by:
I have the following loop the length contains somewhere around 1000+ items: For elemIndex = 0 To len - 1 elem = CType(doc.all.item(elemIndex), mshtml.IHTMLElement) If elem.tagName = "A" Then If...
2
19288
ADezii
by: ADezii | last post by:
If you are executing a code segment for a fixed number of iterations, always use a For...Next Loop instead of a Do...Loop, since it is significantly faster. Each pass through a Do...Loop that...
0
7203
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7282
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7339
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7463
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5581
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5017
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1515
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
738
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.