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");
}
}
}
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.
Programming Assignment #2 EE 2372 MAKE SURE TO FOLLOW INSTRUCTIONS CAREFULLY, IF YOU HAVE ANY DOUBTS...
C Programming Homework Question 1. The task is to demonstrate the knowledge of using conditional statements and loops. The following incomplete command line-based program was written to mimic a simple calculator. At the current state, the program reads the first number (operand-1) digit by digit and allows the user to select the type of operation. #include <stdio.h> int main() { int count1=0, value=0, number1=0, operation=0; printf("Enter the first operand, one digit at a time ... or enter -1 to stop...
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 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 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 (+)...
With this program you are going to design a math practice program for younger users. You will ask the user to enter two numbers. Only numbers may be entered. If the user enters a word, the program should disregard the entry and wait for an integer entry. There will not be another prompt if the user enters data other than whole numbers (integers). Here is a sample run: Your static methods should accept two integers and return an integer. Do...
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...
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...
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...
Hello, In Python Enhance the prior assignment by doing the following 1) Create a class that contain all prior functions MyLib # This function adds two numbers def addition(a, b): return a + b # This function subtracts two numbers def subtraction(a, b): return a - b # This function multiplies two numbers def multiplication(a, b): return a * b # This function divides two numbers # The ZeroDivisionError exception is raised when division or modulo by zero takes place...
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); }...