Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 11th, 2005, 2:27 PM   #1
Konnor
Newbie
 
Join Date: Aug 2005
Posts: 20
Rep Power: 0 Konnor is on a distinguished road
Prime Numbers

This exercise took me donkey's years to do (I am quite new to this). I have a solution, but for some reason, '1' isn't picked up as a prime number.

The exercise is this (taken from Bruce Eckel's guide, chapter 3)

2. Write a program that uses two nested for loops and the modulus operator (%) to detect and print prime numbers.

This is my solution, with the numbers 1 - 100

int main(int nNumberofArgs, char* pszArgs[])
{
    int number = 0;
    
    for (int i = 0; i <= 100; i++)
    {
        int modulus = 1;
        int primecount = 0;
        
        for (int i = 0; i <= number; i++)
        {
                double answer = number % modulus;
                        if (answer == 0)
                        {
                          primecount++;
                        }  
                modulus++;
        }        
    
        if (primecount == 2)
        {
            cout << number << endl;
        } 
        number++;
    }
    
    system("PAUSE");
    return 0; 
}

Any tips on how to do this better would be much appreciated, as my code doesn't 'feel' too clean (the variable's and there positions in the program seem wrong). Can it be done with just for loops?

Also, is it normal to struggle with these exercises so early on? I am trying to read through the chapters and then attempt the tasks, but occasionally find certain tasks very difficult and time consuming, thus making me frustrated and slowing down the learning process. Is the best way to tackle this guide to work through all the exercises in each chapter?

Cheers,
Konnor is offline   Reply With Quote
Old Sep 11th, 2005, 2:33 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
'One' isn't normally included as a prime. There's another really recent thread regarding primes on the forum; last day or two. You might want to do a search ("prime" should make a nice keyword). My example there uses bitfields for nefarious reasons, but the principles are the same.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Sep 12th, 2005, 6:09 AM   #3
Riddle
Programmer
 
Riddle's Avatar
 
Join Date: May 2005
Location: Nar Shaddaa
Posts: 42
Rep Power: 0 Riddle is on a distinguished road
Send a message via ICQ to Riddle Send a message via AIM to Riddle Send a message via MSN to Riddle
I dunno why you have so much code there..

int isprime(int num) {
    for(int a=2;a<=num;a++) { if(!(num%a)) { return false; } a++; }
    return true;
}

Anyway, 1 isn't counted as a prime number because it's not one. Prime numbers are numbers divisible only by 1 and themselves- think logically for a moment and think about that, and why 1 wouldn't be a prime.
Riddle is offline   Reply With Quote
Old Sep 12th, 2005, 1:15 PM   #4
Konnor
Newbie
 
Join Date: Aug 2005
Posts: 20
Rep Power: 0 Konnor is on a distinguished road
Quote:
I dunno why you have so much code there..
because I'm still learning.

Thanks for that slightly more efficient version riddle. Much Obliged.
Konnor is offline   Reply With Quote
Old Sep 12th, 2005, 4:12 PM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Ask Riddle to go back five years and pluck one of his programs as an example .
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Sep 12th, 2005, 5:53 PM   #6
Riddle
Programmer
 
Riddle's Avatar
 
Join Date: May 2005
Location: Nar Shaddaa
Posts: 42
Rep Power: 0 Riddle is on a distinguished road
Send a message via ICQ to Riddle Send a message via AIM to Riddle Send a message via MSN to Riddle
Quote:
Originally Posted by DaWei
Ask Riddle to go back five years and pluck one of his programs as an example .
psha! Five years ago, I couldn't do much in C++ (I don't think my skills went beyond making a calculator and temperature scale converter). But hey, I had mad skills in QBasic! Bleh.. now, I'm almost ashamed to say I knew QBasic.
Riddle is offline   Reply With Quote
Old Sep 12th, 2005, 6:46 PM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
On numerous occassions I've had to to back and maintain someone's old code. Every time I asked who the stupid SOB was it turned out to be me!
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:19 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC