### Prime Number Function?

Can someone help me Im stuck and ive waited until the last minute.

Write and test a C++ program to perform the following sequence of events:

Create an array of 50 integer values.
Initialize the first element of the array to a value of 2 (the first prime number).
Use a function to examine a single number, determining whether it is prime or not by dividing it by all numbers from 2 to half the candidate value, looking for any divisions that have no remainder.
Generate the first 49 prime numbers after the number 2, and store them in consecutive values of the array.
Use a loop to display the contents of the array.
10pt BONUS - Use the previously-found primes as divisors to find the next prime.
10pt BONUS - Save the list of prime numbers to a file named primes.txt.
Format the file with one prime number per line.
Last edited on
What do you have so far? What exactly are you stuck on? You'll have to give us more info as this is not the type of forum where homework gets done for you - you'll need to show us you're putting in a majority of the effort. :)
 ``1234567891011`` ``````int main() { int num[50]; //initalize the array for (int i=2; i<50; i++) // initalize the first element to 2 { num[1] = 2; } system("pause"); return 0; }``````

this is what I have so far.
Well your for loop's description is incorrect.

Do you know how to write functions or find primes?
Arent primes only divisible by 1 and itself?
That's all you can come up with on your own? If your current understanding of for loops is this weak, you need to go read about them again. Currently your i variable is doing absolutely nothing, and all your for loop is doing is setting the 2nd memory location in your "num" array to the value of 2. And that's it. That's all your code does at the moment. You're going to have to put in more than 2 minutes man.
I do not know if this correct for you. It works when I run the program. Check it for yourself,give me feedback and I will go and fix it for you mate. By the way your for loop look alright to me mate.
int main()
{
int num[50];
bool prime = true;

for(int i = 2; i < 50; i++)
{
if(i % num[i])
{
prime = false;
}
cout<<i<<endl;
}
system("pause");
return 0;
}
Last edited on
His for loop looks alright? His for loop does nothing but set the 2nd value of num[] to 2... How is that correct? While it's nice that the person somewhat understands how to write a very simple for loop (even though they don't seem to understand what it's doing...), they could have just scrapped the whole thing and typed num[1]=2 instead. Even if he would have put i instead of 1, his for loop would only be setting every value in his array (except for the first two) to the number 2. Please explain how this is anywhere near correct. It's completely missing the actual logic (or even an attempt at the logic) that this homework assignment is designed to teach.

Please do not do people's homework for them if they're not willing to put forth more than a few minutes of effort. This forum exists to help people learn, and doing things for them without making them think at least a little bit on their own is not going to accomplish that goal.
Guys I solved it sorry I was in a bind but my teacher helped me thank you for all of your help sorry for the trouble.
No worries mate, glad ya got it.
The majority of prime number listing programs use the % operator for testing, I thought I would write one without using %. Here it is.

#include <iostream>
#include <cstdlib>
using namespace std;

int main(void)
{
int n,m,i,k,j,p;
bool TEST; // becomes false if a number is equal to multiplication of two other numbers
TEST = true; // initialize TEST

cout << "This is a prime number listing program" << "\n";
cout << "Up to which number do you want to list primes? ";
cin >> n;

for (i=2; i<n+1; i++) // tries all numbers up to n

{
for (j=2; j<i; j++) // multiply all numbers below the number being tried (i) to see if it gives i

{
for (k=2; k<i; k++)

{
p=j*k;

if (p==i) // if a certain product equals i, TEST changes flag & loop is aborted

{
TEST = false;
break;
}

}

}

if (TEST == true) // if TEST does not change after loop is finished, it means i is prime
cout << i << " ";
else TEST = true; // else reset TEST back to "true" and try the next number

}

}
Topic archived. No new replies allowed.