Hey guys. I read the forum rules so I'm just going to be honest, this is a homework question. I'm not looking for a completed program or anything. I'm well ahead of my deadline but I'm not really sure where to go from here or how to structure this. I've been digging through the forum for a couple days now and it helped me figure out how to find the primes but I'm not really sure how to get them into the chart efficiently. The final output is supposed to look like the following. (Broken up by 1,000's and 100's with averages and totals.) The code I have is below.
g1 g2 g3 g4 g5 g6 g7 g8 g9 g10 Average Primes
----------------------------------------------------------------
25 21 16 16 17 14 16 14 15 14 16.8 for 1 to 1000
16 12 15 11 17 12 15 12 12 13 13.5 for 1001 to 2000
…
8 11 8 8 7 9 8 10 10 8 8.7 for 99001 to 100000
Total number of primes from 1 to 100000: 9592
Average primes per 1000: 95.92
Percentage of primes from 1 to 100000: 9.59
//Libraries
#include<iostream>
#include<iomanip>
usingnamespace std;
//Prototypes
bool isPrime (long);
int main()
{
int x = 0, y = 0;
int temp = 0;
for (int n = 2; n <= 100000; n++)
{
x = isPrime(n);
temp = x;
if (temp > 0)
y = y + 1;
}
cout << y << endl;
system("PAUSE");
return 0;
}
bool isPrime (long n)
{
for(int i = 2; i < n ;)
{
if (n > 2)
{
if (n % i == 0)
returnfalse;
else
i++;
}
}
}
Thanks in advance to anyone who takes the time to help. Appreciate it.
I'm not sure that I understand the chart. These two lines make sense:
Total number of primes from 1 to 100000: 9592
Average primes per 1000: 95.92
but I don't get the next line: Percentage of primes from 1 to 100000: 9.59
and the rest of the chart, what do the headings mean? g1 g2 g3 g4 g5 g6 g7 g8 g9 g10 Average Primes
and what are these numbers? 25 21 16 16 17 14 16 14 15 14 16.8 for 1 to 1000
As for the problem as a whole, I'd say you could break it down into two separate concepts, first some code which generates a list of primes. Just think of that as a list of numbers.
Secondly, a part of the program which takes a list of numbers and analyses them. I guess you could think of this as being much the same as if you had a file containing a list of numbers, and you need to analyse them. But I don't understand this part - maybe I'm missing something?
#include<iostream>
#include<iomanip>
//using namespace std; // Generally considered bad practice, google it for more info
//Prototypes
bool isPrime (constlong);
int main()
{
int x = 0, totalPrimes = 0; //Try to give variables reasonable names
//int temp = 0; // purpose of temp? dosen't seem to do anything
for (int n = 2; n <= 100000; ++n)//Prefer ++n over n++ unless you want to retrieve the original value
{
x = isPrime(n);
//temp = x;
if (x > 0)
totalPrimes = totalPrimes + 1;
}
std::cout << totalPrimes << endl;
// Use this instead of system("PAUSE");
// visit the begginers forum and go to "Console Closing Down" for a discussion on why
std::cout << "Press ENTER to continue...";
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
return 0;
}
bool isPrime (constlong n)// changed n to const as it doesn't change in function
{
if (n > 3)// moved check outside of loop, the expression will stay the same as n doesn't change, also 3 is prime so you don't need to check against that
{
int halfN = (n / 2) + 1; // Only need to check i up to half of n + 1, think about it for a moment and ask why if you can't figure it out
for(int i = 2; i < halfN ; ++i)
{
if (n % i == 0)
returnfalse;
}
}
// will only reach here if n <= 2 OR if n is prime
// that being the case we return true
returntrue;
}
As for your question sorry I'm not sure what all those numbers are meant to be.
Edit: Note I didn't actually try to compile this so sorry for any mistakes. (like the one I secretly just fixed)
std::cout << "Press ENTER to continue...";
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
Actually it is not a good solution either, because it depends on std::cin state. Try to do this after std::cin >> /*some variable*/ for example.
You should be careful using this one.
Serious problem with your isPrime function. It must return a value. Ensure that it does. And perhaps pay attention to compiler warnings, because it is very likely to be warning you about this.
Chervil yeah sorry about that. The 'g' columns were primes per 100. And then an average at the end.
LBEaston I think you saying I didn't need the "if" statement inside the loop made something in my dumb brain click and it all made sense after that. You'd figure that's a pretty obvious thing I don't know why I didn't see it. Either way that helped a lot. I really appreciate it. And yeah, my teacher has mentioned that namespace standard is bad form, I'm just not comfortable with the std; syntax yet. Next program I'll start though.
Cire yes. That makes a lot of sense. I will most certainly remember that from now on.
Anyway, here's the finished program, if anyone has any critiques I'd be open to hear them. Otherwise thanks again folks!