I am working on homework, and been doing this assignment with absolutely no luck, i am in need of major help.

This is the assignment. Using an array, initialize all elements in the array to 1. Starting with array subscript 2, every time an array element is found whose value is 1, loop through the remainder of the array and set to zero every element whose subscript is a multiple of the subscript for the element with value 1. all elements beyond 2 in the array that are multiples of 2 will be set to zero (subscripts 4, 6, 8, 10, etc.);


this is my code, i have initialized all elements in the aray to 1. I need MAJOR help with a formula to determine if its prime, and set everything else to a 0. I have been working on this one part for 3 days and i finally gave in and need help... Any help is really appiciated!!!

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

void prime_num(int);
const unsigned W = 10;

int main()
{
    const unsigned arraySize = 5000;
    int a[arraySize];
    unsigned b;
    unsigned c;   
    unsigned d;
    
cout<<"This program is programming assignemnt #5."<<endl;
cout<<"THE SIEVE OF ERATOSTHENES."<<endl;
cout<<"By Jeremy Rice of CSCI 111."<<endl;

for (int i = 2; i < arraySize; i++)
{
    a[i] = 1;   
}

for (int j =2; j < b; j++)
              



system("pause");
return 0;
}

Your description of the problem doesn't make sense. First you say to initialize all elements of the array to 1. That's simple enough to do.

for (int i = 0; i < arraySize; i++)
    a[i] = 1;

>>Starting with array subscript 2, every time an array element is found whose value is 1
Well since we set all elements to 1 as above then the 2nd subscript will be 1. So your program should set elements 2, 4, 6, 8, 10, ... 5000 to the value of 0 and leave all others alone.

for (int i = 2; i < arraySize; i += 2)
    a[i] = 0;

Are you talking something along the lines like this, to eliminate all the numbesr divisible by two..


if so, my question is how would i print the subscript of the array if it is set to 1? again ty for your help

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

void prime_num(int);
const unsigned W = 10;

int main()
{
    const unsigned arraySize = 5;
    int a[arraySize];
    unsigned b;
    unsigned c;   
    unsigned d;
    
cout<<"This program is programming assignemnt #5."<<endl;
cout<<"THE SIEVE OF ERATOSTHENES."<<endl;
cout<<"By Jeremy Rice of CSCI 111."<<endl;


       for (int i = 0; i < arraySize; i++)
{
    a[i] = 1;
    for (int i = 2; i < arraySize; i += 2)
{
    a[i] = 0;
  
}
}


cin>>b;
for (int j = 0; j < b; j++)
cout<<a[j];

        
system("pause");
return 0;
}

>>Are you talking something along the lines like this, to eliminate all the numbesr divisible by two
I don't know -- all I know is what you posted.

>>how would i print the subscript of the array if it is set to 1?
Simple -- create another loop that looks at all the elements of the array, not just every other one, check if its value is 1, and if it is then print the value of the loop counter.

If you learn now to format your code you will discover it's easier to find errors and your code will be easier to read. This is very worthwhile when you get to more complex programs. It also helps us understand your code.

Ok, tried to format a little better, added some comments, but its not alot hehe.. anyways, my prog has a ways to go, but the last part I know how to do, its just taking out the prime numbers, and displaying them. I figured the counter out I think. I just need help with the formula to keep checkin like... take out multiples of 3, 4, 5 and so one, up until the user specified number.

Thanks for the help you have already provided, i appiciate it alot. Really new to programming, and never uesd arrays before.

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


const unsigned W = 10;

int main()
{
    const unsigned arraySize = 5;
    int a[arraySize];
    unsigned b;
    unsigned c;   
    unsigned d;
    unsigned counter = 0;


cout<<"This program is programming assignemnt #5."<<endl;
cout<<"THE SIEVE OF ERATOSTHENES."<<endl;
cout<<"By Jeremy Rice of CSCI 111."<<endl;

//Initializing all elements to 1
          
for (int i = 0; i < arraySize; i++)
{
         a[i] = 1;
// Initializing all elements divisable by 2, to 0
for (int j = 0; j < arraySize; j = j+2)
    {
         a[j+1] = 0;
    }

}

cin>>b;
for (int j = 2; j < b; j++)
{
    if (a[j] == 1)
    {
       counter++;
       cout<<counter<<endl;
    } 
    else
        counter++; 
}

        
system("pause");
return 0;
}

Your formatting is still hiding your errors. Pay closer attention to the section on Indentation and you will see a glaring error when you indent properly.

nvm nvm I figured it out!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.