473,405 Members | 2,349 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Can "cout" do evil?

#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<iterator>
#include<string>
#include<algorithm>

using namespace std;

#define M 1000009

vector<intprimes;

//generate C(n, 1), C(n, 2), ...
void dfs(vector<int&mp, int its, int i, vector<int&temp){
if(i==0) {
int s=1;
for(vector<int>::iterator it2(temp.begin()); it2!=temp.end();
it2++){ s=s*(*it2); }
if(s>1) mp.push_back(s); return;
}
if(mp[its]==0) return;
int my_its(its);
for(vector<int>::iterator it_t(mp.begin() + its); *it_t!=0; it_t+
+, my_its++){
temp.push_back(*it_t);
dfs(mp, my_its+1, i-1, temp);
temp.pop_back();
}
}

int main(){
vector<intnumbers; numbers.resize(M, 0);

//generate prime numbers
primes.push_back(2);
for(int i=3; i<M; i+=2){
if(numbers[i]==1) continue;
primes.push_back(i);
for(int j=i+i; j<M; j+=i)
numbers[j] = 1;
}

int m, k;
while(cin>>m>>k){
vector<intmprime; int mm=m;
cout<<1; //<1>

//prime factors of m
for(vector<int>::iterator it(primes.begin()); it!=primes.end()
&&*it<=mm; it++){
if(mm%(*it)==0) mprime.push_back(*it);
while(mm%(*it)==0) mm=mm/(*it);
}

//generate C(n,i), push the multiplication result of the "i" factors
in mprime, seperated by 0
int msize = mprime.size();
mprime.push_back(0);
for(int i=2; i<=msize; i++){
vector<inttemp;
dfs(mprime, 0, i, temp);
mprime.push_back(0);
}

//calculate the k-th number x with gcd(x,m)=1
int result = 0; int total=0; bool change=true;
while(total<k){
result += (k-total); total=result;
for(vector<int>::iterator it(mprime.begin()); it!
=mprime.end()&&*it<=m&&*it<=result; it++){
if(*it==0){ change=(change?false:true); continue;}
if(change) total-=(result/(*it));
else total+=(result/(*it));
}
}
cout<<result<<endl;
}
}

---------code--------
The above is the code to calculate the k-th number x of m having
gcd(x,m)=1
notice the line marked <1>
If the <1line exists, everything works fine.
But if I remove line <1>, the guy says error.
what's wrong? Can a simple "cout" do anything evil?
Jun 27 '08 #1
2 1712
Sorry dude, but this program with or without the line <1works. There
is another issue with this code but this issue isn't related with the
cout stream. On Visual C++ 2005 compiler...

saya-jin

On May 2, 4:48 am, thomas <FreshTho...@gmail.comwrote:
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<iterator>
#include<string>
#include<algorithm>

using namespace std;

#define M 1000009

vector<intprimes;

//generate C(n, 1), C(n, 2), ...
void dfs(vector<int&mp, int its, int i, vector<int&temp){
if(i==0) {
int s=1;
for(vector<int>::iterator it2(temp.begin()); it2!=temp.end();
it2++){ s=s*(*it2); }
if(s>1) mp.push_back(s); return;
}
if(mp[its]==0) return;
int my_its(its);
for(vector<int>::iterator it_t(mp.begin() + its); *it_t!=0; it_t+
+, my_its++){
temp.push_back(*it_t);
dfs(mp, my_its+1, i-1, temp);
temp.pop_back();
}

}

int main(){
vector<intnumbers; numbers.resize(M, 0);

//generate prime numbers
primes.push_back(2);
for(int i=3; i<M; i+=2){
if(numbers[i]==1) continue;
primes.push_back(i);
for(int j=i+i; j<M; j+=i)
numbers[j] = 1;
}

int m, k;
while(cin>>m>>k){
vector<intmprime; int mm=m;
cout<<1; //<1>

//prime factors of m
for(vector<int>::iterator it(primes.begin()); it!=primes.end()
&&*it<=mm; it++){
if(mm%(*it)==0) mprime.push_back(*it);
while(mm%(*it)==0) mm=mm/(*it);
}

//generate C(n,i), push the multiplication result of the "i" factors
in mprime, seperated by 0
int msize = mprime.size();
mprime.push_back(0);
for(int i=2; i<=msize; i++){
vector<inttemp;
dfs(mprime, 0, i, temp);
mprime.push_back(0);
}

//calculate the k-th number x with gcd(x,m)=1
int result = 0; int total=0; bool change=true;
while(total<k){
result += (k-total); total=result;
for(vector<int>::iterator it(mprime.begin()); it!
=mprime.end()&&*it<=m&&*it<=result; it++){
if(*it==0){ change=(change?false:true); continue;}
if(change) total-=(result/(*it));
else total+=(result/(*it));
}
}
cout<<result<<endl;
}

}

---------code--------
The above is the code to calculate the k-th number x of m having
gcd(x,m)=1
notice the line marked <1>
If the <1line exists, everything works fine.
But if I remove line <1>, the guy says error.
what's wrong? Can a simple "cout" do anything evil?
Jun 27 '08 #2
thomas wrote:
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<iterator>
#include<string>
#include<algorithm>

using namespace std;

#define M 1000009

vector<intprimes;

//generate C(n, 1), C(n, 2), ...
void dfs(vector<int&mp, int its, int i, vector<int&temp){
if(i==0) {
int s=1;
for(vector<int>::iterator it2(temp.begin()); it2!=temp.end();
it2++){ s=s*(*it2); }
if(s>1) mp.push_back(s); return;
}
if(mp[its]==0) return;
int my_its(its);
for(vector<int>::iterator it_t(mp.begin() + its); *it_t!=0; it_t+
+, my_its++){
temp.push_back(*it_t);
dfs(mp, my_its+1, i-1, temp);
temp.pop_back();
}
}

int main(){
vector<intnumbers; numbers.resize(M, 0);

//generate prime numbers
primes.push_back(2);
for(int i=3; i<M; i+=2){
if(numbers[i]==1) continue;
primes.push_back(i);
for(int j=i+i; j<M; j+=i)
numbers[j] = 1;
}

int m, k;
while(cin>>m>>k){
vector<intmprime; int mm=m;
cout<<1; //<1>

//prime factors of m
for(vector<int>::iterator it(primes.begin()); it!=primes.end()
&&*it<=mm; it++){
if(mm%(*it)==0) mprime.push_back(*it);
while(mm%(*it)==0) mm=mm/(*it);
}

//generate C(n,i), push the multiplication result of the "i" factors
in mprime, seperated by 0
int msize = mprime.size();
mprime.push_back(0);
for(int i=2; i<=msize; i++){
vector<inttemp;
dfs(mprime, 0, i, temp);
mprime.push_back(0);
}

//calculate the k-th number x with gcd(x,m)=1
int result = 0; int total=0; bool change=true;
while(total<k){
result += (k-total); total=result;
for(vector<int>::iterator it(mprime.begin()); it!
=mprime.end()&&*it<=m&&*it<=result; it++){
if(*it==0){ change=(change?false:true); continue;}
if(change) total-=(result/(*it));
else total+=(result/(*it));
}
}
cout<<result<<endl;
}
}

---------code--------
The above is the code to calculate the k-th number x of m having
gcd(x,m)=1
notice the line marked <1>
If the <1line exists, everything works fine.
But if I remove line <1>, the guy says error.
what's wrong? Can a simple "cout" do anything evil?
Generally this type of problem, removing something that should have no
affect on the program will cause a program fault with or with out it,
generally this means you have undefined behavior in your program.

Undefined behavior can change as your program changes.

Usually the undefined behavior is a buffer overflow or underflow,
overwritting a buffer somewhere. Sometimes this will make a program crash,
sometimes it won't, it all depends on what happens to be in the memory the
buffer is overwriting. And when you change a program the layout tends to
change chaning what is being overwritten.

To test this since you are using vectors, isntead of:
numbers[j] ...
change it to
numbers.at(j) ...
at() checks and makes sure the index is inside the bounds of the array. If
it is not at() will throw an error you can detect.

Give it a try.
--
Jim Langston
ta*******@rocketmail.com
Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Jan | last post by:
I store sql-commands in a database table. In the first step I get the sql command out of the database table with embedded sql. In the second step I try to execute the command, which i got from the...
16
by: Michael | last post by:
just a quickie: as cout << endl; is to cout << "\n"; is there an equivalent for cout << "\t";
6
by: Fao | last post by:
Hi, I am in my first year of C++ in college and my professor wants me to Write a Program with multiple functions,to input two sets of user-defined data types: One type named 'Sign' declared by...
13
by: Fao | last post by:
Hello, I am having some problems with inheritance. The compiler does not not return any error messages, but when I execute the program, it only allows me to enter the number, but nothing else...
23
by: mahesh | last post by:
Hi all, I have following code that is supposed to increase the power by specified value. int main() { system("cls"); int i, exponent; double base; double new_base=0.0;
12
by: kevineller794 | last post by:
I want to make a split string function, but it's getting complicated. What I want to do is make a function with a String, BeginStr and an EndStr variable, and I want it to return it in a char...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.