Question

Problem: Write a program that behaves as described below.If the first command-line argument after the program...

Problem: Write a program that behaves as described below.If the first command-line argument after the program name (argv[1]) is “--help”, print the usage information for the program. If that argument is not “--help”, you are to expectargv[1]and subsequent arguments to be real numbers(C, integer, float, or double types)in formats acceptable to the sscanf()function of the C library or strings of ASCII chars that are not readable as real numbers. You are to read the numbers, count them and calculate the sum, minimum value, maximum value and average of those numbers, and if there are non-numbers write these to stdout as they are encountered, one per line. If there are no command-line arguments other than the program name(arc equals 1), print a suitable error message and the usage information. If no numbers are among the command line arguments, print a suitable error message and the usage information following the non-numeric arguments present. Naming: Your submitted file is to be named p2.c.Output: Your program’s output must be to stdout and of one of the four formats following, assuming argc and argv are the usual parameters for main()and where <program_name>is argv[0], and rnumis any real number in decimal format.If argv[1]is “--help”, display the following.Usage: <program_name><program_name> --help display this usage material.<program_name> <arg> [<arg> ...]calculate the sum, minimum, maximum and mean of the real number command line arguments. Non-numeric values will be echoed to the standard output device, one per line, with the numeric results printed following the non-numeric lines. If real numbers are among the command line arguments, echo the non-numeric command-line arguments to stdout, then display the following as indicated in the above usage text. For the sequence provided the following values were calculated.Found = <count of numbers>Sum = <sum>Min = <min>Max = <max>Mean = <mean>Where the values indicated in angle-brackets are the corresponding calculated values. Note that you are NOT to replicate the angle-brackets. For example, the entire string <sum>is to be replaced with the sum.If there are no arguments following argv[0](argcequals 1), display the following.*** ERROR: No command line arguments were found.Usage: <program_name><program_name> --helpdisplay this usage material.
<program_name> <arg> [<arg> ...]calculate the sum, minimum, maximum and mean of the real number command line arguments.Non-numeric values will be echoed to the standard output device, one per line, with the numeric results printed following the non-numeric lines. The error message and usage information displayed when arguments are present but no numbers are found should be of a similar format. Note that argv[0]is the program name, which should be used in generating the usage and error messages. In the above, <program_name>is to be replaced by the name of the program as run, without any filename extension, while <arg> [<arg> ...]is to be displayed, not replaced. You are to obtain the name of the program from argv[0], the first command-line argument. Preferably you should pare the path from the name, but this is not required. How to pare the path from the name will be discussed, though.

This must be written in C, not C++.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The required c++ program is given below. The explanations are provided as comments:

#include<stdio.h>
#include<stdlib.h>

//fuction to print error messages
void errorMessage(int i)
{
//if no arguements are passes
if(i == 0)
{
printf("\nerror: no arguements passed");
}
//if no real numbers are passed
if(i == 1)
{
printf("\nerror: no real numbers passed");
}
}

//function to print usage material
void helpMessage()
{
printf("\n Usage: main.c");
printf("\n main.c --help");
printf("\n\t display this usage material");
printf("\n main.c <arg> [<arg> ...]");
printf("\n\t diplay the sum, minimum, maximum and mean of the real");
printf("\n\tnumber command line arguements. Non-numeric values will");
printf("\n\techoed to the standard output device, one per line, with");
printf("\n\tthe numeric results printed following the non-numeric lines.");
}

///function to print no. of real numbers, sum
//minimum, maximum and mean real numbers
void printVals(int c, double sum, double min, double max, double mean)
{
printf("\nFor the sequence provided, the following values were calculated:");
printf("\nFound = %d", c);
printf("\nSum = %f", sum);
printf("\nMin = %f", min);
printf("\nMax = %f", max);
printf("\nMean = %f", mean);
}

//funtion to check if a string is a real number
bool isReal(char c[])
{
double n;
int b;
b = sscanf(c, "%f", &n);
if(b==1) return true;
return false;
}

//function to convert a string into real number
double real(char c[])
{
double n;
sscanf(c, "%f", &n);
return n;
}

int main(int argc, char *argv[])
{
int count=0, i;
double min = 99999.0, max = -99999.0, sum=0.0, mean;
//if no arguements are give
if(argc == 1)
{
errorMessage(0);
return 0;
}
//if --help is in the arguments
if(argv[1] == "--help")
{
helpMessage();
return 0;
}
for(i = 1; i<argc; i++)
{
//if the argument is not a real number
if(!isReal(argv[i])) printf("%s\n", argv[i]);
//if the argument is a real number
else
{
count++;
if(min>real(argv[i])) min = real(argv[i]);
if(max<real(argv[i])) max = real(argv[i]);
sum+=real(argv[i]);
}
}
mean = sum/count;
//if there are no real numbers
if(count == 0)
{
errorMessage(1);
return 0;
}
else
{
//to print the required values
printVals(count, sum, min, max, mean);
}
return 0;
}

That concludes the solution. If you have any doubts or you need more information, please reach out to me in the comment section.

Add a comment
Know the answer?
Add Answer to:
Problem: Write a program that behaves as described below.If the first command-line argument after the program...
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
  • Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text...

    Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text file by removing all blank lines (including lines that only contain white spaces), all spaces/tabs before the beginning of the line, and all spaces/tabs at the end of the line. The file must be saved under a different name with all the lines numbered and a single blank line added at the end of the file. For example, if the input file is given...

  • Your task is to write a C++ program that consumes integer values as command line arguments...

    Your task is to write a C++ program that consumes integer values as command line arguments and returns the arithmetic mean of these values. To increase the flexibility of the program, there should be no set number of arguments. To overcome this, we will require the argument argv[1] to be the number of integers the user enters. For example, if the user wants to calculate the arithmetic mean of 4 numbers, they would pass in 4 as the first argument...

  • C Language Programming. Using the program below - When executing on the command line only this...

    C Language Programming. Using the program below - When executing on the command line only this program name, the program will accept keyboard input and display such until the user does control+break to exit the program. The new code should within only this case situation “if (argc == 1){ /* no args; copy standard input */” Replace line #20 “filecopy(stdin, stdout);” with new code. Open read a text file “7NoInputFileResponse.txt” that contains a message “There were no arguments on the...

  • Command Line Arguments: Write a program in C Compiler that will accept two integers on command...

    Command Line Arguments: Write a program in C Compiler that will accept two integers on command line, subtract the second from the first (1st - 2nd) and display the answer. It exactly two numbers are not provided on command line, there should be a simple error message printed and the program ends with no further input, output, or calculations

  • In C Programming Language, write a command line argument(s) program. The line should have program_name.exe input_file1.txt...

    In C Programming Language, write a command line argument(s) program. The line should have program_name.exe input_file1.txt “char_string”. The program should print an error message if there are not exactly three arguments on the line. input_file1.txt will have a word in it different than the “char_string”. The program will print on one line the char_string concatenated with input_file1.txt followed by the program name. Upon success print on new line, “Program finished!”

  • Write a program that determines if a string (passed as a command-line argument) has all unique...

    Write a program that determines if a string (passed as a command-line argument) has all unique characters. The program must print the number of times each existing character appears, and then print whether or not they are all unique. Special requirements: You are NOT allowed to modify the input string or create any additional data structures (no arrays, lists, dictionaries, or other strings). You may have only one string (the one received as the command-line argument). You may have one...

  • 1) Write a C program that displays all the command line arguments that appear on the...

    1) Write a C program that displays all the command line arguments that appear on the command line when the program is invoked. Use the file name cl.c for your c program. Test your program with cl hello goodbye and cl 1 2 3 4 5 6 7 8 and cl 2) Write a C program which displays the sum of the command line arguments. Hint: use sscanf to convert the decimal arguments (which are strings) to binary numbers that...

  • Write a C++ program that takes two numbers from the command line and perform and arithmetic...

    Write a C++ program that takes two numbers from the command line and perform and arithmetic operations with them. Additionally your program must be able to take three command line arguments where if the last argument is 'a' an addition is performed, and if 's' then subtraction is performed with the first two arguments. Do not use 'cin' or gets() type functions. Do not for user input. All input must be specified on the command line separated by blank spaces...

  • Java!!! Write a program that takes a file name as its command line argument. It assumes...

    Java!!! Write a program that takes a file name as its command line argument. It assumes the file is a binary data file and checks the first 4 bytes of the file to see whether they contain the integer −889275714. It outputs “yes” or “no” or an error message if there was an IOException generated. (Trivia question: Why test for that particular value? Hint: Try it on several .class files.)

  • Part 1: Write a C program that takes an integer command line argument n, spawns n...

    Part 1: Write a C program that takes an integer command line argument n, spawns n processes that will each generate a random numbers between -100 and 100, and then computes and prints out the sum of these random numbers. Each process needs to print out the random number it generates. name the program 003_2.c

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