Image

Imageeveranddespair wrote in Imagecpp

HALP

Hi, just joined. I am in desperate need of help. I am taking an intro c++ class and am having a hard time on my 5th assignment. This one is waaay over my head. I took Intro the C before with the same teacher, and did well in the class. And I have been doing well in this class, but this assignment is kicking my ass. Anyway, this IS a homework assignment, so I am not fishing for people to do my homework for me(how else could I learn?) I just need a push. I am not really sure how to start.

Write a C++ program to calculate and display the number of primes in the first 50 “chiliads”. Your results should be as presented below, under testing.

Start End Number of Primes
1 1000 168
1001 2000 135
2001 3000 127
3001 4000 120
4001 5000 119
5001 6000 114
6001 7000 117
7001 8000 107
8001 9000 110
9001 10000 112



49001 50000 98

Total primes in the first 50 chiliads: 5133
Average number per chiliad: 102.66

She gave a bunch of background on chiliads, but that is unimportant. Basically a chaliad is an array of 1000 elements. So I have to count the number of primes in each chiliad and to total number of primes in all 50 chiliads. AND the average number of primes in each chiliad. BAH.

So I have to use nested loops and functions(that is what this assignment is supposed to be practicing.) so I have two function prototypes(which my teacher gave):

bool isPrime (long n);
// Returns true if n is a prime, else false

long primeCount (long x, long y);
// Returns the number of primes between x and y, inclusive.

I understand the concept of these functions, but I am not sure how to execute them.
Can someone please help? An algorithm? hints? anything to push me in the right direction?

EDIT: Here's what I have as of now:


#include <iostream> using namespace std; bool isPrime(long n); //returns true if n is prime, else false. long primeCount(long x, long y); //returns the number of primes between x and y. int main() { int i, j; for(i = 0; i <= 50; i++) for(j = 1; j <= 1000; j++) isPrime(long); if(isPrime == true) primeCount(); return 0; } bool isPrime(long n) { for(int a = 1; a <= 50000; a++) if((n % a) == 0) break; if(a == n) isPrime = true; else isPrime = false; } long primeCount(long x, long y)


I am now having trouble with primeCount. I'm stuck on how to count the primes total, and the primes in each chiliad.