Overclock.net - Overclocking.net
     
 
Home Gallery Reviews Blogs Register Today's Posts Mark Forums Read Members List


Go Back   Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming

Reply
 
LinkBack Thread Tools
Old 07-07-06   #1 (permalink)
*cough* Stock *cough*
 
MangledWookie's Avatar
 
amd nvidia

Join Date: May 2005
Posts: 258

Rep: 28 MangledWookie is acknowledged by some
Unique Rep: 27
FAQs Submitted: 1
Trader Rating: 0
Default Baffled /w C++ code:

Im still trying to get used to C++ code and stuff. I can read it and usually understand it, but apprently i cant write it for crap

I thought that making a program that calculates primes wouuld be a decent 'getting used to it again' excercise but ive been left baffled.

Heres what i have right now:

Code:
#include <iostream.h>
#include <math.h>

int main(){
	int n;
	int i;
	int is_prime;

	is_prime = true;

	cout << "In ascending order, the prime numbers are: \n\n\n";

	for (n=3; n>0; n++)
	{
		for (i=2; i <= sqrt(double(n); i++) 
		{
			if (n%i == 0) 
				is_prime = false;
		}

		if (is_prime == true)
			cout << n << "\n";
	}
	return (0);
}
It compiles fine, but it just sits there, not giving any output.

If it something big, or you have a big suggestion for it, then post here - but if it just a small mistake, just PM me and spare me some humiliation : lachen :

Peace ppl
__________________
KDE Gnome

Hungry? check out my FAQ

System: My System
CPU
1.5Ghz P4 - Willamette
Motherboard
Acer S81M
Sound Card
Integrated
Power Supply
140W gayness
Case
Veritron 3300
OS
Windows XP pro - SP2
Monitor
17inch CRT (flickery)
MangledWookie is offline   Reply With Quote
Old 07-07-06   #2 (permalink)
<3 TB303
 
Xaimus's Avatar
 
amd nvidia

Join Date: Dec 2004
Location: Manhattan, KS
Posts: 575

Trader Rating: 0
Default

The innermost "if statement" that determines if a number is prime or not never halts its parent "for" loop. Also, is_prime is never reset.

Code:
#include <iostream>
#include <math.h>

using namespace std;

int main() {
	int n;
	int i;
	bool is_prime = true;

	cout << "In ascending order, the prime numbers are: \n\n\n";

	for (n=3; n>0; n++)
	{
		for (i=2; i <= sqrt(double(n); i++) 
		{
			if (n%i == 0) {
				is_prime = false;
				break;
			}
		}

		if (is_prime == true)
			cout << n << "\n";

		is_prime = true;
	}
	return (0);
}
Changes in bold are corrections to logic errors. Italic changes are minor stylistic revisions.
__________________
3030303030303030303 <33333333

System: Turd
CPU
Sempron 2800+ :(
Motherboard
GA7NF-RZ :(
Memory
1.25GB
Graphics Card
geForce 6800XT
Hard Drive
80GB + 120GB
Sound Card
M-Audio Audiophile 192 :D
Power Supply
500W Seasonic S12
Case
Black Antec P180 :D
OS
FreeBSD 6.2-RELEASE :D :D
Monitor
Two 17" CRTs
Xaimus is offline Overclocked Account   Reply With Quote
Old 07-08-06   #3 (permalink)
*cough* Stock *cough*
 
MangledWookie's Avatar
 
amd nvidia

Join Date: May 2005
Posts: 258

Rep: 28 MangledWookie is acknowledged by some
Unique Rep: 27
FAQs Submitted: 1
Trader Rating: 0
Default

Quote:
Originally Posted by Xaimus
The innermost "if statement" that determines if a number is prime or not never halts its parent "for" loop. Also, is_prime is never reset.

Code:
#include <iostream>
#include <math.h>

using namespace std;

int main() {
	int n;
	int i;
	bool is_prime = true;

	cout << "In ascending order, the prime numbers are: \n\n\n";

	for (n=3; n>0; n++)
	{
		for (i=2; i <= sqrt(double(n); i++) 
		{
			if (n%i == 0) {
				is_prime = false;
				break;
			}
		}

		if (is_prime == true)
			cout << n << "\n";

		is_prime = true;
	}
	return (0);
}
Changes in bold are corrections to logic errors. Italic changes are minor stylistic revisions.
Ah beautiful. Makes sense!. And thanks for noting the 'is_true' reset. I would have had a stroke figuring that out

Thanks alot - peace
__________________
KDE Gnome

Hungry? check out my FAQ

System: My System
CPU
1.5Ghz P4 - Willamette
Motherboard
Acer S81M
Sound Card
Integrated
Power Supply
140W gayness
Case
Veritron 3300
OS
Windows XP pro - SP2
Monitor
17inch CRT (flickery)
MangledWookie is offline   Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools



All times are GMT -4. The time now is 11:09 AM.


Overclock.net is a Carbon Neutral Site Creative Commons License Internet Security By ControlScan

Terms of Service / Forum Rules | Privacy Policy | Advertising | Become an Official Vendor
Copyright © 2008 Shogun Interactive Development. Most rights reserved.
Page generated in 0.19889 seconds with 8 queries