Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I'm going to ask you to write some code, now. You are going to write a function that will convert a binary number as a

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

I'm going to ask you to write some code, now. You are going to write a function that will convert a binary number as a string to an unsigned integer value. The function you will fill in will be this: ** * Convert a binary representation of a * number to an unsigned integer. * For this function, the values Y and N represent * true (1) and false (0) respectfully. So, the string: * YYNY is equal to 1101 binary, which is equal to 13. * Unexpected characters are ignored. Only Y's and N's are * considered to be valid. Stop converting when you get * to a space character or the end of the string. The * representation is case-sensitive (only Y/N are valid * true and false values). * 'aYNCY YY' should convert to 5 * 'NYNYny' should convert to 5 * @param binary Binary number as a string of 'Y's and 'N's * and other characters. * @returns unsigned int result */ unsigned int bin2dec(const char *binary) For example, if you do this, the output should be 327. printf("%u ", bin2dec("YNYNNNYYY")); I am supplying a simple main program to test that your solution works. Note that this does not test every possible way the program may be used, but if your solution works you should get valid conversions. Step 1: Starter Files Inside the system2 directory you already created, create a new directory named bin2dec and cd into that directory. Then type this command: cp /user/cse320/files/bin2dec/* . This will copy over starter files so you don't have to start from scratch. Step 2: Try It To build and run the program as initially supplied, type these commands: make ./bin2dec Amazingly, you will find that every binary string it tries will be converted to the number 47. That's because you have not filled in the function yet, so it currently just returns a constant value. # I got /usr/bin/Id: cannot open output file bin2dec: Is a directory Then try this command: make test When you type that command, an executable is built with a main we supply that has more robust tests in it. Currently it will not pass any tests: Assertion failed c[46]: actual=47 expected=0 For this case your solution is incorrect Input was: N Be sure you read the header that describes how the function should work. Again, it was expecting a 0. The string "N" is just a single false, so it should be a value of 0. But the function returned a 47. Part of your grade will be based on passing these tests. We may also include additional tests. Step 3: Solve the Problem! Fill in bin2dec and get it working. It does not take much code, but it does take a bit of thought. Details on submission are at the end of the assignment. Hints and Suggestions The result should be an unsigned integer. This makes the problem somewhat easier. The type for unsigned integers in C is: unsigned int. The type to use in printf for outputting an unsigned value is "u", not "d": printf("%u ", unsignedValue); Your solution should ignore any characters that are not "N" or "Y" in the input string except a space, which ends the string. Those characters do not count as a zero or a one. If there are no characters in the input at all, that is a zero. This is a very simple task that requires only about 8 lines of code. The key to solving this task is reading and understanding the page Converting a string to a number. That page describes the process you, will use, just in a different base. If your solution involves any intermediate numbers or strings or reversing the string order you are doing it wrong and will have a great deal of difficulty getting it to work. The whole point of this exercise is to understand how the function fmdecimal works in base 10 and then make a version of that function for a different base, in this case base 2. Frequently Asked Questions I get: undefined reference to 'pow' + I get: cannot find -1320 when I try to test. + My solution works for YYYYYYYYYY, but fails for anything longer. ./bin2 dec 47 'N' converts to 47 'Y' converts to 47 'YNY N Y' converts to 47 'Y Y' converts to 47 'YNNY' converts to 47 'Y' converts to 47 "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY' converts to 47, 'NYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY' converts to 47 NYYYYYYYYYYYNYYYYYYNYYYYYYYYYYYY' converts to 47 'YN' converts to 47 'YNN' converts to 47 'YNNN' converts to 47 'YNNNN' converts to 47 'YNNN' converts to 47

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Theory Icdt 99 7th International Conference Jerusalem Israel January 10 12 1999 Proceedings Lncs 1540

Authors: Catriel Beeri ,Peter Buneman

1st Edition

3540654526, 978-3540654520

More Books

Students also viewed these Databases questions