CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

1. Junior Member
Join Date
Oct 2011
Posts
1

## help with functions

hey guys i need to make this code into seprate functions, i have no idea where to start. help !!

Basically its a fourier graph program in which the user enters the number of interations and the program pumps out a graph .

Code:
```#define _USE_MATH_DEFINES
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

int main ()
{
int not;
double unrsum=0;
const int asize =61;
int rsum;
double xy=40/M_PI;
int arrayx[asize];
int max=0;
int min=0;

cout << "FOURIER SERIES GRAPH\n";
cout << "No. of terms = ";
cin>>not;
while (not<=0)

{
cout << "No. of terms must be greater than zero."<<endl;
cout << "Enter No. of terms: ";
cin>> not;
}

for(int theta=0;theta<=360;theta+=6)
{
for(int n=0;n<not;n++)
{
}
if (unrsum > 0)
{
rsum = xy*unrsum + 0.5;
}
else
{
rsum = xy*unrsum - 0.5;
}
int r=theta/6;
arrayx[r]=rsum;
unrsum=0;
}

max = arrayx[0];
for (int i = 1; i < asize; i++)
{
if (arrayx[i] > max)
{
max = arrayx[i];
}
}
min =arrayx[0];
for (int i = 1; i < asize; i++)
{
if (arrayx[i] < min)
{
min = arrayx[i];
}
}
{ for(int i =max;i>=min;i--)

{
if (i==0)
{
cout <<setw(3)<<i << " ";

for (int x=0;x<=asize;x++){

if(i==arrayx[x])
{
cout << "*";
}

else
{
cout << "-";
}
}
}
else
{	cout <<setw(3)<<i<<" ";

for (int x=0;x<=asize;x++){

if(i==arrayx[x])
{
cout << "*";
}

else
{
cout << " ";
}
}

}
cout <<endl;
}

}
}```

thanks so much !

2. ## Re: help with functions

Just create a function and move this code to it. What is the problem?

3. ## Re: help with functions

You can split it up in as many functions you want. What is the reason for splitting it up ?

4. Member +
Join Date
Jan 2009
Posts
596

## Re: help with functions

What you are trying to do is called 'refactoring'. In your case, the first thing to do is work through the code trying to work out which each bit does (maybe adding comments as you go). You should then be able to see which bits 'go together' most tightly. These bits can then be put in separate functions.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•