Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,402 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,126 people online right now. Registration is fast and FREE... Join Now!




Time-stepping signal generator and reciever using class objects

 
Reply to this topicStart new topic

Time-stepping signal generator and reciever using class objects, Programming assignment

JEM_26
18 Sep, 2006 - 09:37 AM
Post #1

New D.I.C Head
*

Joined: 18 Sep, 2006
Posts: 3


My Contributions
The details of the assignment have been attached. Here is what I have come up with so far. I would appreciate anyone's help.

CODE
#include <iostream>
#include <cmath>
#include <ctime>
#include <fstream>
#include <iomanip>

using namespace std;

class generator
{
public:
       void makeSignal(int signal); //generates random ints and chars to form signal
       void sendDelimiter(char delimiter); //sends delimiter to receiver when no signal
       void displaySignal(); //displays signal generated at each time-step

private:
        int number;  //variable to store random number
        char letter; //variable to store random letter
};

void generator::makeSignal(int signal)
{
  srand(time(NULL));
  
  int r;  
    
  number = rand()%10;
  r = rand()%10;
  letter = 'A' + r;
}

void generator::sendDelimiter(char delimiter)
{
char d = delimiter;
}

class receiver
{
public:
       void makeMatrix(); //displays the signals that have been stored into the matrix

private:
        int matrix[10][10];  //variable to store a 10x10 matrix of integers (signals)
};

void receiver::makeMatrix()
{
int matrix[10][10];
int row;
int col;
                                     //initializes the entire matrix to zero
for (row = 0; row < 10; row++)
     for (col = 0; col < 10; col++)
     matrix[row][col] = 0;
}
//Other important functions              
void showSelection();      //complete
void initializeReceiver(receiver& initiate);
void chooseDistribution();
void numberOfSteps(int steps);
void setFrequency(int freq);
void saveRun(int matrix);

int main()
{
int choice; //variable to hold the selection

showSelection();
cin >> choice;
cout << endl;

while (choice !=6)
{
     switch (choice)
     {
     case 1:
          initializeReceiver(receiver);
          break;
     case 2:
          chooseDistribution();
          break;
     case 3:
          numberOfSteps(int);
          break;
     case 4:
          setFrequency();
          break;
     case 5:
          saveRun();
          break;
     default:
          cout << "Invalid Selection." << endl <<endl;
     }
          showSelection();
          cin >> choice;
          cout << endl;
}
return 0;
}

void showSelection() //User Menu
{
cout << "************** Time-Stepping Simulations **************" << endl << endl;
cout << "1. Initialize the receiver" << endl;
cout << "2. Choose distribution type" << endl;
cout << "3. Set number of time steps" << endl;
cout << "4. Set relative frequency of non-signal time steps" << endl;
cout << "5. Save to file and run simulation" << endl;
cout << "6. Quit" << endl << endl;
cout << "Choose from options 1-6: ";
}

//receiver.makeMatrix();
/*void initializeReceiver(receiver& initiate);
}
initiate.makeMatrix();           //creates an instance of class receiver
} */  

void chooseDistribution()
{
     int choice;

     cout << "1. Uniform Distribution" << endl;
     cout << "2. Normal Distribution" <<endl << endl;
     cout << "Choose 1 or 2: ";
     cin >> choice;
    
     if (choice ==1) //write up uniform distribution function
     {
       cout << "you chose " << choice << endl; //To test
     }
     else //write up normal distribution function
       cout << "you chose " << choice << endl; //to test
}

void numberOfSteps(int steps)
{
int timeSteps;

cout << "How many time steps? " << endl;
cin >> timeSteps;
}

void setFrequency(int freq)
{
int frequency;

cout << "What frequency of non-signal time steps? " << endl;
cin >> frequency;
}

void saveRun(int matrix)
{
ofstream fout;

fout.open("timeSteps.dat", ios::app);

fout << matrix[0][0];

fout.close();
}



Attached File(s)
Attached File  Lab_1_Instructions.doc ( 20.5k ) Number of downloads: 81
User is offlineProfile CardPM
+Quote Post

Xing
RE: Time-stepping Signal Generator And Reciever Using Class Objects
19 Sep, 2006 - 04:16 AM
Post #2

D.I.C Addict
Group Icon

Joined: 22 Jul, 2006
Posts: 723



Thanked: 2 times
Dream Kudos: 1575
My Contributions
Why are your other important functions not in any class?
User is offlineProfile CardPM
+Quote Post

JEM_26
RE: Time-stepping Signal Generator And Reciever Using Class Objects
19 Sep, 2006 - 04:46 AM
Post #3

New D.I.C Head
*

Joined: 18 Sep, 2006
Posts: 3


My Contributions
QUOTE(Xing @ 19 Sep, 2006 - 05:16 AM) *

Why are your other important functions not in any class?


I'm new at programming, so I need all the help I can get. By your remark, I'm guessing the functions should be inside the class. Any other suggestions?
User is offlineProfile CardPM
+Quote Post

JEM_26
RE: Time-stepping Signal Generator And Reciever Using Class Objects
21 Sep, 2006 - 11:49 AM
Post #4

New D.I.C Head
*

Joined: 18 Sep, 2006
Posts: 3


My Contributions
I have made some adjustments to the program, but I still need to finish it. If anyone has any suggestions PLEASE reply. I need all the help I can get! ohno.gif

CODE
#include <iostream>
#include <cmath>
#include <ctime>
#include <fstream>
#include <iomanip>

using namespace std;

//Generator Class:
class generator
{
public:
       void chooseDistribution();          // choose between uniform or normal
       void numberOfSteps();      //sets the # of time steps
       void setFrequency();       //sets non-signal frequency
       void sendDelimiter(generator& G); //sends delimiter to receiver when no signal
       void makeSignal(generator& G);    //generates random ints and chars to form signal
       //void saveRun(generator& G, receiver& R);     saves to file and shows results

private:
        int number;  //variable to store random number
        char letter; //variable to store random letter
        int choiceD;
        double valueD;
        int steps;// number of iteration times
        int frequency;
        int delimiter; //non-signals
        char delimChar;
        int delimInt;
        int signal;
};

double normal(const double &mean, const double &sdiv)
{
  static const double pii=3.1415927;
  static const double r_max=RAND_MAX+1;
  return sdiv*sqrt(-2*log((rand()+1)/r_max))*sin(2*pii*rand()/r_max)+mean;
}

void generator::chooseDistribution()
{
cout << "1. Uniform Distribution" << endl;
cout << "2. Normal Distribution" << endl << endl;
cout << "Choose 1 or 2: ";
cin >> choiceD;
cout << endl;
    
     if (choiceD==1) //write up uniform distribution function
     {
       cout << "You chose uniform distribution." << endl;
     }
     else
     {
       cout << "You chose normal distribution." << endl; //to test
      
       double mean;
       double sd;
      
       cout <<"What mean? ";
       cin >> mean;
       cout << "Standard deviation? ";
       cin >> sd;
       cout << endl;
      
       valueD = normal(mean,sd);
      
       cout << "testing: Value of normal dist is " << valueD << endl << endl;
     }

}

void generator::numberOfSteps()
{
cout << "How many time steps? " << endl;
cin >> steps;
}

void generator::setFrequency()
{
cout << "What frequency of non-signal time steps? " << endl;
cin >> frequency;
}

void generator::sendDelimiter(generator& G)
{
G.numberOfSteps();
G.setFrequency();
delimiter = steps*(frequency/100);
delimChar = '*';
delimInt = -1;
}

void generator::makeSignal(generator& G)
{
  srand(time(NULL));
  
  int r;
  G.sendDelimiter(G);
  G.numberOfSteps();

  while (delimiter <= steps)
      if (delimiter!=0 && steps!=0)
      {
       delimChar = letter;
       delimInt = number;
      }
      else
      {
       number = rand()%10;
       r = rand()%10;
       letter = 'A' + r;
      }
       signal = letter,number;
       cout << "testing the signal: " << signal << endl << endl;
       return;
}

//Receiver Class:
class receiver
{
public:
       void initializeReceiver(); //displays the signals that have been stored into the matrix
       void drawMatrix(receiver& R);

private:
        int matrix[10][10];  //variable to store a 10x10 matrix of integers (signals)
};

void receiver::initializeReceiver()
{
char row;
int col;
                                     //initializes the entire matrix to zero
for (row = 0; row < 10; row++)
     for (col = 0; col < 10; col++)
     matrix[row][col] = 0;
}

void receiver::drawMatrix(receiver& R)
{
int k;
system("cls");

cout<<endl<<endl;
cout<<setw(38)<<"_______________________________"<<endl;
for(int row=0; row<10; row++)
{
  cout<<setw(8)<<"|";
  for(int i=0; i<10; i++)
        {
      
        cout<<setw(10)<<"|";
        
        }
        cout<<endl;
        cout<<setw(8)<<"|";

        for(int i=0; i<10; i++)
        {
         k=row*10+i;      
         cout<<setw(5)<<matrix[k]<<setw(5)<<"|";
        }
         cout<<endl;
         cout<<setw(8)<<"|";
for(int i=0; i<10; i++)
        {
      
        cout<<setw(10)<<"_________|";
        
        }
cout<<endl;
}
cout<<endl<<endl;
}

//Other functions              
void showSelection();      
void saveRun(generator& G,
             receiver& R); //saves to file and runs simulation


int main()
{
generator G;
receiver R;
int choice; //variable to hold the selection

showSelection();
cin >> choice;
cout << endl;

//saveRun();

while (choice !=6)
{
     switch (choice)
     {
     case 1:
          R.initializeReceiver();
          break;
     case 2:
          G.chooseDistribution();
          break;
     case 3:
          G.numberOfSteps();
          break;
     case 4:
          G.setFrequency();
          break;
     case 5:
          saveRun(G, R);
          break;
     default:
          cout << "Invalid Selection." << endl <<endl;
     }
          showSelection();
          cin >> choice;
          cout << endl;
}


return 0;
}

void showSelection() //User Menu
{
cout << "************** Time-Stepping Simulations **************" << endl << endl;
cout << "1. Initialize the receiver" << endl;
cout << "2. Choose distribution type" << endl;
cout << "3. Set number of time steps" << endl;
cout << "4. Set relative frequency of non-signal time steps" << endl;
cout << "5. Save to file and run simulation" << endl;
cout << "6. Quit" << endl << endl;
cout << "Choose from options 1-6: ";
}

void saveRun()
{
}

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 03:09AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month