Topic List |
Page List:
1 |
---|---|
OhhhJa 10/14/18 1:10:29 AM #1: |
#include <iostream>
using namespace std; int Prime(int); void primes (int n); int main() { int n; cout << "Enter a positive integer: "; cin >> n; while (n >= 0) { if(Prime(n) == 0) cout << n << " is a prime number."; else cout << n << " is not a prime number."; return 0; } } int Prime(int n) { bool flag = false; for(int i = 2; i <= n/2; ++i) { if(n%i == 0) { flag = true; break; } } return flag; } How would I adjust this program to make it print out all prime numbers up to n = 12? I'll give you the best blowie of your life if you help ... Copied to Clipboard!
|
TheWorstPoster 10/14/18 1:13:59 AM #2: |
/* C++ Program - Print Prime Numbers */ ... Copied to Clipboard!
|
TheWorstPoster 10/14/18 1:15:37 AM #3: |
I just stole that code online somewhere.
I never coded in C++ (I do mainly Java and C#) ... Copied to Clipboard!
|
OhhhJa 10/14/18 1:22:45 AM #4: |
well, you managed to find what I couldn't. Unfortunately, it still isn't working. It probably just needs minor adjustments. It gives me an error saying that there is no such directory with iostream.h
... Copied to Clipboard!
|
Yellow 10/14/18 1:27:15 AM #5: |
first off, you're using "while" incorrectly in main. You should be using "if" instead
if (n >= 0) 'While' is meant to loop code and will repeat as long as the condition is met. 'if' will only go through once, which is what you're going for here I imagine. If you want to loop through every number up until 12, you would have to use a 'for' loop in your main function. int main() { int n; cout << "Calculate every number up until: "; cin >> n; for (int i = 0; i < n; i++) { if (Prime(i) == 0) cout << i << " is a prime number.\n"; else cout << i << " is not a prime number.\n"; } return 0; } Also, a little tip, to do line ends you can type \n in the middle of the string. That makes the output much easier to read. You can also use endl http://www.cplusplus.com/forum/beginner/2138/ ... Copied to Clipboard!
|
Yellow 10/14/18 1:30:11 AM #6: |
OhhhJa posted...
well, you managed to find what I couldn't. Unfortunately, it still isn't working. It probably just needs minor adjustments. It gives me an error saying that there is no such directory with iostream.h The only adjustment I needed to make to your code to get it to compile was adding #include "pch.h" to the top. ... Copied to Clipboard!
|
Yellow 10/14/18 1:42:41 AM #7: |
Also, don't copy code from the internet for assignments, your teacher has automated tools that will catch you doing that.
You should be fine using my code, I only changed yours a little. ... Copied to Clipboard!
|
OhhhJa 10/14/18 1:48:46 AM #8: |
I appreciate the help but im still getting an error message that says unqualified id before "if"
#include<iostream> using namespace std; int Prime(int); void primes (int n); if (n >= 0) { if (Prime(n) == 0) cout << n << " is a prime number."; else cout << n << " is not a prime number."; } return 0; int main() { int n; cout << "Calculate every number up until: "; cin >> n; for (int i = 0; i < n; i++) { if (Prime(i) == 0) cout << i << " is a prime number.\n"; else cout << i << " is not a prime number.\n"; } return 0; } also I should add that he wants us to use that void primes command for this assignment ... Copied to Clipboard!
|
Yellow 10/14/18 2:03:39 AM #9: |
OhhhJa posted...
also I should add that he wants us to use that void primes command for this assignment Can you be more specific about what he wants you to do and how you're supposed to do it? Like, what's the assignment exactly ... Copied to Clipboard!
|
OhhhJa 10/14/18 2:09:31 AM #10: |
the directions: Write another program which its main() works like the previous question but for the prime number function, have the method print out all primes up to the value n. The function prototype is: void primes(int n).
Ex. if n=12 program would print 2,3,5,7,11. If n=19, program would print 2,3,5,7,11,13,17,19. The code in the OP is what I used for the original question which was to basically just set up a program where the user could enter in any number and determine if it was prime. And he told us to use a while loop which is why I did. Not sure if I did everything right for the first question but everything compiled fine when I ran it ... Copied to Clipboard!
|
Yellow 10/14/18 2:18:04 AM #11: |
OhhhJa posted...
And he told us to use a while loop which is why I did. That's goofy. Get rid of that if statement, I was only using that as an example. You can't have code outside of a function/method. Your int Prime() function was good, keep that. A void method is basically a method without a return type (I'm sure you knew that, though). He probably wants you to use it to organize your code. Go ahead and make the definition for void primes (int n), put all the code involving looping through the numbers into it. The only code in your main function should be the code you use to get input from the user, and then actually calling your void primes(int n); method. Also, it helps to name all of your variables and objects with names that will remind you what they do/are. Maybe your void primes(int n) function should be named void PrintPrimes(int n), or better yet PrintPrimes(int max_number) ... Copied to Clipboard!
|
Sahuagin 10/14/18 12:20:09 PM #12: |
rather than return 0 in the while loop, which will exit the program since it's in the main function, instead decrement n and then you will count down to zero.
main function then looks something like:
you might have to change it to have a separate counter that counts up to n if you really need it to count up to the number instead of down to zero. if you haven't implemented primes and you're supposed to then you're missing a part of the assignment. --- ... Copied to Clipboard!
|
Topic List |
Page List:
1 |