•
•
•
•
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
![]() |
•
•
Join Date: Oct 2007
Posts: 32
Reputation:
Rep Power: 2
Solved Threads: 0
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!!!
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;
}
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,539
Reputation:
Rep Power: 40
Solved Threads: 972
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.
>>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 = 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;•
•
Join Date: Oct 2007
Posts: 32
Reputation:
Rep Power: 2
Solved Threads: 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
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;
}
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,539
Reputation:
Rep Power: 40
Solved Threads: 972
>>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.
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.
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
•
•
Join Date: Oct 2007
Posts: 32
Reputation:
Rep Power: 2
Solved Threads: 0
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.
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;
}
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- (reformatted) How to return Multi-Dimensional Arrays (C++)
- Please Read....Im desperate... (Troubleshooting Dead Machines)
- Arrays (C++)
- How to Return Multidimensional Arrays (C++)
- C file input/output 2D arrays. (C)
- passing arrays in visual basic (Visual Basic 4 / 5 / 6)
Other Threads in the C++ Forum
- Previous Thread: array problem
- Next Thread: Writing stuff inside files in C++ bloodshed



Linear Mode