Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Language is C, not C++. Error with strcmp. Copy code and run for specifics. #include #include #include #define HELP --help int main(int argc, char **argv)

Language is C, not C++. Error with strcmp. Copy code and run for specifics.

image text in transcribed

image text in transcribed

#include

#include

#include

#define HELP "--help"

int main(int argc, char **argv)

{

// declaration of variables

int totalCount = argc;

char charArray[100] = {0};

int realArray[100] = {0};

char convertedValue = 0;

int invalidNum = 0;

int validNum = 0;

int sum = 0;

int max = 0;

int min = 0;

float avg = 0;

int realArrayCount = 0;

printf("String = %s ", argv[1]);

if(!(strcmp((char*)argv[1], HELP)))

{

printf("it's a help... Please check and proceed with compilation ");

}

Error with strcmp. Copy code and run for specifics

else

{

for(int i = 2; i

{

sscanf(argv[i], "%d", &convertedValue);

if(convertedValue == 0)

{

charArray[realArrayCount] = argv[i];

invalidNum++;

printf("Invalid string = %s ", argv[i]);

}

else

{

validNum++;

realArray[realArrayCount] = convertedValue;

printf("realArray[%d}: %d ", realArrayCount, realArray[realArrayCount]);

}

realArrayCount++;

}

}

printf("Total Valid Numbers: %d, Total Invalid Numbers: %d ", validNum, invalidNum);

for( int i = 0; i

{

sum += realArray[i];

}

printf("Sum: %d ", sum);

avg = sum / validNum;

printf("Average: %f ", avg);

for(int i = 0; i

{

for(int j = i + 1; j

{

if(realArray[i] > realArray[j])

{

int k;

k = realArray[i];

realArray[i] = realArray[j];

realArray[j] = k;

}

}

}

max = realArray[validNum - 1];

min = realArray[0];

printf("Maximum value: %d ", max);

printf("Minimum value: %d ", min);

printf("Sum: %d ", sum);

printf("Average: %f ", avg);

}

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 expect argv[1] and subsequent arguments to be real numbers (C integer, float or double types) in formats acceptable to the sscanffunction 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 (argc 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 argu are the usual parameters for main and where is argv[0], and rnum is any real number in decimal format. If argv[1] is "--help" display the following. Usage: --help display this usage material. [...] 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 = Min = Max = 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 is to be replaced with the sum. If there are no arguments following argv[0] (argc equals 1). display the following. *** ERROR: No command line arguments were found. Usage: #include Sinclude #include 5 #define HELP --help" 7 int main(int arge, char **arg) // declaration of variables int totalCount - argc; char charArray[100] = {e): int realArray[160] - {@}; char convertedValue = 8: int invalidnum - e; int validum - B; int sum - @; int max - @ int min -e; float avg -e; int realArrayCount - @ printf("String - Xs ", argv[1]); if(!(strcmp((char*)argv[1], HELP))) printf("it's a help... Please check and proceed with compilation "); else for(int i -2; i realArray[J]) int k; k - realArray[i]; realArray[i] - realArray[j]; realArray[j] - k; BAN max - realArray[validum - 1]; min - realArray[@]: printf("Maximum value: Xd ", max): printf("Minimum value: %d ", min); printf("Sum: %d ", sum): printf("Average: Xf ", avg)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started