Need help to make this code work c++

Watch
JokesOnYoo
Badges: 20
Rep:
?
#1
Report Thread starter 3 years ago
#1
#include <iostream>

#include <string>

using namespace std;



class sequence{







public:

sequence();

sequence(int x);

sequence(int a, int b);

sequence(int a, int b, int c);

~sequence();

sequence& operator+(const sequence &objectrhs);



friend istream& operator >>(istream&, sequence&);

friend ostream& operator <<(ostream&, sequence&);





void set_num_samples(); //Set no. of samples

float* value; //pointer to float variable

void allocate_memory(); //Allocates memory

void set_values_array(); //Sets the values of an array

void check_array_input(float l); //Checks the values of the array

void reset_input(float j); //Resets the array

void de_allocate(); //deallocates memory



int get_num_samples();

int get_num_samples2();

void set_full_array(int x);







private:

int num_samples; //number of samples in the object sequence



};


class FIR{



public:

FIR(int a);

~FIR();



float* value;



void set_full_array(int x);

void allocate_memory();

void apply_filter(sequence& object1);

int get_num_samples();





private:

int num_samples; //Number of samples of the FIR response.





};






//Constructor for each object


void sequence::set_num_samples() { //Set_num_sample definition



cout << "Please enter the number of values in your sequence: \n";



cin >> num_samples;

if(num_samples < 0){





cout << "Value entered must be greater than 0 "<< endl;

cout << "Please enter the value again: " << endl;

cin >> num_samples;





}





}; //ok (stream op)


void sequence::allocate_memory() {



value = new float[num_samples]; //Allocated memory for Array.





};


void sequence::set_values_array(){ //Set values for array



int k;

for(k=0; k<num_samples; k++){





cout << "Please enter a positive value for element : " << (k+1) << endl;

cin >> value[k];

while(value[k] < 0){



cout << "Enter positive value " << endl;

cin >> value[k];



}

}



cout << "Values have been assigned successfully! " << endl;



};



//Constructor functions


void sequence::check_array_input(floa t l) { //Checks array values.





cout << endl << "If you would like to check input values, enter 'y' otherwise, enter 'n' to continue..." << endl;

char check_value;

cin >> check_value;

if (check_value == 'y') {



int i;

for (i = 0; i < l; i++) {



cout << "Value no. " << (i + 1) << " is:" << endl;

cout << value[i] << endl;





}

}

}

void sequence::reset_input(float j) { //Reset voltage value and set to 0;



cout << endl << "If you would like to reset all input values, enter 'y' otherwise, enter 'n' to continue..."

<< endl;



char check_value2;

cin >> check_value2;

if (check_value2 == 'y') {

int i;

for (i = 0; i < j; i++) {



cout << "Value no." << (i + 1) << ": " << value[i];

value[i] = 0;

cout << " is set to 0!" << endl;





}

}

}

void sequence::de_allocate(){ //De-allocate memory



delete[] value;

num_samples = 0;

cout << "De-allocation of input array successful, num of samples reset to 0! " << endl;



}

int sequence::get_num_samples(){



return num_samples;



}

int sequence::get_num_samples2(){



return num_samples;



}


//Combine Coefficient with input voltage

void sequence::set_full_array(int x){



num_samples = x;





}

sequence::sequence(){ //SEQUENCE CONSTRUCTOR



set_num_samples();

allocate_memory();

set_values_array();

check_array_input(num_samples);

reset_input(num_samples);

cout << endl << "Constructor complete!" << endl;



};

sequence::sequence(int a){ //sequence constructor 2 (for full sequence)



set_full_array(a);

allocate_memory();





}

sequence::sequence(int a, int b){ //For combined object sequence.



int h = (a + b);

set_full_array(h);

allocate_memory();



};

sequence::~sequence(){ //Destructor





cout << "Destructor is called" << endl;







};




//Applyign the filter

FIR::FIR(int a){ //constructor for FIR response



set_full_array(a);

allocate_memory();



};


FIR::~FIR(){



cout << "Destructor called" << endl;



}


void FIR::set_full_array(int x){



num_samples = x;





}


void FIR::allocate_memory() {



value = new float[num_samples]; //Allocated memory for Array.





}


int FIR::get_num_samples() {



return num_samples;



}


void FIR::apply_filter(sequence& object1){



int x;

int n;

int y = num_samples;

float temp[num_samples];

for(x=0; x<y; x++){



for(n=0; n<y; n++) {

temp[n] = object1.value[n] + temp[n];

}

temp[x]=value[x];

cout << "Value no. " << (x+1) << value[x] << endl;

}



}





int main(){



// Create object, constructor called

// Constructor calls, set_num_sample, allocate_memory, set_values_array

// Enters values for voltage Inputs to the sequence into an array

// Checks values of the array

// Asks user if they want to reset values and set num samples = 0.





do {

cout << "Press the Enter key to continue:" << endl;

} while (cin.get() != '\n');



cout << "Input voltage A sequence created!" << endl;

sequence inputvoltageA;



cout << endl << "Coefficient A sequence created!" << endl;

sequence coefficientA;







int a = inputvoltageA.get_num_samples2() ;

int b = coefficientA.get_num_samples2();

if(a != b){



cout << "The number of values for input voltage sequence A and coefficient A must be identical!" << endl;

cout << "Input Voltage A has: " << a << " values" << endl;

cout << "Coefficient A has: " << b << " values" << endl;

exit(0);





}



//Combines sequence and coefficients







int x = coefficientA.get_num_samples();

sequence full(x);

int i;

for(i=0; i<x; i++){



cout << endl << "Coefficient value no." << (i+1) << ": " << coefficientA.value[i] << endl;

cout << "Input voltage value no." << (i+1) << ": " << inputvoltageA.value[i] << endl;

full.value[i] = (coefficientA.value[i]) * (inputvoltageA.value[i]);

cout << "Input sequence A value no." << (i+1) << ": " << full.value[i] << endl;





}







//Create new object.



cout << endl << "If you would like to create a new input sequence B enter 'y', otherwise enter 'n'" << endl;



char prompt5;

cin >> prompt5;

if(prompt5 == 'y') {



cout << "Input voltage B sequence created!" << endl;

sequence inputvoltageB;



cout << endl << "Coefficient B sequence created!" << endl;

sequence coefficientB;





int a = inputvoltageB.get_num_samples2() ;

int b = coefficientB.get_num_samples2();

if (a != b) {



cout << "The number of values for input voltage sequence B and coefficient B must be identical!" << endl;

cout << "Input Voltage B has: " << a << " values" << endl;

cout << "Coefficient B has: " << b << " values" << endl;

exit(0);





}





//Combines coefficients



int z = coefficientB.get_num_samples();

sequence fullsecond(z);

int i;

for(i=0; i<z; i++){



cout << endl << "Coefficient B value no." << (i+1) << ": " << coefficientB.value[i] << endl;

cout << "Input voltage B value no." << (i+1) << ": " << inputvoltageB.value[i] << endl;

fullsecond.value[i] = (coefficientB.value[i]) * (inputvoltageB.value[i]);

cout << "Input sequence B value no." << (i+1) << ": " << fullsecond.value[i] << endl;





}







cout << endl << "If you would like to combine input sequence A and B enter 'y', otherwise enter 'n'" << endl;



char prompt6;

cin >> prompt6;

if(prompt6 == 'y'){



int e = full.get_num_samples();

int d = fullsecond.get_num_samples();

int i;

sequence combine(e,d);



for(i=0; i<e; i++){



combine.value[i] = full.value[i];

cout << "Combined sequence value no. " << (i+1) << ": " << combine.value[i] << endl;



}



int z;

for(z=0; z<d; z++){



combine.value[z] = fullsecond.value[z];

cout << "Combined sequence value no. " << (z+e+1) << ": " << combine.value[z] << endl;





}





cout << endl << "Would you like to calculate the the filtered response of the combined sequence enter 'y'" << endl;



char prompt6;

cin >> prompt6;

if(prompt6 == 'y') {



int v;

v = combine.get_num_samples();

FIR response(v); // created the combined a and b sequence and allocated the memory fo array.



int x;

int t;

float temp[v];

for(x=0; x<v; x++){



temp[0] = combine.value[0];

temp[x] = combine.value[x] + temp[x-1];



response.value[x] = temp[0] + temp[x];

cout << "Response value no. " << (x+1) << ": " << response.value[x] << endl;





}



}



}



}

return 0;



}



Cant get the filter at the bottom to work.
Can any coding genius help me out please?

It should be
voutput 1 = vinput1
voutput 2 = vinput1 + vinput2
voutput 3 = vinput1 + vinput2 + vinput3 etc...
0
reply
there8
Badges: 1
Rep:
?
#2
Report 3 years ago
#2
(Original post by JokesOnYoo)
#include <iostream>

#include <string>

using namespace std;



class sequence{







public:

sequence();

sequence(int x);

sequence(int a, int b);

sequence(int a, int b, int c);

~sequence();

sequence& operator+(const sequence &objectrhs);



friend istream& operator >>(istream&, sequence&;

friend ostream& operator <<(ostream&, sequence&;





void set_num_samples(); //Set no. of samples

float* value; //pointer to float variable

void allocate_memory(); //Allocates memory

void set_values_array(); //Sets the values of an array

void check_array_input(float l); //Checks the values of the array

void reset_input(float j); //Resets the array

void de_allocate(); //deallocates memory



int get_num_samples();

int get_num_samples2();

void set_full_array(int x);







private:

int num_samples; //number of samples in the object sequence



};


class FIR{



public:

FIR(int a);

~FIR();



float* value;



void set_full_array(int x);

void allocate_memory();

void apply_filter(sequence& object1);

int get_num_samples();





private:

int num_samples; //Number of samples of the FIR response.





};






//Constructor for each object


void sequence::set_num_samples() { //Set_num_sample definition



cout << "Please enter the number of values in your sequence: \n";



cin >> num_samples;

if(num_samples < 0){





cout << "Value entered must be greater than 0 "<< endl;

cout << "Please enter the value again: " << endl;

cin >> num_samples;





}





}; //ok (stream op)


void sequence::allocate_memory() {



value = new float[num_samples]; //Allocated memory for Array.





};


void sequence::set_values_array(){ //Set values for array



int k;

for(k=0; k<num_samples; k++){





cout << "Please enter a positive value for element : " << (k+1) << endl;

cin >> value[k];

while(value[k] < 0){



cout << "Enter positive value " << endl;

cin >> value[k];



}

}



cout << "Values have been assigned successfully! " << endl;



};



//Constructor functions


void sequence::check_array_input(floa t l) { //Checks array values.





cout << endl << "If you would like to check input values, enter 'y' otherwise, enter 'n' to continue..." << endl;

char check_value;

cin >> check_value;

if (check_value == 'y' {



int i;

for (i = 0; i < l; i++) {



cout << "Value no. " << (i + 1) << " is:" << endl;

cout << value[i] << endl;





}

}

}

void sequence::reset_input(float j) { //Reset voltage value and set to 0;



cout << endl << "If you would like to reset all input values, enter 'y' otherwise, enter 'n' to continue..."

<< endl;



char check_value2;

cin >> check_value2;

if (check_value2 == 'y' {

int i;

for (i = 0; i < j; i++) {



cout << "Value no." << (i + 1) << ": " << value[i];

value[i] = 0;

cout << " is set to 0!" << endl;





}

}

}

void sequence::de_allocate(){ //De-allocate memory



delete[] value;

num_samples = 0;

cout << "De-allocation of input array successful, num of samples reset to 0! " << endl;



}

int sequence::get_num_samples(){



return num_samples;



}

int sequence::get_num_samples2(){



return num_samples;



}


//Combine Coefficient with input voltage

void sequence::set_full_array(int x){



num_samples = x;





}

sequence::sequence(){ //SEQUENCE CONSTRUCTOR



set_num_samples();

allocate_memory();

set_values_array();

check_array_input(num_samples);

reset_input(num_samples);

cout << endl << "Constructor complete!" << endl;



};

sequence::sequence(int a){ //sequence constructor 2 (for full sequence)



set_full_array(a);

allocate_memory();





}

sequence::sequence(int a, int b){ //For combined object sequence.



int h = (a + b);

set_full_array(h);

allocate_memory();



};

sequence::~sequence(){ //Destructor





cout << "Destructor is called" << endl;







};




//Applyign the filter

FIR::FIR(int a){ //constructor for FIR response



set_full_array(a);

allocate_memory();



};


FIR::~FIR(){



cout << "Destructor called" << endl;



}


void FIR::set_full_array(int x){



num_samples = x;





}


void FIR::allocate_memory() {



value = new float[num_samples]; //Allocated memory for Array.





}


int FIR::get_num_samples() {



return num_samples;



}


void FIR::apply_filter(sequence& object1){



int x;

int n;

int y = num_samples;

float temp[num_samples];

for(x=0; x<y; x++){



for(n=0; n<y; n++) {

temp[n] = object1.value[n] + temp[n];

}

temp[x]=value[x];

cout << "Value no. " << (x+1) << value[x] << endl;

}



}





int main(){



// Create object, constructor called

// Constructor calls, set_num_sample, allocate_memory, set_values_array

// Enters values for voltage Inputs to the sequence into an array

// Checks values of the array

// Asks user if they want to reset values and set num samples = 0.





do {

cout << "Press the Enter key to continue:" << endl;

} while (cin.get() != '\n';



cout << "Input voltage A sequence created!" << endl;

sequence inputvoltageA;



cout << endl << "Coefficient A sequence created!" << endl;

sequence coefficientA;







int a = inputvoltageA.get_num_samples2() ;

int b = coefficientA.get_num_samples2();

if(a != b){



cout << "The number of values for input voltage sequence A and coefficient A must be identical!" << endl;

cout << "Input Voltage A has: " << a << " values" << endl;

cout << "Coefficient A has: " << b << " values" << endl;

exit(0);





}



//Combines sequence and coefficients







int x = coefficientA.get_num_samples();

sequence full(x);

int i;

for(i=0; i<x; i++){



cout << endl << "Coefficient value no." << (i+1) << ": " << coefficientA.value[i] << endl;

cout << "Input voltage value no." << (i+1) << ": " << inputvoltageA.value[i] << endl;

full.value[i] = (coefficientA.value[i]) * (inputvoltageA.value[i]);

cout << "Input sequence A value no." << (i+1) << ": " << full.value[i] << endl;





}







//Create new object.



cout << endl << "If you would like to create a new input sequence B enter 'y', otherwise enter 'n'" << endl;



char prompt5;

cin >> prompt5;

if(prompt5 == 'y' {



cout << "Input voltage B sequence created!" << endl;

sequence inputvoltageB;



cout << endl << "Coefficient B sequence created!" << endl;

sequence coefficientB;





int a = inputvoltageB.get_num_samples2() ;

int b = coefficientB.get_num_samples2();

if (a != b) {



cout << "The number of values for input voltage sequence B and coefficient B must be identical!" << endl;

cout << "Input Voltage B has: " << a << " values" << endl;

cout << "Coefficient B has: " << b << " values" << endl;

exit(0);





}





//Combines coefficients



int z = coefficientB.get_num_samples();

sequence fullsecond(z);

int i;

for(i=0; i<z; i++){



cout << endl << "Coefficient B value no." << (i+1) << ": " << coefficientB.value[i] << endl;

cout << "Input voltage B value no." << (i+1) << ": " << inputvoltageB.value[i] << endl;

fullsecond.value[i] = (coefficientB.value[i]) * (inputvoltageB.value[i]);

cout << "Input sequence B value no." << (i+1) << ": " << fullsecond.value[i] << endl;





}







cout << endl << "If you would like to combine input sequence A and B enter 'y', otherwise enter 'n'" << endl;



char prompt6;

cin >> prompt6;

if(prompt6 == 'y'{



int e = full.get_num_samples();

int d = fullsecond.get_num_samples();

int i;

sequence combine(e,d);



for(i=0; i<e; i++){



combine.value[i] = full.value[i];

cout << "Combined sequence value no. " << (i+1) << ": " << combine.value[i] << endl;



}



int z;

for(z=0; z<d; z++){



combine.value[z] = fullsecond.value[z];

cout << "Combined sequence value no. " << (z+e+1) << ": " << combine.value[z] << endl;





}





cout << endl << "Would you like to calculate the the filtered response of the combined sequence enter 'y'" << endl;



char prompt6;

cin >> prompt6;

if(prompt6 == 'y' {



int v;

v = combine.get_num_samples();

FIR response(v); // created the combined a and b sequence and allocated the memory fo array.



int x;

int t;

float temp[v];

for(x=0; x<v; x++){



temp[0] = combine.value[0];

temp[x] = combine.value[x] + temp[x-1];



response.value[x] = temp[0] + temp[x];

cout << "Response value no. " << (x+1) << ": " << response.value[x] << endl;





}



}



}



}

return 0;



}



Cant get the filter at the bottom to work.
Can any coding genius help me out please?

It should be
voutput 1 = vinput1
voutput 2 = vinput1 + vinput2
voutput 3 = vinput1 + vinput2 + vinput3 etc...
Ask the question on stackoverflow. A few tips: there's way too much code, the formatting is off, so it's hard to see what is going on, and you need to give a few more details as to what you have tried so far, and where you got with it.

Stackoverflow will also help you format it correctly using their markup language
0
reply
Joinedup
Badges: 20
Rep:
?
#3
Report 3 years ago
#3
(Original post by there8)
Ask the question on stackoverflow. A few tips: there's way too much code, the formatting is off, so it's hard to see what is going on, and you need to give a few more details as to what you have tried so far, and where you got with it.

Stackoverflow will also help you format it correctly using their markup language
TSR has has a code tag that formats code into something more readable... it's the # symbol on the advanced options.

(haven't used it in a while - might have got broken in an 'upgrade' for all I know)

Code:
#include<iostream>	using namespace std; 	int main()	{		cout << "Hello World!";		return 0;	}
0
reply
Joinedup
Badges: 20
Rep:
?
#4
Report 3 years ago
#4
^^ yeah I think they've *******ed it up tbh
0
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
Back
to top
Latest
My Feed

See more of what you like on
The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

Personalise

Current uni students - are you thinking of dropping out of university?

Yes, I'm seriously considering dropping out (162)
14.48%
I'm not sure (51)
4.56%
No, I'm going to stick it out for now (330)
29.49%
I have already dropped out (33)
2.95%
I'm not a current university student (543)
48.53%

Watched Threads

View All