Need help

Thread Starter

system0

Joined Mar 21, 2009
3
i have this project in c programming language

1.I have to input a float number which after calculating returns me a 32-bit
note stored in a 32 bit registry .

it has been put to my attention that the --for (int i=0;i>=1;i--) -- part of the code is not being caried out , so can anyone help me fix this problem
Code:

#include<stdio.h>

int main(){
char binbroj[32];
int predznak = 1;
int pot=0;
float realniBroj,frac;
int E;

printf("Unesi realni broj");
scanf("%f",&realniBroj);
if (realniBroj < 0)
{
predznak = -1;
frac = realniBroj * predznak;
binbroj[0] ='1';
}
if (frac < 1 )
{
while (frac < 1)
{
frac *= 2.0;
pot--;
}
}
else {
while (frac > 2)
{
frac /= 2.0;
pot++;
}
}
E = pot * 127 ;
for (int i=0;i>=1;i--)
{

if (E % 2 == 0)
binbroj = '0';
else
binbroj ='1';
E /= 2;
}
frac -= 1;
for (i=9;i<=31;i++){
frac *= 2.0;
if (frac >=1) {
binbroj = '1';
frac -= 1.0;

} else binbroj ='0';
}


printf("float number %f in registry has a binary note \n",realniBroj);

for(i=0;i<32;i++)
{
if(i==1) printf (" ");
if(i==9) printf (" ");
printf("%c",binbroj);
}
}
 

SgtWookie

Joined Jul 17, 2007
22,230
You need to declare what kind of variable "i" is. Also, you did not initialize your float variables. While most compilers will zero the contents for you, it is a good practice to initialize them yourself.

I put a couple of notes in your code, along with indenting it.
Rich (BB code):
#include<stdio.h>

int main(){
	char binbroj[32];
	int predznak = 1;
	int pot=0;
	float realniBroj=0.0,frac=0.0;
	int E;

	printf("Unesi realni broj");
	scanf("%f",&realniBroj);
	if (realniBroj < 0)
	{
		predznak = -1;
		frac = realniBroj * predznak;
		binbroj[0] ='1';
	}
	if (frac < 1 )
	{
		while (frac < 1)	// If frac starts off being zero or negative, this will loop infinitely.
		{
			frac *= 2.0;
			pot--;
		}
	}
	else {
		while (frac > 2)
		{
			frac /= 2.0;
			pot++;
		}
	}
	E = pot * 127 ;
	for (int i=0;i>=1;i--)	// "i" has not been declared.  Since i is initialized to 0, i>=1 will never be satisfied.
	{

		if (E % 2 == 0)
		binbroj = '0';
		else
		binbroj ='1';
		E /= 2;
	}
	frac -= 1;
	for (i=9;i<=31;i++){
		frac *= 2.0;
		if (frac >=1) {
			binbroj = '1';
			frac -= 1.0;

		} else binbroj ='0';
	}


	printf("float number %f in registry has a binary note \n",realniBroj);

	for(i=0;i<32;i++)
	{
		if(i==1) printf (" ");
		if(i==9) printf (" ");
		printf("%c",binbroj);
	}
}
 

DumboFixer

Joined Feb 10, 2009
217
As SgtWookie pointed out in his comments, there is a problem with the line

"for (int i=0;i>=1;i--)".

I has not been declared.

With i initialised to 0, then condition i >= 0 will not be satisfied. also did you mean i-- ?
 

SgtWookie

Joined Jul 17, 2007
22,230
Can someone help me make the code work.
Im a begginner ...
I'm not sure what you're attempting to accomplish with this program.

The prompts and variable names are not in English; therefore do not make much sense to me.

This: for (int i=0;i>=1;i--)
Why didn't you declare "int i;" at the start of the program?
It should look more like:
for (i=0;i<=1;i++)

Near the end of the listing, you have this: printf (" ");
That will output a space, keeping the cursor on the same line.
If you meant to advance to a new line, do this instead: printf ("\n");
 
Last edited:

Mark44

Joined Nov 26, 2007
628
Elaborating on what Wookie and others have said...

In your first post, you said
it has been put to my attention that the --for (int i=0;i>=1;i--) -- part of the code is not being caried out , so can anyone help me fix this problem
The body of this for loop will never execute. The three expressions inside the parentheses and separated by semicolons are (1) the initialization expresssion, (2) the test expression, and (3) loop counter increment or decrement. Things work this way. The initialization expression is evaluated once, before anything else happens. The test expression in the middle is evaluated each time before the body of the loop is executed. If the test expression is true, the loop body executes. If the test expression is false, the loop body does not execute. After the loop body executes or doesn't execute, the increment/decrement part is evaluated.

In your loop, a local variable i is created on the stack, and is initialized to 0. The test expression evaluates i >= 1, and finds that it is false, so the body of the loop is not executed.

In the for loop you asked about ( for (int i = 0; i >= 1; i--) ), the loop control variable's scope is this loop. The other for loops you have use undeclared variables. Although they also use the name i, there is no connection between them and the loop control variable in this loop. You should either declare them in each loop (that is, for (i = 0; ...)) or declare i once at the beginning of your code, near the top of the main function.

Hope that helps.
 
Top