Question

I need help programming this question using C language. This program uses input data entered in...

I need help programming this question using C language.

This program uses input data entered in command line at the same time when the command or .exe file is entered. They are called command-line arguments. Because everything entered in command line is taken as a string, these arguments including the command itself or the .exe file name are stored in an array of char pointers, each pointer points to the first character of a string (i.e., argument). The inputs can be positive, negative, or zero as input data to the program. Duplicate numbers are allowed.

Requirements of main()

  1. Each run, the main() of the program will first print the total input numbers it receives. Because this should not include the command or program name itself, this value must be argc-1, assuming argc is the first parameter of main().
  2. Then, main() will retrieve these input numbers from its 2nd input parameter, say, argv, and call the functions below in the given order to perform several simple arithmetic functions. For each function call, main() consistently passes the total input numbers it receives (e.g., argc-1) plus its own 2nd parameter, argv, to the function.
  3. main() will print the result or return of each function call. All numbers must be printed with three decimal positions.
  4. If the program is executed with no numbers entered, main() should display an error message "You must enter at least one number after program name." and stop execution.

Below are prototypes of each function for your design and implementation.

  • float min(int total, char *data[total]);  // it returns the minimum of data[1]~data[total]
    Note 1: The first element of the array, i.e., data[0] is the command or .exe file name.
    Note 2: Each element of data[ ] is a string and needs to be converted to floating-point type. Consider the use of atof() (in stdlib.h) for this purpose. When it gets an invalid input that cannot be converted, it always returns zero. For example,
    • atof("xy44.55") returns 0
    • atof(".aa55") returns 0
    • atof("22.33xy44.55") returns 22.33 and ignores the rest part till end
  • float min2(int total, char *data[total]);  // it returnsthe second minimum of data[1]~data[total]
  • float max(int total, char *data[total]);  // it returns the maximum of data[1]~data[total]
  • float max2(int total, char *data[total]);  // it returns the second maximum of data[1]~data[total]
  • void sum_avg(int total, char *data[total], float *, float *);  // it returns the sum and average of data[1]~data[total] through its 3rd and 4th parameters, respectively.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

// NOTE: In the case when second minimum/maximum do not exist, program will output a large/small number

#include <stdio.h>

#include <stdlib.h>

#define inf 100000

float min(int total, char *data[total])

{

float minValue = inf; // to store minimum number

for(int i = 1; i < total; i++)

{

float cur = atof(data[i]); // current number

if(minValue > cur)

minValue = cur;

}

return minValue;

}


float min2(int total, char *data[total])

{

float small; // minimum number

float s_small; // second minimum number

small = s_small = inf;

for(int i = 1; i < total; i++)

{

float cur = atof(data[i]);

if(small > cur)

{

s_small = small;

small = cur;

}

if(s_small > cur && cur != small)

{

s_small = cur;

}

}

return s_small;

}


float max(int total, char *data[total])

{

float maxValue = -inf; // to store largest number

for(int i = 1; i < total; i++)

{

float cur = atof(data[i]); // current number

if(maxValue < cur)

maxValue = cur;

}

return maxValue;

}


float max2(int total, char *data[total])

{

float large; // maxixum number

float s_large; // second maxixum number

large = s_large = -inf;

for(int i = 1; i < total; i++)

{

float cur = atof(data[i]);

if(large < cur)

{

s_large = large;

large = cur;

}

if(s_large < cur && cur != large)

{

s_large = cur;

}

}

return s_large;

}

void sum_avg(int total, char *data[total], float *sum, float *avg)

{

*sum = 0;

for(int i = 1; i < total; i++)

{

float cur = atof(data[i]);

(*sum) += cur;

}

(*avg) = (*sum)/(total - 1);

}


int main(int argc, char *argv[])

{

if(argc == 1)

{

printf("You must enter at least one number after program name.\n");

return 0;

}

printf("Numbers given: ");

for(int i = 1; i < argc; i++)

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

printf("\n");

printf("Minimum among them is: %f\n", min(argc, argv));

printf("Second minimum among them is: %f\n", min2(argc, argv));

printf("Maximum among them is: %f\n", max(argc, argv));

printf("Second maximum among them is %f\n", max2(argc, argv));

float sum = 0, avg = 0;

sum_avg(argc, argv, &sum, &avg);

printf("Sum of all numbers: %f\n", sum);

printf("Average of all numbers: %f\n", avg);

}

Add a comment
Know the answer?
Add Answer to:
I need help programming this question using C language. This program uses input data entered in...
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
  • 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 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...

  • need this in c programming and you can edit the code below. also give me screenshot...

    need this in c programming and you can edit the code below. also give me screenshot of the output #include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <string.h> #define LINE_SIZE 1024 void my_error(char *s) { fprintf(stderr, "Error: %s\n", s); perror("errno"); exit(-1); } // This funciton prints lines (in a file) that contain string s. // assume all lines has at most (LINE_SIZE - 2) ASCII characters. // // Functions that may be called in this function: // fopen(), fclose(), fgets(), fputs(),...

  • I am writing a program in C++, which requires me to read an input text file...

    I am writing a program in C++, which requires me to read an input text file using command line argument. However, I am using xcode on my Macbook to write C++ program, and use terminal instead of command. How do you use int main(int argc, char** argv[]) to read an input file. My professor requires us not to hard code the text file name like .open("example.txt"); Thank you!

  • Need this in c programming

    Question:Many files on our computers, such as executables and many music and video files, are binary files (in contrast to text files). The bytes in these files must be interpreted in ways that depend on the file format. In this exercise, we write a program data-extract to extract integers from a file and save them to an output file. The format of the binary files in this exercise is very simple. The file stores n integers (of type int). Each...

  • Need C programming help. I've started to work on the program, however I struggle when using...

    Need C programming help. I've started to work on the program, however I struggle when using files and pointers. Any help is appreciated as I am having a hard time comleting this code. #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_LINE 100 #define MAX_NAME 30 int countLinesInFile(FILE* fPtr); int findPlayerByName(char** names, char* target, int size); int findMVP(int* goals, int* assists, int size); void printPlayers(int* goals, int* assists, char** names, int size); void allocateMemory(int** goals, int** assists, char*** names, int size);...

  • For the following task, I have written code in C and need help in determining the...

    For the following task, I have written code in C and need help in determining the cause(s) of a segmentation fault which occurs when run. **It prints the message on line 47 "printf("Reading the input file and writing data to output file simultaneously..."); then results in a segmentation fault (core dumped) I am using mobaXterm v11.0 (GNU nano 2.0.9) CSV (comma-separated values) is a popular file format to store tabular kind of data. Each record is in a separate line...

  • C programming The program will require the following structure: struct _data { char *name; long number;...

    C programming The program will require the following structure: struct _data { char *name; long number; }; The program will require command line arguments: int main(int argv, char **argc) { Where argv is the number of arguments and argc is an array holding the arguments (each is a string). Your program must catch any case where no command line arguement was provided and print a warning message (see below). You MUST include/use the following functions, defined as follows: int SCAN(FILE...

  • In c++ programming, can you please edit this program to meet these requirements: The program that...

    In c++ programming, can you please edit this program to meet these requirements: The program that needs editing: #include <iostream> #include <fstream> #include <iomanip> using namespace std; int cal_avg(char* filenumbers, int n){ int a = 0; int number[100]; ifstream filein(filenumbers); if (!filein) { cout << "Please enter a a correct file to read in the numbers from"; }    while (!filein.eof()) { filein >> number[a]; a++; } int total = number[0]; for (a = 0; a < n; a++) {...

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