Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- using namespace std;
- int myFunction(string x){ //12 bit binary decoder
- int y = 0;
- for(int i = 0;i < 12;i++){
- if(x.at(i) == '1'){
- if(i==0){y+=2048;}
- if(i==1){y+=1024;}
- if(i==2){y+=512;}
- if(i==3){y+=256;}
- if(i==4){y+=128;}
- if(i==5){y+=64;}
- if(i==6){y+=32;}
- if(i==7){y+=16;}
- if(i==8){y+=8;}
- if(i==9){y+=4;}
- if(i==10){y+=2;}
- if(i==11){y+=1;}
- }
- }
- cout << "\n"<< y;
- return y;
- }
- int main () {
- int zerocount[12]{0,0,0,0,0,0,0,0,0,0,0,0}, onescount[12]{0,0,0,0,0,0,0,0,0,0,0,0};
- string gamma, epsilon;
- ifstream myfile ("input3.txt");
- string line;
- if (myfile.is_open()){
- while ( getline (myfile,line) ){ //start looping through each line
- for(int i = 0; i < 12;i++){ //for each line loop 12 times, adjust count[i] according to line.at(i)
- if(line.at(i) == '0'){ //counters
- zerocount[i] ++;
- }else{
- onescount[i] ++;
- }
- }
- }
- for(int i =0; i <12;i++){
- if (onescount[i] > zerocount[i]){ //construct gamma/epsilon by concat either a 0 or a 1
- gamma += "1";
- epsilon += "0";
- }
- else {gamma += "0"; epsilon += "1";}
- }
- cout << "\nG: " << gamma << " E: " << epsilon; //print
- int g = myFunction(gamma);
- int e = myFunction(epsilon);
- cout << "\n Final answer is: " <<e*g;
- myfile.close();} else cout << "Unable to open file"; //compacted file reading code
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement