|
-
September 1st, 2010, 01:01 AM
#1
N-th Harmonics
this program is design to solve n-th harmonics. it works for number 1-12 from 13-19 it gives me the wrong answer i honestly can figure out where my mistake is, if some one could pin point where it is i would love to know, so that i could fix it myself or if you wish to show me your idea so that i could learn, the fallowing is my code. i dont know have to copy the whole code from my machine so i will send it from copy and paste. also dont take my code make your own if you got an assignment like this
#include <iostream>
#include <iomanip>
using namespace std;
void enter(int, int[]);
void denom(int, long&, int[]);
void numer(int,long , long&, int[]);
int gcd(long, long );
void fix(long&, long&, long);
void make(int& ,int& , int[], int);
void display (long ,long);
int main()
{
int i ,n;
double Hn =0.0;
long den=0;
long num;
long com=0;
cout << "Please enter a value for the n-th Harmonic" << endl;
cin>>n;
int hold=0;
int ans=0;
int data[n];
enter(n,data);
denom(n,den, data);
numer(n,den,num,data);
cout << num << "/" << den<< endl;
com= gcd(num ,den );
fix (num, den, com);
make(hold ,ans , data, n);
display(num,den);
for (i=1; i<=n; i++)
{
Hn=Hn+(double)1/i;
}
cout << n << ": Hn = " << setprecision(12) << Hn << endl;
cout << "The fraction form is :" << num <<"/" << den << endl;
cout << "The GCD is :" << com << endl;
cout<< ans << "/" << hold << endl;
return 0;
}
void make(int& hold,int& ans, int data[], int n)
{
/*
hold=0;
int other=0;
ans=0;
int test1=0;
int test2=0;
hold= data[0];
int bottom1=0;
int bottom2=0;
int top1=1;
int top2=1;
for (int i=1;i<n; i++)
{
other= hold*data[i];
test1= other/hold;
test2=other/data[i];
//test2= test2*1;
// test1=test1*1;
top1=test2*top1;
top2=test1*1;
top1=top1+top2;
hold= other;
}
ans=top1;
*/
}
void enter(int n , int data[])
{
int k=1;
for (int i=0; i<n;i++)
{
data[i]= k;
k++;
}
}
void denom(int n, long& den, int data[])
{
int c=1;
for(int i=0;i<n; i++)
{
c=c* data[i];
}
den=c;
}
void numer(int n,long den, long& num, int data[])
{
int hold=0;
num=0;
for(int i=0; i<n; i++)
{
hold=den/data[i];
num=num+hold;
}
}
int gcd(long num, long den)
{
/* int hold=0;
int c = num % den;
while (c !=0)
{
num=den;
den= c;
c= num%den;
}
return den;
*/
/* while(1)
{
num=num%den;
if(num==0)
return den;
den=den%num;
if(den==0)
return num;
}
*/
int r;
while (den !=0)
{
r=num%den;
num=den;
den=r;
}
return num;
}
void fix(long& num, long& den, long com)
{
num= num/com;
den=den/com;
}
void display(long num,long den)
{
}
-
September 1st, 2010, 01:28 AM
#2
Re: N-th Harmonics
Please paste your code inside [CODE][/CODE]blocks. It is currently un-readable.
Please take the time to comment and format and comment your code. You are asking to provide their help and time for free. If you don't take the time to make it easy for them, they won't bother.
Last edited by monarch_dodra; September 1st, 2010 at 01:40 AM.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
September 1st, 2010, 10:14 AM
#3
Re: N-th Harmonics
Code:
#include <iostream>
#include <iomanip>
using namespace std;
void enter(int, int[]);
void denom(int, long&, int[]);
void numer(int,long , long&, int[]);
int gcd(long, long );
void fix(long&, long&, long);
void make(int& ,int& , int[], int);
void display (long ,long);
int main()
{
int i ,n;
double Hn =0.0;
long den=0;
long num;
long com=0;
cout << "Please enter a value for the n-th Harmonic" << endl;
cin>>n;
int hold=0;
int ans=0;
int data[n];
enter(n,data);
denom(n,den, data);
numer(n,den,num,data);
cout << num << "/" << den<< endl;
com= gcd(num ,den );
fix (num, den, com);
make(hold ,ans , data, n);
display(num,den);
for (i=1; i<=n; i++)
{
Hn=Hn+(double)1/i;
}
cout << n << ": Hn = " << setprecision(12) << Hn << endl;
cout << "The fraction form is :" << num <<"/" << den << endl;
cout << "The GCD is :" << com << endl;
cout<< ans << "/" << hold << endl;
return 0;
}
void make(int& hold,int& ans, int data[], int n)
{
/*
hold=0;
int other=0;
ans=0;
int test1=0;
int test2=0;
hold= data[0];
int bottom1=0;
int bottom2=0;
int top1=1;
int top2=1;
for (int i=1;i<n; i++)
{
other= hold*data[i];
test1= other/hold;
test2=other/data[i];
//test2= test2*1;
// test1=test1*1;
top1=test2*top1;
top2=test1*1;
top1=top1+top2;
hold= other;
}
ans=top1;
*/
}
void enter(int n , int data[])
{
int k=1;
for (int i=0; i<n;i++)
{
data[i]= k;
k++;
}
}
void denom(int n, long& den, int data[])
{
int c=1;
for(int i=0;i<n; i++)
{
c=c* data[i];
}
den=c;
}
void numer(int n,long den, long& num, int data[])
{
int hold=0;
num=0;
for(int i=0; i<n; i++)
{
hold=den/data[i];
num=num+hold;
}
}
int gcd(long num, long den)
{
/* int hold=0;
int c = num % den;
while (c !=0)
{
num=den;
den= c;
c= num%den;
}
return den;
*/
/* while(1)
{
num=num%den;
if(num==0)
return den;
den=den%num;
if(den==0)
return num;
}
*/
int r;
while (den !=0)
{
r=num%den;
num=den;
den=r;
}
return num;
}
void fix(long& num, long& den, long com)
{
num= num/com;
den=den/com;
}
void display(long num,long den)
{
}
-
September 1st, 2010, 10:21 AM
#4
Re: N-th Harmonics
Code:
#include <iostream>
#include <iomanip>
using namespace std;
void enter(int, int[]);
void denom(int, long&, int[]);
void numer(int,long , long&, int[]);
int gcd(long, long );
void fix(long&, long&, long);
void make(int& ,int& , int[], int);
void display (long ,long);
int main()
{
int i ,n;
double Hn =0.0;
long den=0;
long num;
long com=0;
cout << "Please enter a value for the n-th Harmonic" << endl;
cin>>n;
int hold=0;
int ans=0;
int data[n];
enter(n,data);// entering data into array
denom(n,den, data);// finding denominator
numer(n,den,num,data);// finding numerator
cout << num << "/" << den<< endl;//testinf to see what the numerator and denominator are
// before reducing them
com= gcd(num ,den );// finding the greated common divisor
fix (num, den, com);// this is where the numerator and deninator are being simplefied
make(hold ,ans , data, n);// ignore this
display(num,den);// where i will make my display
for (i=1; i<=n; i++)// this finds it in the decimal state
{
Hn=Hn+(double)1/i;
}
cout << n << ": Hn = " << setprecision(12) << Hn << endl;
cout << "The fraction form is :" << num <<"/" << den << endl;
cout << "The GCD is :" << com << endl;
cout<< ans << "/" << hold << endl;
return 0;
}
void make(int& hold,int& ans, int data[], int n)
{
// not use anymore just ignore it
/*
hold=0;
int other=0;
ans=0;
int test1=0;
int test2=0;
hold= data[0];
int bottom1=0;
int bottom2=0;
int top1=1;
int top2=1;
for (int i=1;i<n; i++)
{
other= hold*data[i];
test1= other/hold;
test2=other/data[i];
//test2= test2*1;
// test1=test1*1;
top1=test2*top1;
top2=test1*1;
top1=top1+top2;
hold= other;
}
ans=top1;
*/
}
void enter(int n , int data[])
{
this is where i am entering data into an array
int k=1;
for (int i=0; i<n;i++)
{
data[i]= k;
k++;
}
}
void denom(int n, long& den, int data[])
{
// this is where i am finding the denominator
int c=1;
for(int i=0;i<n; i++)
{
c=c* data[i];
}
den=c;
}
void numer(int n,long den, long& num, int data[])
{
//this is where i am finding the numerator
int hold=0;
num=0;
for(int i=0; i<n; i++)
{
hold=den/data[i];
num=num+hold;
}
}
int gcd(long num, long den)
{
this is where it finds the greatest common divisor
/* int hold=0;
int c = num % den;
while (c !=0)
{
num=den;
den= c;
c= num%den;
}
return den;
*/
/* while(1)
{
num=num%den;
if(num==0)
return den;
den=den%num;
if(den==0)
return num;
}
*/
int r;
while (den !=0)
{
r=num%den;
num=den;
den=r;
}
return num;
}
void fix(long& num, long& den, long com)
{
// this is where it will lower simplify the numerator and denominator
num= num/com;
den=den/com;
}
void display(long num,long den)
{
//dont wory about this part i will do it its easy
}
i forgot the comment the last one in my class its not required to comment so i dont do it
-
September 1st, 2010, 10:27 AM
#5
Re: N-th Harmonics
functions denom() and numer() are probably the problem.
Look at demon() ...
Code:
void denom(int n, long& den, int data[])
{
// this is where i am finding the denominator
int c=1;
for(int i=0;i<n; i++)
{
c=c* data[i];
}
den=c;
}
You are calculating the denominator using an int variable c to hold the answer.
It is probably 32 bits, so the calculated value will be too large for n>12.
Same for numer().
Also, you should verify that sizeof(long) = 8
-
September 1st, 2010, 09:47 PM
#6
Re: N-th Harmonics
I wrote code to calculate spherical harmonics years ago. The problem I had was rounding error. My guess is that rounding error is your problem as well. I am not going to post my code, because I did not write it as a stand alone function and making it one would not be worth my time. However, I will tell you were I got the algorithm for the code from a paper by Libbrecht entitled Practical considerations for the generation of large-order spherical harmonics
-
September 1st, 2010, 10:44 PM
#7
Re: N-th Harmonics
i found out my mistake by my self, i am good with the program now thanks for your help anyways and it wasnt what you though it was i would tell ya if the code was deleted so that no one can take and use it
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|