C programming
4. Exploring Pointers and Use of Arrays [15 pts]
In a shell environment, programs are often executed using commandlines arguments. For example:
g++ -o cm03_la01 cm03_la01.c.
In C such program can be develop using a standard for which themain program has two parameters as follows:
int main (int argc, char * argv[ ])
{
/*program here */
}
3
Because of the equivalence between pointers and arrays, the above Ccode can also be written as follows:
int main (int argc, char ** argv)
{
/*program here */
}
The first parameter is an integer specifying the number of wordson the command-line, including the
name of the program, so argc is always at least one. The secondparameter is an array of C strings
that stores all of the words from the command-line, including thename of the program, which is
always in argv[0]. The command-line arguments, if they exist, arestored in argv[1], ..., up to
argv[argc-1].
Consider the program bin2dec.c, which is a program for binary todecimal conversion that passes the
binary string as a command line.
For instance by typing â€bin2dec 11111â€, you should get the
following message: “The Decimal equivalent is: 31â€. Analyze andunderstand the functionality
of bin2dec.c. Implement a new program base2dec.c that takes ascommand line parameters the
source base 2, 8, or 16 followed by the string of base symbols andgives the equivalent decimal
value. Below are some examples of execution outputs expected:
“base2dec 3 11111†will have as answer: “Error: The source baseshould be 2, , 8, or 16â€
“base2dec 2 11111†will have as answer: “The decimal equivalentvalue is 31â€
“base2dec 16 FF†will have as answer: “The decimal equivalent valueis 255â€
“base2dec 8 35†will have as answer: “The decimal equivalent valueis 29â€
Deliverable: File base2dec.c
This is what I have so far but it is not complete, pleasefinish/fix the following code
/*----------------------------------------------------------------------------*/
/* Title: bin2dec.c */
/*----------------------------------------------------------------------------*/
/* Binary to decimal conversion */
/* */
/* To compile: */
/* $ gcc -o bin2dec bin2dec.c */
/* $ chmod 755 bin2dec */
/* */
/* To run: */
/* $ ./bin2dec */
/* */
/*----------------------------------------------------------------------------*/
#include
#include
#include
#include
#include
unsigned binary_to_decimal(const char *str)
{
unsigned value, i, p;
/* Check if input is made of 0s(ASCII 48) and 1s (ASCII 49)*/
for (i= 0; i < strlen(str); i++)
{
if (str[i] != 48 && str[i] != 49 )
{
printf(\" Incorrect data\n\");
exit (0);
}
}
/* Convert Binary to Decimal
Repeat for each binary digit starting from the last array element*/
p = strlen(str) -1;
value = 0;
for (i= 0; i < strlen(str); i++)
{
value += (str[p] - 48) * (int)pow((double)2, (double)i);
 Â
p--;
 Â
 Â
}
return value;
}
unsigned hex_to_decimal(const char *str)
{
unsigned value, i, p;
/* Check if input is made of 0s(ASCII 48) and 1s (ASCII 49)*/
for (i= 0; i < strlen(str); i++)
{
if (str[i] != 48 && str[i] != 49 && str[i] != 50&& str[i] != 51 && str[i] != 52 && str[i]!= 53
&& str[i] != 54 && str[i] != 55 && str[i]!= 56 && str[i] != 57 && str[i] != 65 &&str[i] != 66
&& str[i] != 67 && str[i] != 68 && str[i]!= 69 && str[i] != 70)
{
printf(\" Incorrect data\n\");
exit (0);
}
}
/* Convert Binary to Decimal
Repeat for each binary digit starting from the last array element*/
p = strlen(str) -1;
value = 0;
for (i= 0; i < strlen(str); i++)
{
value += (str[p] - 48) * (int)pow((double)16, (double)i);
p--;
 Â
 Â
}
return value;
}
unsigned oct_to_decimal(const char *str)
{
unsigned value, i, p;
/* Check if input is made of 0s(ASCII 48) and 1s (ASCII 49)*/
for (i= 0; i < strlen(str); i++)
{