Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Details We are calling this digit_plow because we are going to use a given digit as a plow to push digits that are less than

Details We are calling this digit_plow because we are going to use a given digit as a plow to push digits that are less than the plow to one side of the plow and digits that are greater than the plow to the other. For example, suppose you are given the number 1148764558 and the plow digit is 3 and you are told to put the larger numbers to the right of the plow, then this would be the resulting number 1148764558. In this case the plow is not present in the number, so nothing changes. The numbers less than 3 are on the left side, and the numbers greater than 3 are on the right. Here is another example. Given this number 1404524817, the plow 4, and we are told to put the larger digits on the right of the plow, then the result would be 1021444587. Since 0, 1, 2, and 3, are less than 4, those appear in the resulting number to the left side of the plow (in the order they appeared in the original number). In this case the input number didnt have 3, so it doesnt appear in the result. Because 5, 6, 7, 8, and 9 are greater than 4, they appear in the result number on the right. Not of all of the larger digits appear in the input number which is why they dont appear in the result. Notice that the same relative order of the digits is retained in the result from the input number. If the plow shows up more than once, there is a group of plow digits in the middle. In general, you are given an input number, a plow digit, and where to store the digits that are larger than the plow, if there are any. The result will be a new number with the digits in the same relative order, but moved to one side or the other of the plow. You can do all of this using basic math operators, e.g. division, multiplication, addition, modulus, etc. Examples You program will be given an input file similar to the one below: 166680978 5 ONLEFT 1148764558 3 ONRIGHT 1404524817 4 ONRIGHT 762260593 5 ONRIGHT 1667879331 6 ONRIGHT 461853093 6 ONLEFT 372184581 9 ONRIGHT 135623999 7 ONRIGHT 566140699 6 ONRIGHT 1340064517 8 ONLEFT Then the provided main function will produce a table like this one Number Result Plow Larger On Which Side 166680978 666897810 5 ONLEFT 1148764558 1148764558 3 ONRIGHT 1404524817 1021444587 4 ONRIGHT 762260593 220357669 5 ONRIGHT 1667879331 1331667879 6 ONRIGHT 461853093 896415303 6 ONLEFT 372184581 372184581 9 ONRIGHT 135623999 135623999 7 ONRIGHT 566140699 514066699 6 ONRIGHT 1340064517 1340064517 8 ONLEFT We will be using the main.c as it is provided for you so you should not modify this file. If you have to modify the main in any way it will not work in testing. Your implementation must do all of the work in the function you are writing. Implementation You will be writing a file named digit_plow.c that will implement the function that is declared in the digit_plow.h file. The first thing your digit_plow.c file should do is #include "digit_plow.h" and then implement the code to produce the correct result. Your function must adhere to the given function declaration. Do not change the function interface or we will not be able to grade it. It must return the result via a return statement. Suggestions These are some suggestions on how to approach solving this problem. You are free to make up your own mind about how you want to approach this, but it may make it harder. Start on the right side of the number, that is in the ones place. Do not try to figure out how many digits are in the number. Starting on the left is possible but makes this much harder. Figure out how to get a single digit from the right side of the number and how to compute a new number that is the old number with the right most digit removed. For example, if the input number is 123, how can you get 3? And how can you get a new number 12? Think about how you can group the sets of digits, the numbers less than the plow, numbers equal to the plow, and greater than the plow. Once you have those groups, how can you recombine them into the answer? Our solution including many braces on their own lines and some blank lines is 50 lines long. So if you are writing 100s of lines of code, you are probably approaching the program in the wrong way. I did not write any other functions. If you think you need to write other functions, you may be approaching the problem in the wrong way. Restrictions You may not convert the number or digits to strings or characters. You may not use arrays, pointers, or structs. No dynamic allocation. It is unnecessary. You may not use the math.h library, nor are we going to be linking to that library. You must use the main.c and digit_plow.h as provided. Do not modify them. You must comment your function as to its purpose. Also any tricky code. Testing Use the provided input and output files to test your code. Use the tool diff or the file difference checker in VS Code to make sure your results match the expected output. Compiling Assuming you are using the name digit_plow.c for your file, this is the command you should use to compile your code. This is also essentially the command we will be using for grading gcc -Wall -Wextra -std=c11 -g -o digit_plow digit_plow.c main.c Then you can run your program, digit_plow, like this ./digit_plow input-2.txt myoutput-2.txt You can diff the results like this diff -w output-2.txt myoutput-2.txt If this doesnt show any differences then you are good with that input file. If it does show differences try this command to help see where the problems are diff -wy output-2.txt myoutput-2.txt The -y option will put the output side-by-side with a | to denote where the issues are. main.c #include #include #include #include "digit_plow.h" int main(int argc, char* argv[]) { if ( argc != 3 ) { fprintf( stderr, "Usage: %s input.txt output.txt ", argv[0] ); exit(1); } FILE* in = fopen(argv[1], "r" ); FILE* out = fopen(argv[2], "w" ); uint64_t number; uint8_t plow; enum Larger where; char where_string[10]; fprintf(out, "Number\t\tResult\t\tPlow\tLarger On Which Side "); fscanf( in, "%" SCNu64 " %" SCNu8 " %s", &number, &plow, where_string ); while( !feof(in) ) { if ( strcmp( "ONRIGHT", where_string ) == 0 ) where = ONRIGHT; else where = ONLEFT; uint64_t result = digit_plow( number, plow, where ); fprintf( out, "%" PRIu64 "\t%" PRIu64 "\t%" PRIu8 "\t%s ", number, result, plow, where_string); fscanf( in, "%" SCNu64 " %" SCNu8 " %s", &number, &plow, where_string ); } fclose(in); fclose(out); return 0; } digit_plow.h #include #include enum Larger { ONLEFT, ONRIGHT}; uint64_t digit_plow( uint64_t input_number, uint8_t plow, enum Larger where); input.txt 1234567890 5 ONLEFT 9876543210 5 ONRIGHT output.txt Number Result Plow Larger On Which Side 1234567890 6789512340 5 ONLEFT 9876543210 4321059876 5 ONRIGHT This is our first C assignment and as such we are going to be focusing on writing a single function. The function will use repetition and selection to reorder digits within a given number Write the Code in C++ please

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

Oracle Solaris 11.2 System Administration (oracle Press)

Authors: Harry Foxwell

1st Edition

007184421X, 9780071844215

More Books

Students also viewed these Databases questions