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 and then the 4 numbers as the remaining arguments.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%
  • Let’s experiment1 with command line arguments some more. Suppose we need integers as command line arguments. We will need to use the atoi() function provided in h to convert the char* argv[] values to integers. atoi() stands for “ASCII to int.” Here is an example program that prints the sum of three integers entered as command line arguments.

 

#include <iostream>

#include <stdlib.h>

using namespace std;

 

void printUsage(char exeName[])

{

    cout << "\n\tUsage: " << exeName << " [int] [int] [int]\n";

    cout << "\n\tReturns the sum of the 3 int command line arguments.\n\n";

}

 

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

{

    if (argc != 4)

    {

        printUsage(argv[0]);

        return 1;

    }

 

    int a = atoi(argv[1]);

    int b = atoi(argv[2]);

    int c = atoi(argv[3]);

    int sum = a + b + c;

    cout << sum << endl;

    return 0;

}

 

 

A more elegant implementation would be:

 

    int sum = 0;

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

    {

        sum += atoi(argv[i]);

    }

 

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 and then the 4 numbers as the remaining arguments. Here is an example of how the program should look in execution:

 

 

In this example, avg is the name of the executable and the user wants the average of 4 numbers, which are 5, 9, 2, and 11. The program then prints the arithmetic mean (average). The benefits of command line arguments should now be obvious. The user simply enters in the name of the executable program followed by the arguments. This saves time for the user by reducing interactivity. It also enables programs to be more easily scripted for automating tasks.

 

Include a usage statement as shown in the above addition example. Since we will need to perform division between two integers, be cautious of type casting. Your program must make use of functions. Show your full source code and a screenshot of your running program.

 

. C:\WINDOWS\system32\cmd.exe
C:\Users\Adam\Deskt op\CYBR210 Code>count
Usage: count [-option] [filename]
Opt ions :
[-1]
[-wi
lettercount
wordcount
C:\Users\Adam\Desktop\CYBR210 Code>count -1 hi.txt
Lettercount : 8
C: \Users\Adam\DesktopCYBR210 Code>count -w hi.txt
Wordcount : 3
C: \Users\Adam\DesktopCYBR210 Code>_
>
Transcribed Image Text:. C:\WINDOWS\system32\cmd.exe C:\Users\Adam\Deskt op\CYBR210 Code>count Usage: count [-option] [filename] Opt ions : [-1] [-wi lettercount wordcount C:\Users\Adam\Desktop\CYBR210 Code>count -1 hi.txt Lettercount : 8 C: \Users\Adam\DesktopCYBR210 Code>count -w hi.txt Wordcount : 3 C: \Users\Adam\DesktopCYBR210 Code>_ >
C: \Users \AdamDesktop\CYBR210 Code>add 4 5
Usage: add Cint] Cint] Cint ]
Returns the sum of the 3 int command line arguments.
C:\Users \AdamDesktop\CYBR210 Code>add 4 59
18
Transcribed Image Text:C: \Users \AdamDesktop\CYBR210 Code>add 4 5 Usage: add Cint] Cint] Cint ] Returns the sum of the 3 int command line arguments. C:\Users \AdamDesktop\CYBR210 Code>add 4 59 18
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Function Arguments
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education