User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 455,964 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,607 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 602 | Replies: 7 | Solved
Reply
Join Date: Oct 2007
Posts: 32
Reputation: jrice528 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
jrice528 jrice528 is offline Offline
Light Poster

desperate help with arrays

  #1  
Nov 19th, 2007
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;
}

AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,539
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 972
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Most Valuable Poster

Re: desperate help with arrays

  #2  
Nov 19th, 2007
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;
<<Freelance Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Join Date: Oct 2007
Posts: 32
Reputation: jrice528 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
jrice528 jrice528 is offline Offline
Light Poster

Re: desperate help with arrays

  #3  
Nov 19th, 2007
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;
}

Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,539
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 972
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Most Valuable Poster

Re: desperate help with arrays

  #4  
Nov 19th, 2007
>>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.
Last edited by Ancient Dragon : Nov 19th, 2007 at 11:27 pm.
<<Freelance Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Join Date: May 2006
Posts: 2,779
Reputation: WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold 
Rep Power: 15
Solved Threads: 229
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Maven

Re: desperate help with arrays

  #5  
Nov 19th, 2007
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.
Age is unimportant -- except in cheese
Reply With Quote  
Join Date: Oct 2007
Posts: 32
Reputation: jrice528 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
jrice528 jrice528 is offline Offline
Light Poster

Re: desperate help with arrays

  #6  
Nov 20th, 2007
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;
}

Reply With Quote  
Join Date: May 2006
Posts: 2,779
Reputation: WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold 
Rep Power: 15
Solved Threads: 229
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Maven

Re: desperate help with arrays

  #7  
Nov 20th, 2007
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.
Age is unimportant -- except in cheese
Reply With Quote  
Join Date: Oct 2007
Posts: 32
Reputation: jrice528 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
jrice528 jrice528 is offline Offline
Light Poster

Re: desperate help with arrays

  #8  
Nov 20th, 2007
nvm nvm I figured it out!
Last edited by jrice528 : Nov 20th, 2007 at 6:04 pm.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 9:02 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC