Question

Programming Assignment #2 EE 2372 MAKE SURE TO FOLLOW INSTRUCTIONS CAREFULLY, IF YOU HAVE ANY DOUBTS...

Programming Assignment #2

EE 2372

MAKE SURE TO FOLLOW INSTRUCTIONS CAREFULLY, IF YOU HAVE ANY DOUBTS PLEASE EMAIL ME!

This programming assignment will just be a small extension to programming assignment 1. In the first assignment each of the functions could only take a predefined number of inputs. This time the objective is to be able to complete these functions with any amount of inputs.

The user will pass arguments through the command line (this means you will have to use argc and argv) then complete the desired function based on the arguments provided.

Example input:

./myExec D 23.1 4 25 57.2

Addition (A or a):

This function will now take all the numbers provided by the user and add them.

The printed statement should read as follows:

“The result of addition is {answer}”

Subtraction (S or s):

This function will now take the first number provided and subtract the rest.

The printed statement should read as follows:

“The result of subtraction is {answer}”

Multiplication (M or m):

This function will now multiply all the numbers provided.

The printed statement should read as follows:

“The result of multiplication is {answer}”

Division (D or d):

This function will take the first number then divide it by the rest.

The printed statement should read as follows:

“The result of division is {answer}”

Prime Function (P or p):

This function will now print out true or false for each input on the same line.

The printed statement should read as follows (assuming 3 numbers are passed):

“{True or False} {True or False} {True or False}”

Deliverables:

Upload your program onto blackboard as a “{first_name}_{last_name}_a1.c” file and make sure that it compiles using gcc. Be sure that your program follows the exact specifications, or you will lose points. Also, your code must be well commented with a comment at the beginning of your file that contains your name, ID number and the course (EE 2372). The format should look like this:

My Name

ID Number

EE 2372

Point Distribution:

Follows the naming specification (5%)

Good comments (5%)

Each function works (90%)

***NOTE***

If your code does not compile using gcc with the command

“gcc {first_initial}_{last_name}_a1.c -o assignment1” then you will receive reduced credit.

Also make sure that all printed statements are in their own line at the end or you it will be marked as incorrect.

***END OF NOTE***

//
//ID:
//EE 2372

//intialized with Standard I/O header
#include<stdio.h>
#include<math.h>
//Entry point of the C program
void main()
{
//Initialized variables (floats, integers, and charactes)
float a, b;
int c, i, r=0;
char command;
//Ask's user for an input character which will determine opperation that would run 
printf("Pick a function: (a)dd, (s)ubstract, (m)ultiply, (d)ivide, (p)prime ");
scanf("%c",&command);

        //if statement for user input of addition
        if(command =='a'||command == 'A'){
                printf("Enter a number: ");
                //program asks and recieves user input for the first number
                scanf("%f", &a);
                printf("Enter a number: ");
                //program asks and recieves user input for the second number
                scanf("%f", &b);
                //Executes mathematical funcion and prints results
                printf("The result of addition is %f\n",a+b);

//for the exectutions of (s, m, and d) functions and executions are identical to the addition, except for the mathematical function that is done
}
        //if statement for user input of subtraction
        if(command == 's'||command == 'S'){
                printf("Enter a number: ");
                scanf("%f", &a);
                printf("Enter a number: ");
                scanf("%f", &b);
                printf("The result of subtraction is %f\n",a-b);
}
        //if statement for user input of multiplication
        if(command == 'm'||command =='M'){
                printf("Enter a number: ");
                scanf("%f", &a);
                printf("Enter a number: ");
                scanf("%f", &b);
                printf("The result of multiplication is %f\n",a*b);
}
        //if statement for user input of division
        if(command == 'd'||command =='D'){
                printf("Enter numerator: ");
                scanf("%f", &a);
                printf("Enter an denomiator: ");
                scanf("%f", &b);
                printf("The result of division is %f\n",a/b);
}
        //if statement for user input of the prime function
        if(command == 'p'||command =='P'){
                printf("Enter a number: ");
                scanf("%d", &c);
                //for loop for determining prime numbers
                for( i = 1; i<= c; i++)
                {
                        if(c%i==0){
                                r++;
                                }
}
                //Result is taken into an if-else statement with an output of truth or false resulting wether the number is prime or not
                if(r==2){
                        printf("\nTrue\n");
                        }
                else{
                        printf("\nFalse\n");
}
}
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code

#include<stdio.h>
#include<math.h>
#include <stdlib.h>
//Entry point of the C program
void main(int argc, char *argv[])
{
   //Initialized variables (floats, integers, and charactes)
   float ans=0;
   int num;
   int countArgc, i,check;
   char command;
   command=argv[1][0];
   countArgc=argc-2;
   if(command =='a'||command == 'A')
   {
       for(i=2;i<argc;i++)
           ans+=atof(argv[i]);
       printf("The result of addition is %.2f\n",ans);
   }
   else if(command =='s'||command == 'S')
   {
       ans=atof(argv[2]);
       for(i=3;i<argc;i++)
           ans-=atof(argv[i]);
       printf("The result of subtraction is %.2f\n",ans);
   }
   else if(command =='m'||command == 'M')
   {
       ans=1;
       for(i=2;i<argc;i++)
           ans*=atof(argv[i]);
       printf("The result of multiplication is %.2f\n",ans);
   }
   else if(command =='d'||command == 'D')
   {
       ans=atof(argv[2]);
       for(i=3;i<argc;i++)
           ans/=atof(argv[i]);
       printf("The result of subtraction is %.2f\n",ans);
   }
   else if(command =='p'||command == 'P')
   {
       for(i=2;i<argc;i++)
       {
           check=checkPrimeNumber(atoi(argv[i]));
           if(check==0)
               printf("{True} ");
           else
               printf("{False} ");
       }
       printf("\n");
   }
}
int checkPrimeNumber(int n)
{
int j, flag = 1;
for(j=2; j <= n/2; ++j)
{
if (n%j == 0)
{
flag =0;
break;
}
}
return flag;
}

output

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.

Add a comment
Know the answer?
Add Answer to:
Programming Assignment #2 EE 2372 MAKE SURE TO FOLLOW INSTRUCTIONS CAREFULLY, IF YOU HAVE ANY DOUBTS...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This is a quick little assignment I have to do, but I missed alot of class...

    This is a quick little assignment I have to do, but I missed alot of class due to the Hurricane and no one here knows what theyre doing. please help! this is Java with intelli-J Overview In this project students will build a four-function one-run calculator on the command line. The program will first prompt the user for two numbers, then display a menu with five operations. It will allow the user to select an option by reading input using...

  • The assignment In this assignment you will take the Matrix addition and subtraction code and modify...

    The assignment In this assignment you will take the Matrix addition and subtraction code and modify it to utilize the following 1. Looping user input with menus in an AskUserinput function a. User decides which operation to use (add, subtract) on MatrixA and MatrixB b. User decides what scalar to multiply MatrixC by c. User can complete more than one operation or cancel. Please select the matrix operation 1- Matrix Addition A+ B 2-Matrix Subtraction A-B 3Scalar Multiplication sC 4-Cancel...

  • OUTCOMES After you finish this assignment, you will be able to do the following: Define an...

    OUTCOMES After you finish this assignment, you will be able to do the following: Define an abstract class Create concrete classes from an abstract class Overload an operator Split classes into .h and .cpp files Open files for reading Write to files Use output manipulators such as setw, fixed, and setprecision DESCRIPTION A binary arithmetic operation takes two double operands (left and right) to perform addition, subtraction, multiplication, or division on. For example, 10 + 11 is an addition (+)...

  • In this lab you will code a simple calculator. It need not be anything overly fancy,...

    In this lab you will code a simple calculator. It need not be anything overly fancy, but it is up to you to take it as far as you want. You will be creating this program in three small sections. You have the menu, the performance of the basic arithmetic operations (Addition, Subtraction Multiplication, and Division), and the looping. You may want to get the switch menu working first, and then fill in the code for these four arithmetic operations...

  • Write an ARM program that implements a simple four-function calculator and prompts the user to enter...

    Write an ARM program that implements a simple four-function calculator and prompts the user to enter a pair of decimal integers (A and B) followed by a character that specifies one of the operators: ‘+’ for addition to compute A+B                             ‘-‘ for subtraction to compute A-B ‘*’ for multiplication to produce the product A*B ‘/’ for division to produce the quotient A/B. Input should be the pair of numbers followed by the operand followed by a return. For example...

  • PLease IN C not in C++ or JAVA, Lab3. Write a computer program in C which...

    PLease IN C not in C++ or JAVA, Lab3. Write a computer program in C which will simulate a calculator. Your calculator needs to support the five basic operations (addition, subtraction, multiplication, division and modulus) plus primality testing (natural number is prime if it has no non-trivial divisors). : Rewrite your lab 3 calculator program using functions. Each mathematical operation in the menu should be represented by a separate function. In addition to all operations your calculator had to support...

  • C programm , ´hello i need your help -Given the C program primes.c with the following main method: int main() { int num, res; char buffer[11]; bool finished = false; while (!finished)...

    C programm , ´hello i need your help -Given the C program primes.c with the following main method: int main() { int num, res; char buffer[11]; bool finished = false; while (!finished) { printf("Enter n > 0 or quit\n"); scanf("%10s", buffer); if (strcmp(buffer, "quit") == 0) { finished = true; } else { // Convert input to number and compute n-th prime num = atoi(buffer); if (num > 0) { res = nth_prime(num); printf("Prime #%d is %d\n", num, res); }...

  • ​​​​​​ C Programming Language The operation of multiplication for positive integers can be expressed as: A.B=2...

    ​​​​​​ C Programming Language The operation of multiplication for positive integers can be expressed as: A.B=2 1 A for (A+[A.(B-1)] B=1 for B>1 Task 1 (for 1 point) is to write a program that will read and print on the screen the values of two integers (A and B) given by user as command line parameters. Task 2 (for 1 point) is to write a program that will calculate the result of the multiplication of two integers. It should print...

  • Mini Project You are required to do this assignment on your own skills. No copying from...

    Mini Project You are required to do this assignment on your own skills. No copying from other students are not allowed. Write a menu driven Bash script using the guidelines given below. Some of the commands and syntax may not be covered in lectures. Please refer other sources to understand syntax and commands you need to complete this assignment. This program takes user input and to perform simple arithmetic operations such as addition, subtraction, multiplication and division of any two...

  • Is Prime Number In this program, you will be using C++ programming constructs, such as functions....

    Is Prime Number In this program, you will be using C++ programming constructs, such as functions. main.cpp Write a program that asks the user to enter a positive integer, and outputs a message indicating whether the integer is a prime number. If the user enters a negative integer, output an error message. isPrime Create a function called isPrime that contains one integer parameter, and returns a boolean result. If the integer input is a prime number, then this function returns...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT