Question

C++ Program Your program should: 1) Validate that the number of commandline arguments, after a.out is...

C++ Program
Your program should:
1) Validate that the number of commandline arguments, after a.out is in the range [1,6]. If not, print an error message and stop the program with exit code 1.
2) Declare a vector of ints or array of ints to store the arguments. Use a loop to parse all the arguments (except a.out) into your vector/array.
3) Use a loop to calculate the greatest number (maximum). If the maximum is greater than 100, print "XNN" on it's own line.
4) Use a loop to add together the numbers that are greater than 10. That means that numbers less than or equal to 10 are skipped for the purpose of adding. If this sum (the result of adding) is greater than 50, print "NNX" on its own line.
5) Use a loop to determine whether the last number appears again; in other words, determine whether the last number is duplicated. If so, print "ow" on its own line.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>

#include <vector>

#include <string.h>

using namespace std;

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

{

    // 1) Validate that the number of commandline arguments,

    // after a.out is in the range [1,6].

    // If not, print an error message and stop the program with exit code 1.

    if (argc < 2 || argc > 7)

    {

        cout << "Error! please enter 1 to 6 integers.\n";

        return 1;

    }

    // 2) Declare a vector of ints or array of ints to store the arguments.

    vector<int> array;

    // Use a loop to parse all the arguments (except a.out) into your vector/array.

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

    {

        // convert to int and add

        array.push_back(atoi(argv[i]));

    }

    // 3) Use a loop to calculate the greatest number (maximum).

    int max = 0;

    for (int i = 0; i < array.size(); i++)

    {

        if (array[i] > max)

            max = array[i];

    }

    // If the maximum is greater than 100, print "XNN" on it's own line.

    if (max > 100)

        cout << "XNN\n";

    // 4) Use a loop to add together the numbers that are greater than 10.

    // That means that numbers less than or equal to 10 are skipped for the purpose of adding.

    int sum = 0;

    for (int i = 0; i < array.size(); i++)

    {

        if (array[i] > 10)

            sum += array[i];

    }

    // If this sum (the result of adding) is greater than 50, print "NNX" on its own line.

    if (sum > 50)

        cout << "NNX\n";

    // 5) Use a loop to determine whether the last number appears again;

    // in other words, determine whether the last number is duplicated.

    for (int i = 0; i < array.size() - 1; i++)

    {

        if (array[i] == array[array.size() - 1])

        {

            // If so, print "ow" on its own line.

            cout << "ow\n";

            break;

        }

    }

    return 0;

}

Output:

PS D:\fixer> . \a.exe 332 XNN NNX PS D:\fixer> .\a.exe 100 NNX PS D:\fixer> .\a.exe 40 10 PS D:\fixer> .\a.exe 40 32 NNX

.

Add a comment
Know the answer?
Add Answer to:
C++ Program Your program should: 1) Validate that the number of commandline arguments, after a.out is...
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
  • 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...

  • This program is in C++ Write a program that validates charge account numbers read in from...

    This program is in C++ Write a program that validates charge account numbers read in from the file Charges.txt following this assignment (A23 Data). Use a selection sort function on the array to sort the numbers then use a binary search to validate the account numbers. Print out the sorted list of account numbers so you can see the valid ones. Prompt the user to enter an account number and the validate that number. If the account number entered is...

  • use C++ to write the following program. needs to ask the user for n The user...

    use C++ to write the following program. needs to ask the user for n The user must put in those 5 values, probably using a for loop. NOTE: You can assume the number of values to be entered is 5. My attempted program below: (not correct, but just a basis) 4. Larger Than n In a program, write a function that accepts three arguments: an array, the size of the array, and a number n. Assume that the array contains...

  • C++ Program - Arrays- Include the following header files in your program:     string, iomanip, iostream Suggestion:...

    C++ Program - Arrays- Include the following header files in your program:     string, iomanip, iostream Suggestion: code steps 1 thru 4 then test then add requirement 5, then test, then add 6, then test etc. Add comments to display assignment //step 1., //step 2. etc. This program is to have no programer created functions. Just do everything in main and make sure you comment each step. Create a program which has: 1. The following arrays created:                 a. an array...

  • (The code should be in C) You should invoke your program by passing the number of...

    (The code should be in C) You should invoke your program by passing the number of available resources of each type on the command line. Ex) ./a.out 3 3 2 Input: Your program will read two files: 1) containing the maximum number of each resources, 2) containing the allocation number of each resources. Output: 1) Whether or not a system is in a safe state: “Safe” or “Not Safe” 2) If the system is in “Safe” state, print the sequence...

  • 7eatng that function Write a C++ program that does the following: Fill an array of 123...

    7eatng that function Write a C++ program that does the following: Fill an array of 123 elements using srand) and rand) with random numbers between 150 and 667. Fill an array of 123 elements with random numbers between 150 and 667. Using a loop. Fill an array of 123 elements with random numbers between 150 and 667. Using a for loop Use a void function to fill an array of 123 elements with random numbers between 150 and 667, Possible...

  • 1. Write a complete C++ program named “CheckAmount” that accepts command line arguments. It checks whether...

    1. Write a complete C++ program named “CheckAmount” that accepts command line arguments. It checks whether there is a command line argument of “-amount” followed by an amount number. It will print out that number amount. For all other cases, it will print out -1. For example, if you run this program with correct arguments as follows, it will print out 1.99, 0.75 and 1.1 respectively CheckAmount -amount 1.99 CheckAmount -help -amount 0.75 CheckAmount -help -check -amount 1.1 -verbose And...

  • python In a program, write a function that accepts two arguments: a list, and a number...

    python In a program, write a function that accepts two arguments: a list, and a number n. Assume that the list contains numbers. The function should display the number n, the list of numbers, and a sub list of all the numbers in the list that are greater than the number n. Initialize the list and the number n in the main function of your code and call your function, passing the list and number as arguments. Run your code...

  • In C Programming Adding to your program in part A, go through the command line arguments...

    In C Programming Adding to your program in part A, go through the command line arguments and find the largest and smallest arguments by alphabetical order. Note that you should not need to sort your arguments, but instead compare them and save the smallest and largest strings as you go through. For example, if called with ./reverse one two three: It would display the output for part A:             Three two one And then it would display   The smallest string was:...

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