In: Computer Science
C programming
4. Exploring Pointers and Use of Arrays [15 pts]
In a shell environment, programs are often executed using command
lines arguments. For example:
g++ -o cm03_la01 cm03_la01.c.
In C such program can be develop using a standard for which the
main 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 C
code can also be written as follows:
int main (int argc, char ** argv)
{
/*program here */
}
The first parameter is an integer specifying the number of words
on the command-line, including the
name of the program, so argc is always at least one. The second
parameter is an array of C strings
that stores all of the words from the command-line, including the
name of the program, which is
always in argv[0]. The command-line arguments, if they exist, are
stored in argv[1], ..., up to
argv[argc-1].
Consider the program bin2dec.c, which is a program for binary to
decimal 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 and
understand the functionality
of bin2dec.c. Implement a new program base2dec.c that takes as
command line parameters the
source base 2, 8, or 16 followed by the string of base symbols and
gives the equivalent decimal
value. Below are some examples of execution outputs expected:
“base2dec 3 11111” will have as answer: “Error: The source base
should be 2, , 8, or 16”
“base2dec 2 11111” will have as answer: “The decimal equivalent
value is 31”
“base2dec 16 FF” will have as answer: “The decimal equivalent value
is 255”
“base2dec 8 35” will have as answer: “The decimal equivalent value
is 29”
Deliverable: File base2dec.c
This is what I have so far but it is not complete, please
finish/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 <stdio.h>
#include <math.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
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++)
{
Please find the requested program below. Also including the screenshot of sample output and screenshot of code to understand the indentation.
Please provide your feedback
Thanks and Happy learning!
Sample output:
/*----------------------------------------------------------------------------*/
/* Title: bin2dec.c */
/*----------------------------------------------------------------------------*/
/* Binary to decimal conversion */
/* */
/* To compile: */
/* $ gcc -o bin2dec bin2dec.c */
/* $ chmod 755 bin2dec */
/* */
/* To run: */
/* $ ./bin2dec */
/* */
/*----------------------------------------------------------------------------*/
#include <stdio.h>
#include <math.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
unsigned binary_to_decimal(const char *str)
{
unsigned value = 0;
unsigned i, p;
if (str != NULL)
{
//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--;
}
}
else
{
printf("Cannot convert binary to decimal as the input data is NULL\n");
}
return value;
}
unsigned hex_to_decimal(const char *str)
{
unsigned value = 0;
unsigned i, p;
if (str != NULL)
{
//Check if input is made of numbers 0(ASCII 48) to 9(ASCII 57)
//And alphabets A(ASCII 65) to F(ASCII 70)
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 hexadecimal to Decimal
//Repeat for each digit starting from the last array element
p = strlen(str) - 1;
value = 0;
int numTosub;
for (i = 0; i < strlen(str); i++)
{
if (str[p] >= 48 && str[p] <= 57)
{
//For numbers 1 to 9
numTosub = 48;
}
else
{
//For alphabets A to F
//Since the acii value of A is 65 and the value of A in hex is 10
//the number to subtract is 65 - 10 = 55
numTosub = 55;
}
value += (str[p] - numTosub) * (int)pow((double)16, (double)i);
p--;
}
}
else
{
printf("Cannot convert hexadecimal to decimal as the input data is NULL\n");
}
return value;
}
unsigned oct_to_decimal(const char *str)
{
unsigned value = 0;
unsigned i, p;
if (str != NULL)
{
//Check if input is made of numbers between 0(ASCII 48) and 7 (ASCII 55)
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)
{
printf(" Incorrect data\n");
exit(0);
}
}
//Convert octal to Decimal
//Repeat for each 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)8, (double)i);
p--;
}
}
else
{
printf("Cannot convert octal to decimal as the input data is NULL\n");
}
return value;
}
int main(int argc, char** argv)
{
unsigned value = 0;
if (argc != 3)
{
printf("ERROR: Incorrect number of input parameters. Usage: cm03_la01 <base> <number>\n");
return 0;
}
else
{
if (atoi(argv[1]) == 2 )
{
value = binary_to_decimal(argv[2]);
}
else if (atoi(argv[1]) == 8)
{
value = oct_to_decimal(argv[2]);
}
else if (atoi(argv[1]) == 16)
{
value = hex_to_decimal(argv[2]);
}
else
{
printf("Error: The source base should be 2, 8, or 16\n");
return 0;
}
printf("The decimal equivalent value is %d\n", value);
}
return 0;
}