I know how to do it on paper, but not in code! HELP PLEASE!

Pages: 12
programming class is basically teach yourself c++. I know how to find a damn prime number on paper, easy, but not in code, I have no idea what to do. There are no examples in the book for questions I'm doing. I'm confused and scared that I will fail.

Where do I look to solve the problems I have to do? I'm so confused, and don't know where to find the answers.

Write a console program that asks the user to enter an integer greater than 1. The
program determines if the number entered is prime and writes a message to standard
output stream stating whether the number is prime or not. For example, if the user
enters 42, the program displays "42 is not prime." If the user enters 47, the program
displays "47 is prime."
Develop 8 test cases and verify that your program runs correctly for them. The test
cases should contain some prime numbers as well as non-prime values. Include test
cases that try to break your program so that you are sure you have implemented the
code correctly.
Include the 8 test cases within a multi-line comment near the top of your source code
file. Make sure the comment is nicely organized and easy to read and understand.



I know I have to have n(the number the users enters) has to be divided by one and itself, but if I were to code that the number would always be divisible by 1 and itself so every number the users enters would be prime, I don't know how to
account for the other numbers. Test cases am I supposed to be using switch, (I don't have any examples., seriously I don't know what to do!)



Write a console program that reads a positive integer entered by the user. The program
computes and displays all the prime numbers between 2 and the number entered.
To solve this problem, you need to nest an inner loop within an outer loop.


Never have done an inner loop within an outer loops so I don't have a clue.


Write a console program that asks the user to enter a positive integer greater than 1.
The program computes the prime factors of the number and prints them. For example, if
the user enters 100, the program displays 2 2 5 5.




If I know how to do number 1 then this would be easier.



Please help, I come to the forums as a last resort. The most frustrating thing is I KNOW how I woul;d find this on paper, but I don't know how to translate it into code.
Ok. I am putting now the codes with explanations and please read it, because further and further it will get harder for you to do other assignments.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

#include <iostream>
using namespace std;
void Primenumber(int);//defining function to determine if number is prime or not
int main()
{

    int number;//Defining number
    cin>>number;//User inputs number
    Primenumber(number);//Calling function for entered number
}

void Primenumber(int number)
{
    int count=0; // Define counter
    for(int i=1; i<number; i++)
    {
        if(number%i==0) // % operator devides a number and sees if it can be divded exactly or not
        {
            count++; // counter counts how many numbers are diveded exactly
        }
    }
    cout<<count<<endl;
    if (count>=2){
    cout<<"Is not prime"; // sees if counter is more than 2 or equal to 2 it means that the number can be diveded by itself and other numbers too. So it means that it s not a prime number
    
    }
    else {cout<<"Prime number";}// if counter is equal to 1 it means that it is prime
    
}



This is the first assignment that you were given
Last edited on
thanks, but I have never used count before, and my professor has not talked about it.

so far I got up to the for loop, but I don't know what to do after that, (without using count)
thanks, but I have never used count before, and my professor has not talked about it


count in the example above is an integer variable. count++ uses the increment operator ++ to add 1 to count.

The example above is flawed. It should be:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void Primenumber(int number)
{
    bool isPrime = true;
    for (int i = 2; i < number; i++)
    {
        if (number % i == 0)
        {
            isPrime = false;
            break;  // Break out of the loop
        }
    }

    cout << number;
    if (isPrime == false)
        cout<<" is not prime";
    else
        cout<<" is prime";
}
Last edited on
Keep in mind that Nata's and Yulingo algorithm is very slow for large primes. You can cut the time in half by iterating from 1 -> sqrt( n ) or even checking if divisible by 2 then going 3-> sqrt( n ) and increment by 2.

[edit]mistype mind earlier.[/edit]
Last edited on
^ I haven't used squares before so I'll stay away, still getting compilation errors when compiling.


Do I have to do anything special when usuing bool is prime?
Do I have to do anything special when usuing bool
In c++ you shouldn't.

What errors are you getting when trying to compile? Are you defining this function inside of the main function? Do you have a main function? Have you read how to declare/define functions?

http://www.cplusplus.com/doc/tutorial/

Also I think a lot of people prefer not to use break statements , multiple returns and goto but it is just a preference.
bool Primenumber(int);
Do one thing and do it well. Your functions shouldn't have to manage I/O

> I KNOW how I woul;d find this on paper,
¿how?


@yulingo: your example is flawed, it consider all the numbers as prime
¿what's the problem with Nata's code?
@yulingo: your example is flawed, it consider all the numbers as prime


He didn't mention why so here it is:
isPrime = true; should be false on line 8.
Last edited on
I corrected that. Sorry.
I don't know how you guys know how to do all this stuff but thanks, now im going to to try to run the program. Do I nee dot include the switch function at the end for cases?
No matter what the number is my code says the number is prime.. I tried changing a few things and no matter what the number is it says that is is not prime FUUUUUUUUUUUUUU.


This is frustrating.
Can you post the code you have?
I'm using ssh how do I copy and paste it here?

closed account (ivDwAqkS)
give us the code you have, so we can tell you what you are doing wrong.
Can i post a picture here? It is prett much the same as the code posted above, I removed all the tweaks, but it only says that the number is not prime regardless of what you enter in.

Since I'm acessing the files using SSh I don't know hwo to copy and paste it here.
You can post the picture. Otherwise I think you'd have to use cat and copy the file to your computer.
http://www.hosting.com/support/linux/move-and-copy-files-within-linux/
Last edited on
http://linux.die.net/man/1/scp

> Can i post a picture here?
don't you dare, code is text, and it would be a lot easier to analyze if presented in that form.

Also, ¿how are you able to take a picture but not to copy the text?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include<iostream>
using namespace std;

int main(){
int i;  
int n;
  bool prime;

  cout << "Please enter a positive integer" << endl;
  cin >> n;

  for(int i = 1; i <= n; i++){
    prime = true;
    for(int k= 2; n <= k - 1; k++){
      if(i % n == 0){
        prime = false;
      }
    }
    if(prime){
      cout << i << " is prime" << endl;
    }
  }

  return 0;
}



This is such a pain in the ass
Last edited on
Honestly I don't know what you are doing o.O

What is the second loop supposed to be doing?



I would remove second loop and change first loop from for( int i = 1; i <= n; i++ ) to for( int i = 2; i < n; ++i ); since you don't know about square roots you said earlier.

then you also are using modulo wrong. It should be n % i I don't want to seem harsh or anything but I think you need to read up a little bit more on loops and operators a little bit more.

http://www.cplusplus.com/doc/tutorial/operators/
http://www.cplusplus.com/doc/tutorial/control/
http://www.cplusplus.com/doc/tutorial/
Pages: 12