| View previous topic :: View next topic |
| Author |
Message |
M.CORP Grandmaster Cheater Supreme
Reputation: 28
Joined: 28 Oct 2009 Posts: 1010
|
Posted: Sun Feb 20, 2011 2:18 am Post subject: Finnally! |
|
|
Removed all the 'goto's.
| Code: | // random0.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <windows.h>
#include <iostream>
void prime_num(int num);
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
//Intro...
int num, z = 1;
int msgbox = MessageBox(NULL, L"Greetings, welcome to this application designed to scan for Prime Numbers!\n~Q.A.I. (a.k.a Quantum Artificial Intelligense)", L"Run", MB_ICONINFORMATION);
//Do the loop :D
while (true){
cout << "Enter a number below and i shall calculate all the prime numbers from up to that number!\n";
cin >> num;
prime_num(num);
cin.get();
cout << "\nWant to scan for prime numbers again?\n1 for yes, 2 for no.\n";
int a;
cin >> a;
cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
if (a != 1){
{
break;
}
}
}
return 0;
}
void prime_num(int num){
bool isPrime = true;
for (int i = 2; i < num; i++){
for (int j = 2; j < i; j++){
if (i % j == 0){
isPrime = false;
break;
}
}
if (isPrime){
cout << i << " is a prime number.\n";
}
isPrime = true;
}
}
|
Version 2:
| Code: | // random0.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <windows.h>
#include <iostream>
void prime_num(int num, int max);
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
//Intro...
int num, z = 1, max;
int msgbox = MessageBox(NULL, L"Greetings, welcome to this application designed to scan for Prime Numbers!\n~Q.A.I. (a.k.a Quantum Artificial Intelligense)", L"Run", MB_ICONINFORMATION);
//Do the loop :D
while (true){
cout << "Enter a number to start from: \n";
cin >> num;
cout << "Enter the number to scan up to (Must be higher than the one above to start from!): \n";
cin >> max;
prime_num(num, max);
cin.get();
cout << "\nWant to scan for prime numbers again?\n1 for yes, 2 for no.\n";
int a;
cin >> a;
cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
if (a != 1){
{
break;
}
}
}
return 0;
}
void prime_num(int num, int max){
bool isPrime = true;
for (int i = num; i < max; i++){
for (int j = max; j < i; j++){
if (i % j == 0){
isPrime = false;
break;
}
}
if (isPrime){
cout << i << " is a prime number.\n";
}
isPrime = true;
}
} |
_________________
Last edited by M.CORP on Fri Feb 25, 2011 12:38 am; edited 3 times in total |
|
| Back to top |
|
 |
Polynomial Grandmaster Cheater
Reputation: 5
Joined: 17 Feb 2008 Posts: 524 Location: Inside the Intel CET shadow stack
|
Posted: Sun Feb 20, 2011 7:19 am Post subject: |
|
|
| Quote: | | Also, i'm also aware of the 'goto' lines, i know that they are bad, but they aren't, as long as it's not a big project. |
... no. Using 'goto' is bad because it violates the basic structure of a program. You could have quite easily avoided it using a while or for loop.
Also, check out the 6k+/-1 seive to provide a significant performance increase over your current code.
In other notes, quantum what? Also, "intelligense"? *chuckles*
Other than that, it's vaguely acceptable code. You should really avoid those horrible gotos, and the switch/case is useless visual overhead when you have 1 case (just use an if).
_________________
It's not fun unless every exploit mitigation is enabled.
Please do not reply to my posts with LLM-generated slop; I consider it to be an insult to my time. |
|
| Back to top |
|
 |
Slugsnack Grandmaster Cheater Supreme
Reputation: 71
Joined: 24 Jan 2007 Posts: 1857
|
Posted: Sun Feb 20, 2011 1:01 pm Post subject: |
|
|
| Why do you bother putting breaks after your goto statements ?
|
|
| Back to top |
|
 |
M.CORP Grandmaster Cheater Supreme
Reputation: 28
Joined: 28 Oct 2009 Posts: 1010
|
Posted: Sun Feb 20, 2011 9:26 pm Post subject: |
|
|
| Slugsnack wrote: | | Why do you bother putting breaks after your goto statements ? |
You mean on the cases?
If so, i read it on the cplusplus.com's tutorials on switch cases.
If not, i suppose you are directing to the 'goto exit' statement?
EDIT:
Removed those evit 'goto's.
Is there any other way to print out '\n' multiple times like in python, you just type the following code in and it prints the string how many times.
Also, any other things i should i avoid?
_________________
|
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Mon Feb 21, 2011 1:40 am Post subject: |
|
|
Here's another example using caching for the prime numbers:
| Code: |
//
// main.cpp - Prime Number Example
//
//
// Caching method based on Java implementation by 'starblue' from:
// http://stackoverflow.com/questions/1042902/most-elegant-way-to-generate-prime-numbers/1043247#1043247
//
#include <Windows.h>
#undef max // Template bug.
#include <iostream>
#include <bitset>
int __cdecl main( int argc, TCHAR* argv[] )
{
// Prepare prime number cache.
std::bitset< 10000 > bsPrimeNumbers = 0;
bsPrimeNumbers.flip();
bsPrimeNumbers.set( 0, false );
bsPrimeNumbers.set( 1, false );
// Create prime number cache.
for( size_t x = 0; x * x < bsPrimeNumbers.size(); x++ )
{
if( bsPrimeNumbers.test( x ) )
{
for( size_t y = x * x; y < bsPrimeNumbers.size(); y += x )
bsPrimeNumbers.set( y, false );
}
}
// Variables..
char szOutput[ 2 ][ 256 ] =
{
{ " is not a prime number.\n" },
{ " is a prime number!\n" }
};
char szInput[ 256 ] = { 0 };
int nNumber = 0;
// Loop input.
while( true )
{
std::cout << "\nEnter a prime number, or enter exit to quit: ";
// Exit if invalid or user wishes to..
if( !( std::cin >> szInput ) || _stricmp( szInput, "exit" ) == 0 )
break;
if( ( nNumber = atoi( szInput ) ) == 0 || nNumber >= static_cast< int >( bsPrimeNumbers.size() ) )
{
std::cout << "Invalid number specified, please try again.\n";
}
else
std::cout << nNumber << szOutput[ bsPrimeNumbers.test( nNumber ) ];
// Cleanup the buffer..
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
}
return 0;
} |
I'd suggest using caching if you were going to constantly need to lookup prime numbers. No point in doing loops over and over if you can just store the results once and use it from there. I used a bitset in my example, which is based on the link given at the top of the source.
Some other notes:
- You're in a console application, there is no reason to use MessageBox.
- You call cin.get(); after reading the prime, don't.
_________________
- Retired. |
|
| Back to top |
|
 |
