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
  • 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...

  • 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 language using printf and scanf statements: Write a program to display a histogram based...

    In C language using printf and scanf statements: Write a program to display a histogram based on a number entered by the user. A histogram is a graphical representation of a number (in our case, using the asterisk character). On the same line after displaying the histogram, display the number. The entire program will repeat until the user enters zero or a negative number. Before the program ends, display "Bye...". The program will ask the user to enter a non-zero...

  • Write MIPS code, please. Following: Write a program to be used by the Wisconsin DNR to...

    Write MIPS code, please. Following: Write a program to be used by the Wisconsin DNR to track the number of Deer Hunting Licenses issued in a county and the state. It will display a menu with 5 choices numbered 1-5. They are (1) update the count, (2) display the number of licenses already issued to a county whose number is input, (3) display the county number and the number of licenses already issued to a range of counties whose starting...

  • need help on C++ Discussion: The program should utilize 3 files. player.txt – Player name data...

    need help on C++ Discussion: The program should utilize 3 files. player.txt – Player name data file – It has: player ID, player first name, middle initial, last name soccer.txt – Player information file – It has: player ID, goals scored, number of penalties, jersey number output file - formatted according to the example provided later in this assignment a) Define a struct to hold the information for a person storing first name, middle initial, and   last name). b) Define...

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
ADVERTISEMENT