Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,223 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,025 people online right now. Registration is fast and FREE... Join Now!




Prime numbers... again.

2 Pages V  1 2 >  
Reply to this topicStart new topic

Prime numbers... again., Yea. I suck.

Adot2the
post 25 Mar, 2008 - 11:45 AM
Post #1


New D.I.C Head

*
Joined: 7 Feb, 2008
Posts: 37

I have tried searching for previous posts about finding prime numbers, but I can't really seem to find any that can help me figure out how to impliment them into my code. So I thought I would post what I need help with, and what I have so far. Here it goes. This is only one step to my project, I hope to figure this out so I can do the other half on my own.

Write a program so that the user will enter two integers say x and y. As long as x>y, the program will print all primes between x and y inclusive. Also, write a function named findPrime which accepts two integer parameters say a and b. This function will print all prime numbers from a to b by calling the function isPrime. Have your program call this function busing the user input x and y.

My code:

CODE
// Project 4 IsPrime.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <stdlib.h>



using namespace std;



int main()
{    int x;
    int y;

    printf ( "Please enter two positive integers-->");
        scanf ( "%d" "%d" , &x, &y );

        printf ( "\nPRIME: ");
        

        
        

}

int findPrime ( int a, int b)



If 3 and 30 are entered for x and y, the program should produce the following output:

Prime: 3 5 7 11 13 17 19 23 29
(Prime count =9)


I'm not really sure where to start, not sure how to call isPrime to check to see if its prime or not, pretty sure I know how to write a code to figure out the prime number for 1 number, but not to list in between, just really confused, I know I need a for loop somewhere, and of course a while loop, I could really use help on how the program should look, (an algorithm would be nice). Well, any tips, pointers, or anything else would be greatly appreciated.

Adot2the~

User is offlineProfile CardPM

Go to the top of the page

steelersfan
post 25 Mar, 2008 - 12:13 PM
Post #2


New D.I.C Head

*
Joined: 5 Mar, 2008
Posts: 24

try out my previous posts and responces to them. i have a very similar program that calculated up to the first 100,000 primes and then printed out a section of primes such as your x-y..let me know if it helps..if not i can try to give you some clips of my program to help you

<http://www.dreamincode.net/forums/index.php?showtopic=45246&hl=prime>
User is offlineProfile CardPM

Go to the top of the page

Adot2the
post 25 Mar, 2008 - 12:23 PM
Post #3


New D.I.C Head

*
Joined: 7 Feb, 2008
Posts: 37

still lost, not sure how to write my functions, or how my program should look sad.gif

thanks anyway.
User is offlineProfile CardPM

Go to the top of the page

steelersfan
post 25 Mar, 2008 - 01:25 PM
Post #4


New D.I.C Head

*
Joined: 5 Mar, 2008
Posts: 24

haha well then what functions do you need to have and such?

algorithm for your program would be kind of like:
ask user for x and y inputs
calculate prime numbers between x and y using a for loop
print prime numbers calculated

i know it seems vague but that could maybe help..is there a specific number the user is suppose to not go over or anything??
User is offlineProfile CardPM

Go to the top of the page

Adot2the
post 25 Mar, 2008 - 04:16 PM
Post #5


New D.I.C Head

*
Joined: 7 Feb, 2008
Posts: 37

QUOTE(steelersfan @ 25 Mar, 2008 - 02:25 PM) *

haha well then what functions do you need to have and such?

algorithm for your program would be kind of like:
ask user for x and y inputs
calculate prime numbers between x and y using a for loop
print prime numbers calculated

i know it seems vague but that could maybe help..is there a specific number the user is suppose to not go over or anything??


well if the user enters 3 and 30, it should calculate all of the prime numbers between the two, and not go over 30 if thats what you mean. But no, it should be able to calculate the primes between any range of numbers, the only problem that I have is writing functions to do so, I don't know what an IsPrime function should look like, and a FindPrime function should look like. I don't know how to call the functions to print the numbers out.

User is offlineProfile CardPM

Go to the top of the page

Adot2the
post 26 Mar, 2008 - 02:15 AM
Post #6


New D.I.C Head

*
Joined: 7 Feb, 2008
Posts: 37

can anyone show me how it would look?
User is offlineProfile CardPM

Go to the top of the page

Sepanto
post 26 Mar, 2008 - 02:58 AM
Post #7


D.I.C Head

Group Icon
Joined: 20 Mar, 2008
Posts: 97



Dream Kudos: 50
My Contributions


Let's say we have a function int prime(int a, int cool.gif
cpp

int prime(int x,int y)
{
int i,j;
bool x=true;
if (y>x){
for (i=a;i<b;i++)
{ for (j=2;j<i;j++)
if (i%j=0)
x=false;
if (x==true)
return x;
}
return -1;
}
else
return -1;
}
//this means if no prime is found return -1.
return -1;
}

now my idea for the main was something like this:
cpp

int main()
{
int x,y,i,prime;
scanf("%d%d",&x,&y);
prime=x;
do {
prime=prime(prime,y);
printf("%d",prime);
}while(prime!=-1);
return 0;
}

I hope i didn't complicate it to something unworking, but the algorithem seems right.
User is offlineProfile CardPM

Go to the top of the page

Adot2the
post 27 Mar, 2008 - 12:57 AM
Post #8


New D.I.C Head

*
Joined: 7 Feb, 2008
Posts: 37

hmm not sure why im getting errors.

CODE
// Project 4 IsPrime.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <stdlib.h>
#include <conio.h>


using namespace std;

int isprime( int a, int b)
{    int i;
    int p;

    for (i=2;a<b;i++)

        if (a%i!=0) {
            p++;
            return 0;
        else
            return i
                        }
}

int main()
{    int x;
     int y;

    printf ( "Please enter two positive integers-->");
        scanf ( "%d" "%d" , &x, &y );

        printf ( "\nPRIME: ");

        isprime(i);

   return 0;
}


Just need the program to call isprime to keep printing the value of i. The variable i should be the prime numbers between x and y.

confused lol.
User is offlineProfile CardPM

Go to the top of the page

Adot2the
post 28 Mar, 2008 - 12:09 AM
Post #9


New D.I.C Head

*
Joined: 7 Feb, 2008
Posts: 37

bump, could use help.. lol.
User is offlineProfile CardPM

Go to the top of the page

steelersfan
post 28 Mar, 2008 - 03:36 AM
Post #10


New D.I.C Head

*
Joined: 5 Mar, 2008
Posts: 24

what is the error message you are getting back??
User is offlineProfile CardPM

Go to the top of the page

steelersfan
post 28 Mar, 2008 - 03:41 AM
Post #11


New D.I.C Head

*
Joined: 5 Mar, 2008
Posts: 24

should you maybe also call the isprime function before you call your print function? maybe im wrong though

and also your calculations aren't going to be correct you need another nested for loop...like Sepanto showed you above in his first bit of code he posted
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 28 Mar, 2008 - 06:24 AM
Post #12


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,170



Thanked 33 times

Dream Kudos: 25
My Contributions


You are supplying a variable named i to the function isprime(). you have not declared a variable named i, nor have you instantiated it.
User is online!Profile CardPM

Go to the top of the page

2 Pages V  1 2 >
Reply to this topicStart new topic
Time is now: 11/21/08 08:57PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month