M.CORP Grandmaster Cheater Supreme
Reputation: 28
Joined: 28 Oct 2009 Posts: 1010
|
Posted: Tue Feb 22, 2011 8:53 pm Post subject: |
|
|
Why can't i use cin.get(); after reading prime?
_________________
|
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Wed Feb 23, 2011 4:57 pm Post subject: |
|
|
| +=Marvin=+ wrote: | | Why can't i use cin.get(); after reading prime? |
There's no reason to pause the console between the first time and asking the user if they want to continue. Your next lines that follow go and read the console again so you are performing more work then is needed.
_________________
- Retired. |
|
| Back to top |
|
 |
M.CORP Grandmaster Cheater Supreme
Reputation: 28
Joined: 28 Oct 2009 Posts: 1010
|
Posted: Fri Feb 25, 2011 12:36 am Post subject: |
|
|
Ok, thanks I made another one
This time, it i added the number to start from and the number to stop printing primenumber.
_________________
|
|
| Back to top |
|
 |
HomerSexual Grandmaster Cheater Supreme
Reputation: 5
Joined: 03 Feb 2007 Posts: 1657
|
Posted: Fri Feb 25, 2011 4:41 am Post subject: |
|
|
While I'm a fan in recycling other's code, you did not comment where you got the isPrime function from. Before you try and say that you didn't copy/paste that, look at the code style versus the main method. In main, braces have new lines and the indentation is pretty ugly; but in the isPrime, braces are on the same line and the code is generally much prettier (people don't do that unless they're blatantly dumb).
I hope that you understand how the algorithm works before just copying it over and you SHOULD provide a link in the comments to where you got it.
_________________
|
|
| Back to top |
|
 |
M.CORP Grandmaster Cheater Supreme
Reputation: 28
Joined: 28 Oct 2009 Posts: 1010
|
Posted: Fri Feb 25, 2011 7:23 pm Post subject: |
|
|
Why would i copy and paste?
| Code: | void prime_num(int num, int max){
bool isPrime = true;
for (int i = num; i < max; i++){
for (int j = max; j < i; j++){
if (i % j == 0){
isPrime = false;
break;
}
}
if (isPrime){
cout << i << " is a prime number.\n";
}
isPrime = true;
}
} |
On the main function, i had to edit it to add the while loop and the option that end the program. I had to insert it to prevent the program from ending right away after it scans for prime numbers.
The function has better code indents because i didn't have to insert extra codes (if , else, while, etc.) to prevent errors.
I tend to forget the tab indents because that's how i code. And tab indents don't really interfere with the project unless you are super organized.
_________________
|
|
| Back to top |
|
 |
HomerSexual Grandmaster Cheater Supreme
Reputation: 5
Joined: 03 Feb 2007 Posts: 1657
|
Posted: Fri Feb 25, 2011 11:34 pm Post subject: |
|
|
| +=Marvin=+ wrote: | Why would i copy and paste?
| Code: | void prime_num(int num, int max){
bool isPrime = true;
for (int i = num; i < max; i++){
for (int j = max; j < i; j++){
if (i % j == 0){
isPrime = false;
break;
}
}
if (isPrime){
cout << i << " is a prime number.\n";
}
isPrime = true;
}
} |
On the main function, i had to edit it to add the while loop and the option that end the program. I had to insert it to prevent the program from ending right away after it scans for prime numbers.
The function has better code indents because i didn't have to insert extra codes (if , else, while, etc.) to prevent errors.
I tend to forget the tab indents because that's how i code. And tab indents don't really interfere with the project unless you are super organized. |
Maybe you didn't rip it, but I've never seen someone's code style vary by so much between 2 methods, it just doesn't happen.
Anyways, My point is that coding practices ARE important. You should learn to organize / comment code as much as you can. Starting to do so now will really help you down the road if it's something you want to pursue.
Dear God, what has school done to me?
_________________
|
|
| Back to top |
|
 |
M.CORP Grandmaster Cheater Supreme
Reputation: 28
Joined: 28 Oct 2009 Posts: 1010
|
Posted: Fri Feb 25, 2011 11:49 pm Post subject: |
|
|
Ok, thanks.
I'll add some comments soon. If i have free time.
_________________
|
|
| Back to top |
|
 |
|