Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,101 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,673 people online right now. Registration is fast and FREE... Join Now!




prime numbers in vector except 2

 
Reply to this topicStart new topic

prime numbers in vector except 2, I am trying to store prime numbers in a vector but the code gives me a

nirvan25
2 Oct, 2007 - 04:42 PM
Post #1

New D.I.C Head
*

Joined: 23 Oct, 2005
Posts: 12


My Contributions
using std::cout; using std::endl;
using std::string; using std::vector;
using std::cin; using namespace std;


int main()
{
int num;
cout << " input " << endl;
cin >> num;

vector<int> odd;
vector<int> prime;


int i = 3;
while (i <= num )
{
odd.push_back(i);
i += 2;
}
for ( int z = 0; z != odd.size(); ++z ) {
int count = 0;
for ( int x = 1; x != z + 1; ++x ) {
if ( z % x == 0 ) {
count = count + 1;
}

}
if ( count == 2 ) {
prime.push_back(odd[z]);
}
}

for ( int y = 0; y != prime.size(); ++y ){
cout << prime[y] << endl;
}
system("PAUSE");
}


User is offlineProfile CardPM
+Quote Post

nirvanarupali
RE: Prime Numbers In Vector Except 2
2 Oct, 2007 - 04:46 PM
Post #2

D.I.C Foot
Group Icon

Joined: 1 Aug, 2007
Posts: 983



Thanked: 2 times
Dream Kudos: 375
My Contributions
Hello nirvan25, please use code tag for your codes. # button in the upper right.

What is your questions? post your codes and your questions or problems.
User is offlineProfile CardPM
+Quote Post

nirvan25
RE: Prime Numbers In Vector Except 2
2 Oct, 2007 - 09:56 PM
Post #3

New D.I.C Head
*

Joined: 23 Oct, 2005
Posts: 12


My Contributions
I have updated my code to this
#include <iostream>
#include <vector>

using std::cout; using std::endl;
using std::string; using std::vector;
using std::cin; using namespace std;


int main()
{
int num;
cout << " input " << endl;
cin >> num;

std::vector<int> odd;
std::vector<int> prime;




int i = 1;
while (i <= num )
{
odd.push_back(i);
i += 2;
}
//for ( int z = 0; z != odd.size(); ++z ) {
// int count = 0;
// for ( int x = 1; x != odd[z] + 1; ++x ) {
// if ( odd[z] % x == 0 ) {
// count = count + 1;
// }
//
// }
// if ( count == 2 ) {
// prime.push_back(odd[z]);
// }
// }
for ( int n = 2; n <= odd.size(); n + 2 ) {
odd.erase(odd.begin(n), odd.end(n));
}

for ( int y = 0; y != odd.size(); ++y ){
cout << odd[y] << endl;
}
cout << odd.size() << endl;
system("PAUSE");
}

I have a vector with odd numbers called odd. What I want to do now is remove every third element.I heard of the erase() function but I dont understand the syntax.Can anyone help me please?
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Prime Numbers In Vector Except 2
2 Oct, 2007 - 10:28 PM
Post #4

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
i have a tutorial on the various vector methods that you should probably look at. however, the short version is, if you want to erase a single element at position i from a vector v, you call the method

v.erase(v.begin() + i);

erase() requires iterators (similar to pointers) to access elements. begin() returns an iterator to the first element of the vector, so begin() + i increments that iterator to the position i elements away from begin().

begin() and end() do not accept parameters, as you have written in your code.

and please follow nirvanarupali's recommendation to use code tags - it's the rules. you should edit your previous posts to add them as well.

hope that helps.

-jjh

This post has been edited by jjhaag: 2 Oct, 2007 - 10:30 PM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 08:56PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month