Here is the prompt for my assignment

Your third assignment is to write a C++ program that will print out the primes numbers between 2 and 100. However, you must use two functions in your program.

The first function, factor, takes an int, n, and returns two factors of n whose product is n. For example, if n is 36, factor could return 6 and 6 (or 2 and 18, or 4 and 9, ...). If n is prime, factor should return 1 and n (in that order). If n is not prime, factor should not return 1 and n, but find two non-trivial factors. The factor function should have a return type of void and use call-by-reference parameters to return the two factors.

The second function, prime, should return a Boolean value - true if the argument is prime, and false if it is not prime. Since a good programmer is lazy, the prime function should call the factor function instead of doing any work itself.

Finally, the main program should loop through all the numbers between 2 and 100, and print out just those that are prime. The main program should call the prime function appropriately, which in turn calls the factor function.


_____________________________________________
I think I'll be able to figure out how to find the prime numbers, but I have no clue how to find factors of a number

To find two factors you could do modulo with a variable that is incremented until the rest is 0.

With what? You haven't shown us anything we can help you with. Based on the info you've given, all we can do is write it for you.

With what? You haven't shown us anything we can help you with. Based on the info you've given, all we can do is write it for you.

I can't think of a function that finds the factors of a number

You have followed some thinking process to arrive at the answer, right?

Now convert this into pseudo code and finally code.

If you post atleast the pseudo code here, you will get some help.

12&1
6&2
4&3

why?

Because now you can code the program. As thomas implied,
1) What are the steps you used to figure that out?
2) Write those steps in detail
3) There is the beginning of your code.

This is the code for a program that finds prime numbers 2-100.
No clue how to find factors

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int main(void) {

  int c = 0;
  cout << "Prime numbers between 2 and 100" << endl;
  for(int i=2; i <= 100; i++) {
    bool isPrime = true;
  
    // Check to see if i is prime, print i out if it is.
    for(int j=2; j <= sqrt((double) i); j++) {
      if(i % j == 0)
	isPrime = false;
    }
    if(isPrime) {
      c++;
      cout << setw(2) << i << " ";
      if(c % 10 == 0)
	cout << endl;
    }
  }
  cout << endl;
  return 0;
}

Ok, it looks like someone needs some major handholding. Either that or you can't be bothered to think and want us to do it for you. I'm hoping that's not the case.

From 4th grade math (I think):
How do you know 2 is a factor of 12? What is it that makes it a factor? How did you arrive at that answer?

Please explain in detail. And "because 6*2 is 12" is not an answer.

Same questions with 3, then 5.


And considering you can't do something as easy a finding the factors, where did you steal the prime number code from. There is no way you wrote it.

Edited 6 Years Ago by WaltP: n/a

Have you considered using modulo when finding the factors?
Think of it this way: If x is evenly divided by y, then x has two factors; y and the result of x/y.

Ok, it looks like someone needs some major handholding. Either that or you can't be bothered to think and want us to do it for you. I'm hoping that's not the case.

From 4th grade math (I think):
How do you know 2 is a factor of 12? What is it that makes it a factor? How did you arrive at that answer?

Please explain in detail. And "because 6*2 is 12" is not an answer.

Same questions with 3, then 5.


And considering you can't do something as easy a finding the factors, where did you steal the prime number code from. There is no way you wrote it.

Firstly, the code for the prime number function was available on our class website. It was an exercise we did in class.

And I'm posting this thread because I AM lost and need some "hand holding". This is my first computer language and I've been taking this class for about 2 months now. I'm not too good at breaking down a problem into code.

Now, about factors.

I know that 12 has certain factors because it is divisible by these numbers evenly. When a number has factors of itself and 1, it is prime.

I just don't know how to translate this into code. @strmstn, are you talking about the % command?

for(int i=2; i <= 100; i++) 
{    
  bool isPrime = true;     
  // Check to see if i is prime, print i out if it is.    
  for(int j=2; j <= sqrt((double) i); j++) 
 {      
    if(i % j == 0)
      //what does the above line mean?  
      //it means i isn't prime, true, which is what your previous project was about, but what else does it mean? 
      //write out the meaning using pen and paper  
      //then figure out what the meaning has to say about the relationship of j to i?
This article has been dead for over six months. Start a new discussion instead.