In: Computer Science
I need to understand how I would edit this code without changing anything, only adding.
I am not looking for the exact answer, just information on how I would use the given code to complete some of the todo statements. Thank you!
// include this header file so we can use `printf()`
#include <stdio.h>
// every C program must implement the `main()` function
int main(int argc, char *argv[]) {
//TODO: check for enough arguments
// save the number in an easier-to-use variable
char *number = argv[1];
char *base = argv[2];
//TODO: figure out how many characters in
length
// the base value is (1 or 2?) (optional!)
char *baselength =
//TODO: figure out which base is given
int base = 0;
//TODO: show an error if base is outside bounds
//TODO: figure out how many characters in
length
// the number is (optional!)
//TODO: calculate the decimal number of the
// given value in
int result = 0;
// print the result
printf("%d\n", result);
// program completed successfully, return 0
return 0;
}
DESCRIPTION
You are to write a simple C program that will take 1) a positive number written in a different base and 2) that base in decimal as command line arguments and output the decimal number. For example:
$ ./dec 1101 2 13 $ ./dec 1234 5 194 $ ./dec ZZ 36 1295 $ ./dec CAFE 16 51966 $ ./dec A7 10 INVALID VALUE $ ./dec Usage: ./dec <number> <base> $ ./dec 1111 37 INVALID BASE $ ./dec 1111 1 INVALID BASE
The dollar sign is not something you should type, it just represents the prompt in the terminal.
Makefile
A Makefile is provided for you. Simply type make to compile your program.
You can also type make test to run some tests (basically what you see above). Be sure to test more values than this, though!
You can also type make clean if you want to remove the compiled program (and debugging symbols).
Converting Characters to Numbers
The character '1' is not the same as the number 1. The numeric values that correspond to the actual characters on your screen are outlined in the manpage, execute man ascii in your terminal to view it. Press q to exit.
Project Submission Format
In order for testers to work, you need to submit your project as a file called dec.c, and simply typing make should build it and produce an executable file called dec.
Automated Tests
Compilation
This test will simply test that your program builds when make is called.
Invalid Input
Your program should be able to handle cases where invalid input is passed to the program. In general, it is good practice to never trust user input.
You should catch the following cases:
No Input
If the program is run without any command line arguments, your program should detect it and report it by printing "Usage: ./dec <number> <base>". Print this string exactly. Also, make sure your program exits with a non-zero exit code. The exit code is set when you return in the main function.
Example:
$ ./dec Usage: ./dec <number> <base>
Invalid Base
If the user supplies a number for the base argument that is outside our supported range of 2-36, report it and exit with a non-zero exit code. Print "INVALID BASE" exactly.
Example:
$ ./dec 1111 37 INVALID BASE $ ./dec 1111 1 INVALID BASE
Invalid Value
If the user supplies a number that could not possibly be in the given base, print "INVALID VALUE" exactly and exit with a non-zero exit code.
Example:
$ ./dec A7 10 INVALID VALUE
Random Numbers
This test will just test the correctness of your program's ability to convert numbers to base 10. It will supply random (but valid) arguments and check that your program outputs the correct answer.