|
-
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)
{
}
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
|