Question

In: Computer Science

need c++ format 1.Check endianness on your system . Either Big or Little Endian 2.Convert a...

need c++ format

1.Check endianness on your system

. Either Big or Little Endian

2.Convert a decimal number to 2’s complement (32bits)

E.g., 5 -> 0000 0000 0000 0000 0000 0000 0000 1001   

here is the start code

*********************************************************
#include <iostream>
using namespace std;

void endianness();
void get_two_complement();
void print_menu();

int main(){
    
    int menu;
   
    do {
        print_menu(); 
        cout<< "Enter a number to select the menu: "; 
        cin >>menu;

        if(menu == 1){
            endianness();
        }
        else if (menu == 2){
            get_two_complement();
        } 
        else if (menu == 0){
            break;
        }   
        else {
           printf("wrong input!!!\n");
        }

        print_menu();
        cout<< "Enter a number to select the menu: "; 
        cin >> menu;
     
    }while (menu !=0);
    cout<< "Bye !!!" << endl;
    exit(0);
}

void endianness(){
    cout << "My system uses the following endianness..." << endl;
    //write the code here
    //print either little or big below




   //end of code  
} 

void get_two_complement(){
    int number;
    cout << "Input the number to convert two's complement: ";
    cin >> number;
    cout << number << "'s two's complement is: "; 
    //write the code here



   
    //end of code 
    cout << endl << "[format]:" << endl;
    cout << number << "'s two's complement is: "; 
    cout << "0000 0000 0000 0000 0000 0000 0000 0000"<< endl;
} 

void print_menu(){

    cout << "********************************" << endl; 
    cout << "****    Homwwork 1: Menu    ****" << endl;
    cout << "*  1. Check endianness         *" << endl;
    cout << "*  2. Convert two's complement *" << endl;
    cout << "*  0. Exit the program         *" << endl;
    cout << "********************************" << endl; 

}

Solutions

Expert Solution

#include <iostream>
using namespace std;

void endianness();
void get_two_complement();
void print_menu();

int main(){

int menu;

do {
print_menu();
cout<< "Enter a number to select the menu: "<<endl;
cin >>menu;

if(menu == 1){
endianness();
}
else if (menu == 2){
get_two_complement();
}
else if (menu == 0){
break;
}
else {
cout<<"wrong input!!!"<<endl;
}

print_menu();
cout<< "Enter a number to select the menu: ";
cin >> menu;

}while (menu !=0);
cout<< "Bye !!!" << endl;
return 0;
}

void endianness(){
cout << "My system uses the following endianness..." << endl;
unsigned int x = 5;
char *q = 0;
q = (char*)&x; //A char pointer is assigned to point to the first (least-significant) byte of the integer value(i.e. x).
if (*q == 5) //If the first byte of the integer is having value equal to 5, then the system is Little-Endian. Otherwise the system is Big-Endian.
cout << "Little Endian" << endl;
else
cout<< "Big Endian" << endl;
}

void get_two_complement(){
int number;
cout << "Input the number to convert two's complement: ";
cin >> number;
cout << number << "'s two's complement is: ";

int a[32];int i;
//converting decimal into binary format
for( i =0; i<32; i++){
a[i]=0;
}
for(i=31; number >0; i--) {
a[i]=number%2;
number= number/2;
}

//traverse the array until we get first 1 from right.
for (i = 31 ; i >= 0 ; i--)
if (a[i] == 1)
break;


// If there exists no '1', concatenate 1 at the starting to get the 2's complement.
if (i == -1) {
cout<< "1 0000 0000 0000 0000 0000 0000 0000 0000"<<endl; // 2's complement of '0000...0000' is '10000..0000'
return ;
}

// flip the values after the position of first '1'
for (int k = i-1 ; k >= 0; k--)
{
if (a[k] == 1)
a[k] = 0;
else
a[k] = 1;
}

int count=0;
//Displaying the 2's complement.
for (i = 0 ; i < 32 ; i++){
cout << a[i];
count++;
if(count==4){
count=0;
cout<<"\t";
}
}
cout<<endl;
}

void print_menu(){

cout << "********************************" << endl;
cout << "**** Homwwork 1: Menu ****" << endl;
cout << "* 1. Check endianness *" << endl;
cout << "* 2. Convert two's complement *" << endl;
cout << "* 0. Exit the program *" << endl;
cout << "********************************" << endl;

}


Related Solutions

Write a MIPS program to check if computer is a big-endian or little-endian system. This must...
Write a MIPS program to check if computer is a big-endian or little-endian system. This must be done in MIPS assembly language.
Big Co. owns 80% of the stock of Little Co. On 1/1/23 Little issues $100,000 of...
Big Co. owns 80% of the stock of Little Co. On 1/1/23 Little issues $100,000 of 10%, 10 year bonds directly to Big for $103,000.  Big and Little both use straight-line amortization. Prepare elimination entries RELATING TO THIS INTERCOMPANY TRANSACTION for 2023 and 2024
please check my answer if its right or need better answer. I need example of big...
please check my answer if its right or need better answer. I need example of big five personality traits. so please check my answer • Openness: the founder of x country he is open minded and every one can meet him. also he likes to travel new places and try new food • consciousnesses: like local astonaut called xxxx he is so immagination guy and organized because of his awarness and imagination he went to space to discover more and...
Digital arithmetic: a) Convert +35 to 2-complement b) Convert -35 to 2-complement c) Convert 2-complement from...
Digital arithmetic: a) Convert +35 to 2-complement b) Convert -35 to 2-complement c) Convert 2-complement from 1101 1101 to decimal d) Add 35 - 35 in binary
Big Co. owns 60% of the stock of Little Co.   On 1/1/22, Little Co sells land...
Big Co. owns 60% of the stock of Little Co.   On 1/1/22, Little Co sells land to Big Co for $50,000. The land had cost Little Co. $30,000 several years earlier. On 3/1/25, Big Co sells the land to a thirds party for $80,000 Little Co reports earnings of $50,000 each year. What is the unrealized gain on sale in 2022? What is the income to the NC Interest in 2022 and 2023? What is the income to the NC...
On 1/1/22 Big Co. acquires 40% of Little Co's voting stock for $300,000. Little Co's book...
On 1/1/22 Big Co. acquires 40% of Little Co's voting stock for $300,000. Little Co's book value on that date was $500,000. Little Co. had the following mis-valued assets at 1/1/22: Land: Undervalued by $40,000 (total) Inventory, FIFO basis: Undervalued by $20,000 (total) Equipment, 5 year life: undervalued by $30,000 (total) Any remaining differential is attributed to goodwill. During 2022, Little reports earnings of $50,000 and pays dividends of $10,000 During 2023, Little reports earnings of $60,000 and pays no...
Big Co. purchases shares of Little Co starting on 1/1/21. Little Co. has 100,000 shares of...
Big Co. purchases shares of Little Co starting on 1/1/21. Little Co. has 100,000 shares of stock outstanding. Relevant data shown below: 1/1/21: Purchased 5,000 shares at $18/share, plus $10 commission. 11/1/21: Little Co. paid common dividends totaling $10,000 12/31/21: Little Co. stock trading at $20/share 4/1/22: Purchased 6,000 shares at $21/share, plus $10 commission 11/1/22: Little Co. paid dividends totaling $10,000 12/31/22: Little Co stock trading at $19/share 3/1/23: Sold 1,000 shares of Little Co stock at $19.50/share, less...
C++ Hello .I need to convert this code into template and then test the template with...
C++ Hello .I need to convert this code into template and then test the template with dynamic array of strings also if you can help me move the function out of the class that would be great.also There is a bug where the memory was being freed without using new operator. I cant seem to find it thanks in advance #include using namespace std; class DynamicStringArray {    private:        string *dynamicArray;        int size;    public:   ...
I need to convert the following into C++. The general idea is to create an application...
I need to convert the following into C++. The general idea is to create an application that can support 10 ID's and 10 grades. It needs to calculate the grade average of said ID, and determine if it is an below or above a certain average, ergo A or C. string[10] studentIDArray int[10] gradeArray int averageGrade for(int i = 0; i < gradeArray; i++) { averageGrade += gradeArray[i] } averageGrade = averageGrade / sizeof(gradeArray) for(int i = 0; i <...
How to convert Sudo Code to C language? keyboard_process() {//insert a string. //check for shared memory...
How to convert Sudo Code to C language? keyboard_process() {//insert a string. //check for shared memory If(shared memory not full) { //sendthe string to the shared memory //send signal to process; }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT