Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How to code this exactly so it runs like the examples. Before writing code for t

ID: 3812264 • Letter: H

Question

How to code this exactly so it runs like the examples. Before writing code for this step, work through a few examples on a piece of paper to understand involved. Once you understand how mathematics can solve this problem, then implement your so step in your program. A sample run should loot, like the following: Enter a binary number of up to 10 digits: 100100 The binary number you entered was: 100100 The decimal equivalent of that number is: 36 Once you have the number of digits, you can declare an array of that size, and continue on with figuring our Lab Assignment above. Once you've finished test your program with various valid inputs to make sure your working correctly. Also, your program's I/O must match the examples above and the additional examples Additional Example 1: Enter a binary number of up to 10 digits: 1111000111 The binary number you entered was: 1111000111 The decimal equivalent of that number is: 967 Additional Example 2: Enter a binary number of up to 10 digits: 0011001 The binary number you entered was: 11001 The decimal equivalent of that number is: 25 Turn In Your Work Before turning in your assignment, make sure you have followed all of the instructions stated in th and any additional instructions given by your lab instructor(s). Always test, test, and retest that yo compiles and runs successfully on our Unix machines before submitting it. Show your TA that you completed the assignment. Then submit and upload your lab7.c project code to the CPSC 1011 Labs website on Canvas under the correct lab assignment. After you sub Canvas for this program and other programs that you write this semester, always double check than the correct file(s) (to do this, download your submission from Canvas, view it, and make sure the compiles and runs correctly on our Unix machines).

Explanation / Answer

C++ Code:

#include<bits/stdc++.h>
using namespace std;
int main()
{
   string s;
   cout<<"Enter a binary number of upto 10 digits:"<<endl;
   cin>>s;
   cout<<"The binary number you entered was:"<<endl;
   cout<<s<<endl;
   long long int decimal=0;
   int i,j;
   for(i=0;i<s.length();i++) //s.length() gives size of string
   {   
       decimal+=(s[i]-'0')*pow(2,s.length()-i-1); //pow(a,b) function returns a raised to power b
   }
   cout<<"The decimal equivalent of that number is: ";
   cout<<decimal<<endl;
   return 0;
  
}

Output:

Enter a binary number of upto 10 digits:
1111000111
The binary number you entered was:
1111000111
The decimal equivalent of that number is: 967

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote