Logic problem with one program and a compiler problem with another

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 650 Views - Last Post: 23 September 2011 - 10:19 AM Rate Topic: -----

#1 Ghost344  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 02-September 11

Logic problem with one program and a compiler problem with another

Posted 23 September 2011 - 03:35 AM

Hello everyone!
I'm having a hard time figuring out the logic for a C program. It's a homework assignment and its variables are in Croatian so I hope that won't be a problem. The assignment is to make a program that requires user input of float numbers until the decimal part of a number is entered 3 times, and a function for multiplying the numbers who's whole part (don't know how you call it in English, basically the integer part) is uneven and a function that prints out the number with the smallest decimal part.

Here's how far I've gotten so far:
#include <stdio.h>

float produktNeparnih (float *broj, int i) {
	int j, produkt = 1; 
	
	for (j = 0; j < i; j++) {
		if ((int)broj[j] % 2 != 0)
			produkt *= broj[j];
	}
	
	return produkt;
}

float minimum (float *broj, int i) {
	int j; 
	float min = broj[0];
	
	for (j = 0; j < i; j++) {
		if ((broj[j+1] - (int)broj[j+1]) < (min - (int)min))
			min = broj[j+1];
			printf("%f\n", min);
	}

	return min;
}


void main () {
	float broj[100];
	int kontrola = 0, i, n = 0;

	
	do {
		scanf("%f", &broj[n]);
		for(i = 0; i < n; i++) {
			if ((broj[i] - (int)broj[i]) == (broj[n] - (int)broj[n])){
				kontrola++;
			}
		}
		n++;	
	} while (kontrola < 3);
	
	printf("Produkt neparnih: %f\n", produktNeparnih(broj, i));
	printf("Najmanji: %f\n", minimum(broj, i));
	
}


Problem #2:
This program works perfectly in Linux (compiled with GCC) but it doesn't work under Windows (Dev-Cpp compiler) and I don't have clue what's wrong with it. When I try to compile it in Dev-Cpp I get a "The system cannot find the file specified" error, and a friend of mine manages to compile it but when he runs it and enters only one character the program exits. Here's the code:

/*
napisi program koji unosi znakove sve dok prosjek  njihovih ASCII kodova ne prijedje 80, a zatim u posebnim funkcijama  izracunava prosjek ASCII kodova onih znakova koji su velika slova te  racuna broj samoglasnika.
*/

#include <stdio.h>

//funkcija izracunava prosjek ASCII velikih slova
int velikaSlova (char *array, int i) {
	int ukupno = 0, j, k = 0;
	
	for (j = 0; j < i; j++) {
		if ((int)array[j] > 64 && (int)array[j] < 91) {
			ukupno += (int)array[j];
			k++;
		}
	}

	printf("\nUkupno velikih slova: %d\n", k);
	
	//izbjegavanje djeljenja s nulom
	if (k == 0)
		return 0;
	
	return ukupno / k;
}

//funkcija racuna broj samoglasnika
int brojSamoglasnika (char *array, int i) {
	int ukupno = 0, j;
	
	for (j = 0; j < i; j++) {
		if(array[j] == 'a' ||
		   array[j] == 'A' ||
		   array[j] == 'e' ||
		   array[j] == 'E' ||
		   array[j] == 'i' ||
		   array[j] == 'I' ||
		   array[j] == 'o' ||
		   array[j] == 'O' ||
		   array[j] == 'u' ||
		   array[j] == 'U')
		    ukupno++;
		}
		
	return ukupno;
}

int main() {
	char unos[80]={0};
	int i = 0, j;
	int prosjek, ukupno = 0;
	
	//unos znakova
	do {
		printf("Unesi znak: ");
		scanf("%c", &unos[i]);
		getchar(); //fix za newline bug
		ukupno += (int)unos[i];
		i++;
	} while ((ukupno / i) < 80);
	
	
	//ispis svih unesenih znakova
	printf("\n");
	printf("Uneseni znakovi: (%d) ", i);
	
	for (j = 0; j < i; j++) {
		printf("\"%c\" ", unos[j]);
	}
	
	
	//ispisi
	printf("\n\n");
	printf("Ukupan zbroj ASCII: %d\n", ukupno);
	printf("Prosjek ASCII: %d\n", (ukupno / i));
	
	printf("ASCII prosjek velikih slova: %d\n", velikaSlova(unos, i));
	
	printf("\nUkupno samoglasnika: %d\n", brojSamoglasnika(unos, i));
	
	return 0;

}


Please note that I'm a beginner programmer, (and not that good with English) so I'd appreciate if you could give me advices on anything that you see wrong with the codes up there, doesn't have to be about the issues that I mentioned. :)

This post has been edited by Ghost344: 23 September 2011 - 03:41 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Logic problem with one program and a compiler problem with another

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6078
  • View blog
  • Posts: 23,549
  • Joined: 23-August 08

Re: Logic problem with one program and a compiler problem with another

Posted 23 September 2011 - 03:49 AM

Your English seems fine, but your reporting skills are lacking. What is wrong with the first problem? What file is the second program saying it can't find? Copy and paste any errors you are receiving (hopefully in English).
Was This Post Helpful? 0
  • +
  • -

#3 Ghost344  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 02-September 11

Re: Logic problem with one program and a compiler problem with another

Posted 23 September 2011 - 04:03 AM

Sorry about that.

In the first program the input has to stop when you input the same decimal part of number three times. For example if you enter:

1.14
2.14
3.14

it has to stop, but it doesn't. It stops only when three identical numbers are inputted. That's the part I can't figure out.

In the second program I get this:
http://img545.images...creenshotdm.png

It's on a virtual box but the same thing happens to me on normal Windows. But as I said a friend of mine managed to compile it (on the same compiler, in my defense I never really liked Dev-Cpp) but when he runs it and enters only one character the program exits.
OK, this isn't that important as the first one. I'll figure out the second one somehow.
Was This Post Helpful? 0
  • +
  • -

#4 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • Joined: 05-March 11

Re: Logic problem with one program and a compiler problem with another

Posted 23 September 2011 - 05:07 AM

Quote

Hello everyone!

Hey there. :)
Your English is quite good and easily understandable, actually.

Do you have to use Dev-C++? Try not to if possible because it is outdated. Use a more newer and frequently updated IDE such as Code::Blocks or Microsoft Visual C++.

Note: Code::Blocks is the name of the IDE, not some scary function/data-structure/variable etc. :P

Edit: Typo

This post has been edited by hulla: 23 September 2011 - 05:08 AM

Was This Post Helpful? 0
  • +
  • -

#5 Ghost344  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 02-September 11

Re: Logic problem with one program and a compiler problem with another

Posted 23 September 2011 - 05:25 AM

Actually no, that's the compiler my school uses and the code will have to be compiled there too. I use GCC in my home and all the programs until now where transferable to Dev-Cpp. Do you use those compilers you listed? Could you please try the second code I posted there if it works? Basically what the program does is you enter characters until the average ASCII value of all the characters exceeds 80 and it also prints out the average ASCII of the upper case letters inputted, and the number of vowels.
If it's not too much to ask of course :)
Was This Post Helpful? 0
  • +
  • -

#6 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • Joined: 05-March 11

Re: Logic problem with one program and a compiler problem with another

Posted 23 September 2011 - 05:53 AM

Yes I use Code::Blocks. I rarely use MSVC++ because it is laggy for me.

It compiles on Code::Blocks 10.05.
For me, it only accepts one character.
It prints the ASCII of the first char I enter if I enter a string (naturally this would happen).
It allows me to enter multiple characters if I enter a number first.
It returns with 0 (successful termination of the program).
Hope this helps . . .

Oh BTW, laggy means that something runs slowly.

This post has been edited by hulla: 23 September 2011 - 05:52 AM

Was This Post Helpful? 1
  • +
  • -

#7 Ghost344  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 02-September 11

Re: Logic problem with one program and a compiler problem with another

Posted 23 September 2011 - 06:34 AM

OMG.. I just realized why it does so. If you input the character 'a' it's ASCII value is 97 (and the average would automatically be 97 so more than 80 and it will end the program). Silly me :D
If you want you can enter the character '0' a few times so you can add more characters before the average goes over 80 :)
And thanks a lot for trying the code :)

EDIT: and that's basically what you said, didn't get it at first lol
So only the first program is left.

This post has been edited by Ghost344: 23 September 2011 - 06:37 AM

Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is offline

  • member icon


Reputation: 4142
  • View blog
  • Posts: 12,892
  • Joined: 25-December 09

Re: Logic problem with one program and a compiler problem with another

Posted 23 September 2011 - 07:08 AM

Look closely at the following snippet:
	do {
		scanf("%f", &broj[n]);
		for(i = 0; i < n; i++) {
			if ((broj[i] - (int)broj[i]) == (broj[n] - (int)broj[n])){
				kontrola++;
			}
		}
		n++;

You say:

Quote

In the first program the input has to stop when you input the same decimal part of number three times. For example if you enter:

So you need to compare three numbers, how can you compare three numbers if you only have one or two? Maybe you should wait until after you enter the third number to check if the last 3 numbers are the same? Then you would check the current number against the previous number and the one previous to that.


Jim
Was This Post Helpful? 1
  • +
  • -

#9 Ghost344  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 02-September 11

Re: Logic problem with one program and a compiler problem with another

Posted 23 September 2011 - 07:24 AM

You are right, I've added an if statement that regulates that but the program still doesn't work as it's suppose to. I can enter as many same decimal parts of a float number as I like but it stops with the input only when I enter three same integer parts of the number.
This part is the problem and I don't know how to fix it:
if ((broj[i] - (int)broj[i]) == (broj[n] - (int)broj[n]))


If I enter, for example, the number 3.14, isn't it suppose to
broj[i] == 3.14
(int)broj[i] == 3
broj[i] - (int)broj[i] == .14
?
Was This Post Helpful? 0
  • +
  • -

#10 jimblumberg  Icon User is offline

  • member icon


Reputation: 4142
  • View blog
  • Posts: 12,892
  • Joined: 25-December 09

Re: Logic problem with one program and a compiler problem with another

Posted 23 September 2011 - 07:29 AM

Post your current code.

Jim

This post has been edited by jimblumberg: 23 September 2011 - 07:29 AM

Was This Post Helpful? 0
  • +
  • -

#11 Ghost344  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 02-September 11

Re: Logic problem with one program and a compiler problem with another

Posted 23 September 2011 - 07:32 AM

View Postjimblumberg, on 23 September 2011 - 07:29 AM, said:

Post your current code.

Jim


Here you go!

#include <stdio.h>

float produktNeparnih (float *broj, int i) {
	int j, produkt = 1; 
	
	for (j = 0; j <= i; j++) {
		if ((int)broj[j] % 2 != 0)
			produkt *= broj[j];
	}
	
	return produkt;
}

float minimum (float *broj, int i) {
	int j; 
	float min = broj[0];
	
	for (j = 0; j < i; j++) {
		if ((broj[j+1] - (int)broj[j+1]) < (min - (int)min))
			min = broj[j+1];
		//	printf("%f\n", min);
	}

	return min;
}


void main () {
	float broj[100];
	int kontrola = 0, i, n = 0;

	
	do {
		scanf("%f", &broj[n]);
		if (n > 2) {
			for(i = 0; i < n; i++) {
				if ((broj[i] - (int)broj[i]) == (broj[n] - (int)broj[n])){
					kontrola++;
				}
			} 	
		}
		n++;	
	} while (kontrola < 3);
	
	printf("Produkt neparnih: %f\n", produktNeparnih(broj, i));
	printf("Najmanji: %f\n", minimum(broj, i));
	
}

This post has been edited by Ghost344: 23 September 2011 - 07:37 AM

Was This Post Helpful? 0
  • +
  • -

#12 jimblumberg  Icon User is offline

  • member icon


Reputation: 4142
  • View blog
  • Posts: 12,892
  • Joined: 25-December 09

Re: Logic problem with one program and a compiler problem with another

Posted 23 September 2011 - 08:13 AM

Your problem is probably happening because of floating point round off errors. Run your program through your debugger watching the values of broj[i] and broj[n] in your loop. For example I entered 6.32 and 8.32 but in my debugger these variables were actually 6.32000017 and 8.31999969 because of round off errors. And indeed when I compiled the code the compiler did warn me about this possibility:

Quote

main.cpp|38|warning: comparing floating point with == or != is unsafe


Jim
Was This Post Helpful? 1
  • +
  • -

#13 Ghost344  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 02-September 11

Re: Logic problem with one program and a compiler problem with another

Posted 23 September 2011 - 08:19 AM

Which compiler did you use? Is it somehow possible to round the decimal part of the float number to just 2 or 3 spaces? I'm thinking on multiplying the decimal part with 10 or 100 and turning that number into an integer but is there another way, maybe more efficient?
Was This Post Helpful? 0
  • +
  • -

#14 jimblumberg  Icon User is offline

  • member icon


Reputation: 4142
  • View blog
  • Posts: 12,892
  • Joined: 25-December 09

Re: Logic problem with one program and a compiler problem with another

Posted 23 September 2011 - 08:42 AM

That should work as long as you remember that when you convert to a integer the number is truncated, not rounded up or down. For the numbers I entered if you just multiply by 100 you will end up with 632 and 831 so you will need to round up before you multiply and convert to an int. Adding .005 before the multiplication should do the trick. See this link for a good explanation and other ways to compare floating point numbers.

Quote

Which compiler did you use?

I use gcc on Linux, with quite a few error/warning messages enabled.

Jim

This post has been edited by jimblumberg: 23 September 2011 - 08:43 AM

Was This Post Helpful? 1
  • +
  • -

#15 Ghost344  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 02-September 11

Re: Logic problem with one program and a compiler problem with another

Posted 23 September 2011 - 09:45 AM

View Postjimblumberg, on 23 September 2011 - 08:42 AM, said:

I use gcc on Linux, with quite a few error/warning messages enabled.


I will have to look that up :D
OK, after adding that last part and fixing a few mistakes the final program works like a charm :)
/*
napisi program koji unosi realne brojeve sve dok ne unese tri puta broj sa istim decimalnim dijelom, a zatim u posebnim funkcijama nalazi produkt brojeva sa neparnim cijelim dijelom  te broj sa najmanjim decimalnim djelom.
*/

#include <stdio.h>

float produktNeparnih (float *broj, int i) {
	int j; 
	float produkt = 1; 
	
	for (j = 0; j <= i; j++) {
		if ((int)broj[j] % 2 != 0)
			produkt *= broj[j];
	}
	
	return produkt;
}

float minimum (float *broj, int i) {
	int j; 
	float min = broj[0];
	
	for (j = 0; j < i; j++) {
		if ((broj[j+1] - (int)broj[j+1]) < (min - (int)min))
			min = broj[j+1];
	}

	return min;
}


void main () {
	float broj[100];
	int kontrola, i, n = 0, jedan[100], dva[100];

	
	do {
		scanf("%f", &broj[n]);
		kontrola = 0;
		if (n > 1) {
			for(i = 0; i < n; i++) {
				if ((int)(((broj[i] - (int)broj[i]) + .005) * 100) == (int)(((broj[n] - (int)broj[n]) + .005) * 100)){
					kontrola++;
				}
			} 	
		}
		n++;	
	} while (kontrola < 2);
	
	printf("Produkt neparnih: %f\n", produktNeparnih(broj, i));
	printf("Najmanji: %f\n", minimum(broj, i));
	
}


Thank you all for helping! Couldn't do it without you <3
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2