Question

In: Computer Science

I need to understand how I would edit this code without changing anything, only adding. I...

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.

  1. Your program should be able to handle all bases from 2 to 36.
  2. You may not call any C library functions except for printf().

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.

Solutions

Expert Solution


Related Solutions

PLEASE SOLVE THIS QUESTION WITHOUT CHANGING THE SOURCE CODE POSTED BELOW. (ONLY ADDING) #include <iostream> using...
PLEASE SOLVE THIS QUESTION WITHOUT CHANGING THE SOURCE CODE POSTED BELOW. (ONLY ADDING) #include <iostream> using namespace std; const int arrSize = 3; // QUESTION ii. COMPLETE A displayMatrix(...) FUNCTION HERE // QUESTION iii. COMPLETE A transposedMatrix(...) FUNCTION HERE // QUESTION iv. COMPLETE A calculateTotal(...) FUNCTION HERE int main(){ // variable declaration int mat[arrSize][arrSize], transMat[arrSize][arrSize] = {0}, total = 0; // QUESTION i. Prompt user to enter 9 integer numbers and fill in into the matrix cout << "\nThe original...
use repil.it edit my code please i already did all part but need to edit more...
use repil.it edit my code please i already did all part but need to edit more its run for some not shwing all intro to C-programin be sure to edit on my code very basic and add good comments thank you 1-Write a program that requests 5 integers from the user and stores them in an array. You may do this with either a for loop OR by getting a string from stdin and using sscanf to store formatted input...
I haves code on bottom. what do i need to edit? Create a subdirectory called proj1.  For...
I haves code on bottom. what do i need to edit? Create a subdirectory called proj1.  For this project you need to create at least two files: proj1.cpp, and makefile. Both files should be placed in the proj1 directory. The file proj1.cpp should contain the main function, int main(). In the main() function, the program should read the input until it reaches the end, counting the number of times each word, number, and character is used. A word is defined as a sequence of letters ('a'..'z' or 'A'..'Z')....
How to design the MHO Relay? Need Guidance by using code/modelled or anything.
How to design the MHO Relay? Need Guidance by using code/modelled or anything.
What do I need to implement this code. I need an ADT //--------------------------------------------- // This would...
What do I need to implement this code. I need an ADT //--------------------------------------------- // This would be the Student.h file //--------------------------------------------- #include <iostream> #include <cassert> using namespace std; // each student have a name, an ID (100000000~999999999), and three grades class Student { private: public: Student(); Student(); setName(); setId(); setGrade (); getName(); getId(); getGrade() ; printAll() ; }; //--------------------------------------------- // This would be the Student.cpp file //--------------------------------------------- //====================== YOUR CODE STARTS HERE ====================== Student::Student() //default constructor { } Student::Student(string aName,...
JAVA- How do I edit the following code as minimally as possible to add this method...
JAVA- How do I edit the following code as minimally as possible to add this method for calculating BMI? BMI Method: public static double calculateBMI(int height, int weight) { double BMI = (((double) weight) * 0.453592d) / ((((double) height) * 0.0254) * (((double) height) * 0.0254)); Format f = new DecimalFormat("##.######"); return (f.format(BMI)); } Code: import java.text.DecimalFormat; import java.util.Scanner; public class test2 { public static void main(String[] args) { DecimalFormat f = new DecimalFormat("##.0"); Scanner reader = new Scanner(System.in); System.out.printf("%10s...
How can I edit this C code to make sure that the letter P and L...
How can I edit this C code to make sure that the letter P and L would show up separately one at a time on an interval of one second on a raspberry pi? 1 #include <stdio.h> 2 #include <unistd.h> 3 #include "sense.h" 4 5 #define WHITE 0xFFFF 6 7 int main(void) { 8     // getFrameBuffer should only get called once/program 9     pi_framebuffer_t *fb=getFrameBuffer(); 10     sense_fb_bitmap_t *bm=fb->bitmap; 11 12      bm->pixel[0][0]=WHITE; 13      bm->pixel[0][1]=WHITE; 14      bm->pixel[0][2]=WHITE; 15      bm->pixel[0][3]=WHITE; 16      bm->pixel[0][4]=WHITE; 17      bm->pixel[0][5]=WHITE;...
I need this in PSEUDO CODE: This looks long, but only because I have to give...
I need this in PSEUDO CODE: This looks long, but only because I have to give you my answer to the first part. First part of the questions (already answered) GDOT has contacted you to help write code to control the cross walk signals in Georgia. You must create a Crosswalk Signal class with three hidden attributes (Walk, Hurry and Wait), two constructors (a default that sets all lights to on and an overloaded that sets Hurry to on for...
I have a homework class, but I don't really understand anything and I have to submit...
I have a homework class, but I don't really understand anything and I have to submit my homework next week. Homework must be written in C ++ program language. Can someone help me please... Working with classes (everything written below is one task): Define a class Date that contains integer variables for day, month, and year. 1.1. Create the necessary methods for the class: set, get, default constructor, constructor with arguments. 1.2. Create a method that calculates the number of...
I need to calculat The duration of bond I just need anything under this topic because...
I need to calculat The duration of bond I just need anything under this topic because the question will be like this so I need any question so I can study it
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT