Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
#include <stdio.h>
#include <math.h>
#include <stdlib.h>

void main()
{

    int func (double x[], int n);
    int N,i,t,n,m;
    double f;
    printf("input N\n");
    scanf(" %d",&N);
    printf("f=%lf\n",f);
    printf("t=%lf\n",t);

    double PI= 4.0*atan(1.0);

    for(i=1; i<=N; i+2);
    {
        for(t=0; t<=n; t++);
        {
        srand(time(NULL));
        double f = rand();

     x[t]=(4/PI)*((1/i)*sin(2*i*PI*f*t));
     x[t]>-10||x<10;
    }
    }


}


Here is My code to such a task https://imagizer.imageshack.us/v2/1244x699q90/673/af9sg9.jpg[^]

Could you tell me where am I wrong for Part 1?

It is in the formula that it icreases by 2, so how should I store it?
Now I have something like this


C++
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include "time.h"

int func(double x[], int n);
int main()
{

int func (double x[], int n);
int N,i,t,n,m;
double f,x[1000];
printf("input N\n");
scanf(" %d",&N);


double PI= 4.0*atan(1.0);

for(i=1; i<=N; i+2)
{
for(t=0; t<=n; t++)
{
srand(time(NULL));
double f = rand();


x[t]=(4/PI)*((1/i)*sin(2*i*PI*f*t));

if(x[t]>-10||x<10)
printf("%lf\n",x[t]);
}
}

return 0;
}


So I think I have come up with part 1 solution, but I am still figuring out part 2
I have this so far, can you point me where I am wrong?

C++
#include <stdio.h>
#include <math.h>
#include "winbgi2.h"
#include "time.h"
#include <stdlib.h>

int main()
{	int N ;
printf("input N\n");
	scanf(" %d",&N);
	graphics(1000,200);
int n = 10000;
double f = 0.01;
double PI= 4.0*atan(1.0);
 
for(int t=0; t<=n; t++)
{
double y;
y=(4/PI)*sin(2*PI*f*t);
 int p;
 if (y>0)
 p=50;
 
 else
 p=70;
circle(t,p,1);
}
	wait();

	

	for(int i=1; i<=N; i+2)
	{
		for(int t=0; t<=n; t++)
		{
		
     double x;
	 x=(4/PI)*((1/i)*sin(2*i*PI*f*t));
	 circle(t,x,1);
		}
	}
return 0;
}
Posted
Updated 31-May-15 0:25am
v5
Comments
CPallini 15-Jan-15 10:00am    
Why are you using random numbers?
Sergey Alexandrovich Kryukov 15-Jan-15 13:44pm    
Not a trace of Fourier transform, anyway. :-)
—SA
joshrduncan2012 15-Jan-15 10:02am    
for(i=1; i<=N; i+2);

You have a semicolon at the end that may be interfering with the flow process.
(Same with the inner for loop).
Also, what is "i+2"? You are incrementing i by 2 but not storing it anywhere for the next time through the loop. (The inner for loop is correct with "t++".)

1 solution

Quote:
Could you tell me where am I wrong for Part 1?
Your code has nothing to do with Part 1. You should have drawn a square wave of arbitrary frequency. I see nothing of that in your code. It looks you are trying to approximate the square wave with the proposed Fourier expansion. However your code doesn't implement correctly such an approximation.
 
Share this answer
 
Comments
Member 11377492 15-Jan-15 15:45pm    
Yes I know I should have drawn it, but firstly I would like to get the approximation right and the formula. So what should I change to get the right approximation.?
CPallini 15-Jan-15 15:48pm    
You don't need the approximation formula to draw a square wave.
Member 11377492 15-Jan-15 15:50pm    
So what do I need, couse I have no clue. However it is needed for the next parts isn't it?
CPallini 15-Jan-15 16:34pm    
Well, a square wave is a square wave, something like:
-V when -T/2 < t < 0
0 when t = 0
+V when 0 < t < T/2

(with V = const and T = 1/frequency).
Surely it is not difficult to draw (or store in the array).
Member 11377492 15-Jan-15 20:59pm    
I think I did the first part, now I have a problem with the second one.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900