Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is a C program not a c++ 4. (50 points) write a program that reads a minimu

ID: 3808954 • Letter: T

Question

This is a C program not a c++

4. (50 points) write a program that reads a minimum of three command line arguments and displays the program's results. Use functions to implement the tasks outlined below. a. Command Line arguments: a i. The first argument will be the program name. a.ii. The second argument will be the function to be executed a iii. The third through ending arguments will be the appropriate data. a.iv. write a function to parse the command line data. b. Program will implement the following functions: b.i. A function that finds the largest of list of integer numbers in the command line b.ii. A function that finds the smallest of list of integer numbers in the command line b iii. A function that calculates the sum of integer numbers in the command line iv. A function that calculates the average of integer numbers in the command line. Return type should be an int, not floating point. (Your function should protect against a divide by zero error and display appropriate error messages. b.v. Your program will execute the function specified by the command line input. Treat these functions as command line options. On the command line, function names will be preceded by a or This means the user will type either two double dashes or a single dash followed by one of the following words: max, min, sum, average The command line option --max or-max means your program should call the max function and display the maximum value of all those integer values listed on the command line. The command line option --min or -min means your program should call the min function and display the minimum value of all those integer values listed on the command line. There is no space between the double dashes and the function name on the command line max

Explanation / Answer

I have Executed it in DEV C++ IDE and the command line arguments are given to the program from the tab of execute > parameters as largest 1 2 3 or small 1 2 3 etc...

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

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int largest(int a[],int l)
{
   int i,j;
   int max = a[0];
   for(i = 0;i < l; i++){
       if(a[i] >= max){
           max = a[i];
       }
   }
   return max;
}
int smallest(int a[],int l)
{
   int i,j;
   int small = a[0];
   for(i = 0;i < l; i++){
       if(a[i] <= small){
           small = a[i];
       }
   }
   return small;
}
int sum(int a[],int l){
   int s = 0,i;
   for(i = 0; i < l; i++){
       s = s + a[i];
   }
   return s;
}
int avg(int a[],int l){
   int s = 0,i;
   for(i = 0; i < l; i++){
       s = s + a[i];
   }
   return (int)s/l;
}
int main(int argc, char *argv[]) {
   int i,length = 0,j=0;
   int large[20];
   char *fun;
  
   for(i = 2; i < argc ; i++){
       large[j] = atoi(argv[i]);
       length ++;
       j++;
   }
  
   fun = argv[1];
   if(strcmp(fun,"largest") == 0)
       printf("Largest is = %d ",largest(large,length));
   else if(strcmp(fun,"smallest") == 0)
       printf("Smallest is = %d ",smallest(large,length));
   else if(strcmp(fun,"sum") == 0)
       printf("Sum is = %d ",sum(large,length));
   else if(strcmp(fun,"avg") == 0)
       printf("Avg is = %d ",avg(large,length));
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote