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
  • Command line input In C++ it is possible to accept command line arguments. Command-line arguments are...

    Command line input In C++ it is possible to accept command line arguments. Command-line arguments are given after the name of a program in command-line operating systems like Linux and are passed in to the program from the operating system. To use command line arguments in the program, it must first understand the full declaration of the main function, which until now has accepted no arguments. In fact, main can accept two arguments: one argument is number of command line...

  • 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...

  • Modify When executing on the command line having only this program name, the program will accept...

    Modify When executing on the command line having 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 your new code. Open read a text file “7NoInputFileResponse.txt” that contains a message “There were no arguments on the command line to be tested...

  • 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...

  • 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...

  • write the bash script Write a script compress_large_files.sh. This script accepts one or more command line...

    write the bash script Write a script compress_large_files.sh. This script accepts one or more command line arguments. The first argument has to be an integer; let’s call it size. If this is the only command line argument, compress_large_files.sh inspects all files in the current working directory and compresses every file of size at least size. If there is more than one command line argument, all arguments except the first one must be valid directories. In this case, compress_large_files.sh inspects 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 C program such that … the program receives ( following argv[0] ) one or...

    Write a C program such that … the program receives ( following argv[0] ) one or more integer values as command line arguments the program prints (to standard output) … the command line arguments, with each appearing on a separate line the sum of the integer values that follow argv[0] Hints http://www.cplusplus.com/reference/cstdlib/strtol THE CORRECT OUTPUT OF THE TEST CASE : The command line arguments are ... argv[0] ... "./a.out" argv[1] ... "2" argv[2] ... "9" argv[3] ... "-100" argv[4] ......

  • 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...

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