Question

I am confused how to make program like convert binary to decimal and octal to decimal...

I am confused how to make program like convert binary to decimal and octal to decimal using array in c++.

This is question as below one.

Your program must have at least two significant user defined functions, each using C++ arrays. Programs done using only main function, and done without using arrays, will get no credit. The program you will submit will have a menu driven system as below:

Caution! No validation is done on numbers entered. They must conform to required input form.
******Main Menu******
1. Convert binary to decimal:

2. Convert Octal to decimal: 3. Exit:
1
Enter the binary number :10101

Original binary Number: 10101

Its decimal conversion:21
Caution! No validation is done on numbers entered. They must conform to required input form.
******Main Menu******
1. Convert binary to decimal:
2. Convert Octal to decimal:
3. Exit:

Enter the octal number :177
Original octal Number: 177

Its decimal conversion:127
Caution! No validation is done on numbers entered. They must conform to required input form.
******Main Menu******
1. Convert binary to decimal:
2. Convert Octal to decimal:
3. Exit:
3

And I should use #include <fstream> to exit the program.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE:

#include <iostream>

#include <cmath>

#include <stdlib.h>

using namespace std;

// function to convert bin to decimal

int binToDecimal(int arr[],int n)

{

  int sum=0;

  int count=0;

  for(int i=n-1;i>=0;i--){

    // if arr of i is 1 then multiply it with power of 2 starting with 0

    // and add it in sum

    if(arr[i]==1)

        sum+=pow(2,count);

    count++;

  }

  return sum;

}

// function to convert oct to decimal

int octToDecimal(int arr[],int n)

{

  int sum=0;

  int count=0;

  for(int i=n-1;i>=0;i--){

    // start from the right side of the array and multiple it with the power of 8

    // starting from 0 and add it to sum

    sum+=pow(8,count)*arr[i];

    count++;

  }

  return sum;

}

int main()

{

  int arr[100],choice;

  string s;

  while(true){

    

    cout<<"******Main Menu******\n1. Convert binary to decimal:\n2. Convert Octal to decimal:\n3. Exit:\n";

    cin>>choice;

    switch(choice){

      case 1:

        cout<<"Enter the binary number : ";

        cin>>s;

        cout<<"Original binary Number: "<<s<<endl;

        // insert each strin character in array

        for(int i=0;i<s.length();i++){

        arr[i] = s[i] - '0';

        }

        cout<<"Its decimal conversion: "<<binToDecimal(arr,s.length())<<endl;

        break;

      case 2:

        cout<<"Enter the octal number : ";

        cin>>s;

        cout<<"Original octal Number: "<<s<<endl;

          // insert each strin character in array

        for(int i=0;i<s.length();i++){

        arr[i] = s[i] - '0';

        }

        cout<<"Its decimal conversion: "<<octToDecimal(arr,s.length())<<endl;

        break;

      case 3:

      // exit program

        exit(0);

      default:

      cout<<"Wrong choice\n";

    }

  }

return 0;

}

OUTPUT:

Please upvote if you like my answer and comment below if you have any queries or need any further explanation.

Add a comment
Know the answer?
Add Answer to:
I am confused how to make program like convert binary to decimal and octal to decimal...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a Java program to convert octal (integer) numbers into their decimal number equivalents (exactly the...

    Write a Java program to convert octal (integer) numbers into their decimal number equivalents (exactly the opposite of what you have done in Assignment 4). Make sure that you create a new Project and Java class file for this assignment. Your file should be named “Main.java”. You can read about octal-to-decimal number conversions on wikepedia Assignment 4 is at the bottom Objective Your program should prompt the user to enter a number of no greater than 8 digits. If the...

  • 1. Convert the binary number 10101102 to octal, decimal, and hexadecimal numbers. 2. Convert the decimal...

    1. Convert the binary number 10101102 to octal, decimal, and hexadecimal numbers. 2. Convert the decimal number 236.7510 to binary,octal, and hexadecimal numbers. 3. Add the following two binary numbers: 100111102 and 011110112. Remember to show any carries that are generated along the way. 4. Repeat the previous question, but this time subtract the second binary number from the first. Remember to show any borrows that are required along the way. 5. Determine the encoding of the decimal number 28610...

  • Description For this program, you are going to convert decimal (integer) numbers into their octal number...

    Description For this program, you are going to convert decimal (integer) numbers into their octal number (integer) equivalents. Make sure that you create a new Project and Java class file for this assignment. Your Repl.It file should be named “Main.java”. You can read about octal-to-decimal number conversions from wikepedia or another website Instructions The input to the program will be a single non-negative integer number. If the number is less than or equal to 2097151, convert the number to its...

  • - ZOOM + To TITUITU.UUT 6 Convert each of the following octal numbers to binary, hexadecimal...

    - ZOOM + To TITUITU.UUT 6 Convert each of the following octal numbers to binary, hexadecimal and decimal using the most appropriate conversion method. (a) 371 7. Convert each of the following decimal numbers to binary, octal and decimal using the most appropriate conversion method. (a) 3D65E 8. Show how a 16-bit computer using a two's complement number system would perform the following computations. (a) (2925)10 -(16850).0 = (?). (b) (16850)10-(2925)10 = (?)10

  • 1. Convert the decimal number 435.64 to binary, octal, and hexadecimal. L7 2. Part A. Convert...

    1. Convert the decimal number 435.64 to binary, octal, and hexadecimal. L7 2. Part A. Convert the circuit below into NAND gates. Insert or remove inverters as necessary. Part B. What is the propagation delay from any input to any output for both the original circuit and the NAND gate circuit from part A. Use 1 nS for inverters, 2 nS for NAND 3 nS for NOR, 4 ns for AND, and 5 nS for OR gates.

  • (1) Convert this Hexadecimal to Binary, Octal and Decimal : ABCDEF (2) how the representation of...

    (1) Convert this Hexadecimal to Binary, Octal and Decimal : ABCDEF (2) how the representation of each of these numbers in both two’s complement and sign magnitude formats. Use the following assumptions: ● Assume that the sign magnitude number should be represented in the fewest number of bits possible. ● Assume that the sign bit for negative sign magnitude numbers should be a 1. ● Assume that the two’s complement numbers should be 8 bit numbers. 1. 108 2. -65

  • a) Convert decimal 17.375 to binary, hexadecimal and octal. b) How many bits are needed to...

    a) Convert decimal 17.375 to binary, hexadecimal and octal. b) How many bits are needed to represent a number between -13 ~ +22?

  • 2. Represent the following decimal integers in (unsigned) binary, octal, and hexadecimal forms. Do each conversion...

    2. Represent the following decimal integers in (unsigned) binary, octal, and hexadecimal forms. Do each conversion directly from the decimal form to the other form. You must show all the steps in converting from the decimal form to each other form. a. 77 b. 64 c. 140 3. Represent the following numbers in (unsigned) binary form and in decimal form. Show the conversion steps. a. (AD)16 b. (77)16 c. (17)16 d. (17)8 e. (64)8 f. (140)8

  • C++ program to convert between decimal, hexadecimal, and octal. Please Help!!

    Hi, I need help writing a program that reads in data (hex, octal or decimal values) from an input file and outputs the values in to another base form (hex, octal,decimal) one line at a time depending on the formatting characters provided by the input file. I am posting the code requirements below and an example of what theinput file will look like and what should be output by the program. I only need the one .cpp program file. Thanks...

  • Question (15 pts.) Convert the decimal numbers from 0 to 32 to BCD, Hexadecimal, Octal, and...

    Question (15 pts.) Convert the decimal numbers from 0 to 32 to BCD, Hexadecimal, Octal, and Binary, using the minimum number of bits or digits, and without using calculators Fill your results in the table below. BCD Hex Oct |

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT