First of all, I am a complete newbie to the world of C++ and programming in general, so please go easy on me!
I have been set the task of creating a simple console program to investigate the effects of digitally sampling analogue waveforms.
Basically, the sampling effect needs to be investigated using a single cycle of a cos wave (i.e. 0 - 360 degs).
The program has to display the waveform by printing a character (i.e. '*') at the sampling point, one per row of output.
The user should be able to enter an odd number of sampling points from a minimum of 3 to a maximum of 37, and should be able to set a scale factor. I.e if the user entered '5' for the number of points and '20' for the scale factor, something similar to this should be seen:
here is what i've got already. it's almost working fine now with the single problem that when the points are plotted, they just plot in a straight, veritcal line rather than offset at various points (to show the waveform) as they should be!
any help would be gratefully received
Code:
#include<iostream>
#include<cmath>
using namespace std;
const double pi = acos(-1.0);
class wave
{
double pos; // pos is position of plotted point
int sp,sf; // sp and sf are sample points and scale factor
double RA; // Radian Angle
double DA; // Degree Angle
int sposn; // centre position
public:
void plot(int sp);
int read_sp();
int read_sf();
};
int main()
{
char flag; // flag is user query to run again
wave w;
int sp,sf;
do
{
cout<<"\n\t\tCosine Wave Function";
sp=w.read_sp();
sf=w.read_sf();
w.plot(sp);
cout<<"\n Run Again? (y/n) : ";
cin>>flag;
}while((flag=='y')||(flag=='Y'));
return 0;
}
void wave::plot(int sp)
{
DA=360/(sp-1);
sposn=40;
for(int i=0; i<=360; i+=DA)
{
RA=pi*DA/180.0;
pos=sposn+int(sf*cos(RA));
for(int j=0;j<pos;j++)
cout<<" ";
cout<<"*\n";
}
}
int wave::read_sp(void)
{
do
{
cout<<"\n Enter no. of sample points (must be odd & bet. 3-37) : ";
cin>>sp;
if((sp>=3)&&(sp<=37)&&(sp%2==1))
{
return (sp);
}
else
{
cout<<"\n\t ERROR!!";
}
}while((sp<3)||(sp>37)||(sp%2!=1));
}
int wave::read_sf(void)
{
do
{
cout<<"\n Enter the scale factor (20-40) : ";
cin>>sf;
if((sf>=20)&&(sf<=40))
{
return (sf);
}
else
{
cout<<"\n\t ERROR!!";
}
}while((sf<20)||(sf>40));
}
Last edited by jp206; November 30th, 2008 at 08:46 AM.
Reason: added comments
The output position, in terms of character spacing, of a sample point can be calculated as:
position = CentrePosition + ScaleFactor x sin(radianAngle)
where radianAngle = pi x degreeAngle / 180.0
and pi = acos(-1.0)
sin and acos are functions of the standard cmath library.
The calculation of position should produce an integer value in the range 0 80 as 80 is the
width in characters of the DOS console screen. The sin function accepts angles assumed to
be in radians and returns values in the range -1.0 +1.0. CentrePosition,
ScaleFactor and pi should be declared and initialised as constants with suitable values.
Pseudocode fragments for other key sections of the program:
Validating user input:
repeat
set errorFlag = false
display input prompt for noOfPoints
if noOfPoints < 3 or noOfPoints > 31 or noOfPoints % 2 = 0 then
display error message
set errorFlag = true
end if
until errorFlag = false
Outputting the sampled waveform:
set degreeAngle = 0
for count = 0 to noOfPoints - 1
calculate radianAngle
calculate position
output symbol into a field of width = position
increment degreeAngle by 360.0 / noOfPoints - 1
end for
The actual C++ code equivalents of the above pseudocode fragments will be dealt with during
lectures.
Code fragment for obtaining the system date and time:
char dateStr[255] = {'\0'};
time_t timer = time(NULL);
strcpy(dateStr, asctime(localtime(&timer)));
After executing this code, the character array dateStr contains the date and time as defined
by the computer's clock. It can subsequently be displayed on the console screen using cout.
Required libraries for the assignment: <iostream>, <iomanip>, <cmath>, <cstring>
Bookmarks