Question

In: Computer Science

C programming 4. Exploring Pointers and Use of Arrays [15 pts] In a shell environment, programs...

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++)
{

Solutions

Expert Solution

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;
}

Related Solutions

Write a C++ function that accepts array size and pointers to three arrays a1, a2 and...
Write a C++ function that accepts array size and pointers to three arrays a1, a2 and a3 of type float as parameters. It then multiplies a1 and a2 and stored the result in a3. Assume that array multiplication is done by multiplying corresponding array elements, e.g. a3[i] = a1[i] * a2[i] where 0 <= i <= (array size – 1) Write a C++ main program to dynamically create three equal sized float arrays a1, a2 and a3. The size of...
The concept of pointers in C++ is inherited from the C language, which relies extensively on the use of pointers.
  Topic: The concept of pointers in C++ is inherited from the C language, which relies extensively on the use of pointers. What are the advantages and disadvantages of having the functionality of pointers in a programming language?
) Use functions and arrays to complete the following programs. Requirements: • You should use the...
) Use functions and arrays to complete the following programs. Requirements: • You should use the divide-and-conquer strategy and write multiple functions. • You should always use arrays for the data sequences. • You should always use const int to define the sizes of your arrays. a. Write a C++ program. The program first asks the user to enter 4 numbers to form Sequence 1. Then the program asks the user to enter 8 numbers to form Sequence 2. Finally,...
CODE IN C PLEASE Ask for user to input 4 different numbers Use pointers in calculations...
CODE IN C PLEASE Ask for user to input 4 different numbers Use pointers in calculations instead of variables to calculate the sum of those 4 numbers Print the sum Use a pointer to a pointer for print operation
C programming Write a function called string in() that takes two string pointers as arguments. If...
C programming Write a function called string in() that takes two string pointers as arguments. If the second string is contained in the first string, have the function return the address at which the contained string begins. For instance, string in(“hats”, “at”) would return the address of the a in hats. Otherwise, have the function return the null pointer. Test the function in a complete program that uses a loop to provide input values for feeding to the function.
Your task is to modify the program from the Java Arrays programming assignment to use text...
Your task is to modify the program from the Java Arrays programming assignment to use text files for input and output. I suggest you save acopy of the original before modifying the software. Your modified program should: contain a for loop to read the five test score into the array from a text data file. You will need to create and save a data file for the program to use. It should have one test score on each line of...
Exercises on Arrays –using C++programming 1. You want an array with the numbers 100 – 105....
Exercises on Arrays –using C++programming 1. You want an array with the numbers 100 – 105. In the boxes below, fill in what your array should have. Fill in the index of each element below it. Array Index Open up your editor and write the following program: Declare an array that has the numbers 100 to 105. (How many elements are there in the array?) Print the array. Save your file and test it. Compare your results with your table...
Programming Projects Project 1—UNIX Shell This project consists of designing a C program to serve as...
Programming Projects Project 1—UNIX Shell This project consists of designing a C program to serve as a shell interface that accepts user commands and then executes each command in a separate process. Your implementation will support input and output redirection, as well as pipes as a form of IPC between a pair of commands. Completing this project will involve using the UNIX fork(), exec(), wait(), dup2(), and pipe() system calls and can be completed on any Linux, UNIX, or macOS...
C Programming Language: For this lab, you are going to create two programs. The first program...
C Programming Language: For this lab, you are going to create two programs. The first program (named AsciiToBinary) will read data from an ASCII file and save the data to a new file in a binary format. The second program (named BinaryToAscii) will read data from a binary file and save the data to a new file in ASCII format. Specifications: Both programs will obtain the filenames to be read and written from command line parameters. For example: - bash$...
Write a complete C program that searches an element in array using pointers. Please use the...
Write a complete C program that searches an element in array using pointers. Please use the function called search to find the given number. //Function Prototype void search (int * array, int num, int size)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT