473,403 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Loop counter

Parul Bagadia
188 100+
I have written a function for converting a decimal no. in binary no.
Here what happens is in convert function my j turns out to be 1244860..which i got to know by debugger. Before going in for loop;its value is 0, in for loop value of i-1 is 3...but value of j randomly becomes 1224860.
Hows that happening?
I did the program in Microsoft Visual Studio C++ 6.0
#include<stdio.h>
conv(int d1,int d2)
{
int rem=0,qut=1,i=0,j=0;
int store[20]={0};
while(qut!=0)
{
rem=d1%2;
qut=d1/2;
d1=qut;
store[i]=rem;
i++;
}
for(j=i-1;j>0,i>0;j--,i--)
{
printf("\n%d",store[i]);
}
}
void main()
{
int no1=0,no2=0;
printf("\nEnter first decimal no.\n");
scanf("\n%d",&no1);
printf("\nEnter second decimal no.\n");
scanf("\n%d",&no2);
conv(no1,no2);
}
May 14 '08 #1
1 1566
weaknessforcats
9,208 Expert Mod 8TB
Why not just:
Expand|Select|Wrap|Line Numbers
  1. for(j = 0; j < i; ++j)
  2. {
  3.      printf("\n%d",store[j]);
  4. }
  5.  
?

BTW: I assume this printf is a debug display. If it is, stop doing that because you have to change the code to take the display out and then you are no longer sure the code still works. Use your debugger only.

If it is not a debug display, then take the display out and return a string with the binary values.
May 14 '08 #2

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

Similar topics

7
by: Delphi Sleuth | last post by:
what the heck am I not getting. This little snipet doesn't do what I thought it was going to do. Can anyone tell me why? I expected 2 Hello: 102 All I get is the 2. I had been trying to...
1
by: pauljturner99 | last post by:
Hi, I'm trying to pass a parameter from a for loop to the nested while loop but only the first counter is passed. Here is the code: dim ctr redim ctr(5) ctr(0) = 2 ctr(1) = 4 ctr(2) = 6
16
by: Claudio Grondi | last post by:
Sometimes it is known in advance, that the time spent in a loop will be in order of minutes or even hours, so it makes sense to optimize each element in the loop to make it run faster. One of...
52
by: MP | last post by:
Hi trying to begin to learn database using vb6, ado/adox, mdb format, sql (not using access...just mdb format via ado) i need to group the values of multiple fields - get their possible...
0
by: Gary Herron | last post by:
Dan Upton wrote: Yes. Create a list of keys, and loop through it: pids = procs_dict.keys() for pid in pids: if procs_dict.poll() != None # do the counter updates del procs_dict Then the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...
0
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...

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.