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.

Systems Architecture
7th Edition
ISBN:9781305080195
Author:Stephen D. Burd
Publisher:Stephen D. Burd
Chapter12: Secondary Storage Management
Section: Chapter Questions
Problem 10VE
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
  • SEE MORE QUESTIONS
Recommended textbooks for you
Systems Architecture
Systems Architecture
Computer Science
ISBN:
9781305080195
Author:
Stephen D. Burd
Publisher:
Cengage Learning
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,
LINUX+ AND LPIC-1 GDE.TO LINUX CERTIF.
LINUX+ AND LPIC-1 GDE.TO LINUX CERTIF.
Computer Science
ISBN:
9781337569798
Author:
ECKERT
Publisher:
CENGAGE L