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

C++ programming test 2, chapters 6,7,& 9 on Functions, Arrays, & Pointers 1. Create a one...
C++ programming test 2, chapters 6,7,& 9 on Functions, Arrays, & Pointers 1. Create a one dimensional array, Ages, which will hold 4 ages. Each age is an int.        b. Write a for loop and print, in reverse order the 4 values stored in memory assuming that the ages in the previous question have already been entered with a space between each value. Use subscript notation.                                     short cnt; c. Do the same as above, but use pointer...
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.
Integer Pointers Program and Analysis Demonstrate an understanding of basic C++ programming concepts by completing the...
Integer Pointers Program and Analysis Demonstrate an understanding of basic C++ programming concepts by completing the following: Program: Create a C++ program that asks the user to enter three integer values as input. Store the values into three different variables. For each variable, create three integer pointers that point to each value. Display the contents of the variables and pointers. In your program, be sure to use the new operator and delete operators to management memory. Program Analysis: Given your...
Question 3: C-Strings and pointers (10 pts) [5] The following function appends C-string str2, to C-string...
Question 3: C-Strings and pointers (10 pts) [5] The following function appends C-string str2, to C-string str1. Complete the function using array access, i.e. str1[i], but no pointer access. Note: For both part a) and part b) you may not use any library function calls (e.g. you cannot use strlen, strcat, etc.) // Append strt2 to str1 void my_strcat(char str1[], char str2[]) { //YOUR CODE HERE   } // example of using my_strcat() #include <stdio.h> int main(void) { char my_str1[50] =...
C++ PROGRAM (Pointers and char arrays) IMPORTANT NOTES: 1. CHAR ARRAY MUST BE USED. 2. YOU...
C++ PROGRAM (Pointers and char arrays) IMPORTANT NOTES: 1. CHAR ARRAY MUST BE USED. 2. YOU MUST USE POINTERS. 3. YOU MUST USE THE SWITCH STATEMENT TO EXECUTE THE PROGRAM. 4. ALL MODIFICATIONS MUST BE DONE IN THE SAME ORIGINAL CHAR ARRAY WITHOUT CREATING A NEW ONE. Write a C++ program that modifies a null teminated char array as follows: Consonants are positioned at the beginning of the string and vowels are moved to the end of the string. Example...
WITHOUT USING POINTERS. This is C programming. I am writing a program which should calculate standard...
WITHOUT USING POINTERS. This is C programming. I am writing a program which should calculate standard deviation of an array of exam scores and then add the standard deviation to each array element. It should print the original array, the standard deviation, and the new rounded adjusted array. I included my question as a comment in the line of code that is giving me an issue. (Removing the a from E-x-a-m as Chegg doesn't allow the word.) #include <stdio.h> #include...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT