Question

The main(int argc, char *argv[]) function will perform the following: • Evaluate whether value in argc...

The main(int argc, char *argv[]) function will perform the following:
• Evaluate whether value in argc is greater or equal to 3.

• If it is not, then print the following message: Incorrect number of arguments -

please call with assignment min max
where assignment is the name of your executable and min/max define the range of numbers for your array.

• If argc is ≥ 3 convert the values for min and max into integers and store them in suitable variables.

• Make sure that min ≤ max - if not, swap both values. • Create an integer array with 10 elements, call fillArray(...) to fill your array with random values in range min to max (both inclusive). Call printArray(...) to display the content of your array to the screen.

• Repeat the previous item with an array of size 50.

C++ program

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Given below is the code for teh question. Please do rate the asnwer if it helped. Thank you

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void fillArray(int *arr, int n, int min, int max);
void printArray(int *arr, int n);
int main(int argc, char *argv[]){
   if(argc < 3){
       cout << "Incorrect number of arguments" << endl;
       cout << "Usage: " << argv[0] << " min max" << endl;
       return 0;
   }
   srand(time(0));
   int min = atoi(argv[1]);
   int max = atoi(argv[2]);
   if(max < min) //to swap?
   {
       int temp = min;
       min = max;
       max = temp;
   }
  
   int *arr = new int[10];
   cout << "Creating an array of size 10" << endl;
   fillArray(arr, 10, min, max);
   printArray(arr, 10);
  
   delete []arr;//deallocate memory
   cout << "\nCreating an array of size 50" << endl;
   fillArray(arr, 50, min, max);
   printArray(arr, 50);
   return 0;
}

void fillArray(int *arr, int n, int min, int max){
   for(int i = 0; i < n; i++)
       arr[i] = min + rand() % (max - min + 1);
}
void printArray(int *arr, int n){
   for(int i = 0; i < n; i++){
       if(i % 10 == 0) // pring newline for every 10 numbers
           cout << endl;
       cout << arr[i] << " ";
   }
   cout << endl;
}

Add a comment
Know the answer?
Add Answer to:
The main(int argc, char *argv[]) function will perform the following: • Evaluate whether value in argc...
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
  • implement the following function: void BuildString(int argc, char* argv[]); 1. Arguments passed through the function. Concatenate...

    implement the following function: void BuildString(int argc, char* argv[]); 1. Arguments passed through the function. Concatenate the strings arguments using strcat, display the concatenated string. 2.. If only one argument passed, display the string. 3. If no argument passed, display the message, no argument passed. In main do the following: 1. Call the function BuildString, passing the proper arguments (argc and argv).

  • #include <stdlib.h> #include <stdio.h> #include "main.h" #define MAX_NUM_LENGTH 11 void usage(int argc, char** argv) { if(argc...

    #include <stdlib.h> #include <stdio.h> #include "main.h" #define MAX_NUM_LENGTH 11 void usage(int argc, char** argv) { if(argc < 4) { fprintf(stderr, "usage: %s <input file 1> <input file 2> <output file>\n", argv[0]); exit(EXIT_FAILURE); } } /* This function takes in the two input file names (stored in argv) and determines the number of integers in each file. If the two files both have N integers, return N, otherwise return -1. If one or both of the files do not exist, it...

  • #include <stdio.h> int main(int argc, char *argv[]) {      char a, *pc, c[9];      int i, *pk, k[9];      a='...

    #include <stdio.h> int main(int argc, char *argv[]) {      char a, *pc, c[9];      int i, *pk, k[9];      a='z';      pc=&(c[8]);      pk=&(k[0]);      for (i=0; i<9; i++)     {           *pc=a-(char)i;           pc--;           *pk=(int)a-i;           pk++;           }                     return 0; }//end of main Answer the below questions with the above code Write out the memory map for the above C code in the following table. For array variables, list the address range for the entire array. Assume the memory address starts from 100, that is, the address for a...

  • C++. i have a function bool get(int argc, char* argv[], option& opt) ./a.out -a123 -b 56s...

    C++. i have a function bool get(int argc, char* argv[], option& opt) ./a.out -a123 -b 56s -call -0 -uabs these are the arguments i want to erase the first character '-' and store the output in the option and value string variables. output will be: option: = a value : = 123 option: = b value: = 56s option: = c value := all option : = 0 option: = u value : = abs

  • #include <stdio.h> .. int main(int argc, char *argv[]) { int base, power; printf("enter base and power:...

    #include <stdio.h> .. int main(int argc, char *argv[]) { int base, power; printf("enter base and power: "); scanf("%d %d", &base, &power); while (base != -100){ double res = pow(base, power); double res2 = my_pow(base, power); printf("pow: %.4f\n", res); printf("my_pow: %.4f\n", res2); .... } return 0; } // this function should be RECURSIVE // should not use any loop here double my_pow(double base, double p) { } lab4pow.c file contains: 2.1 Specification Write an ANSI-C program that reads input from the...

  • C Programming write two functions, similar to what you see in the sample program. The first will ask the user to enter some information (I have included the type in parentheses) First Name (char[]) L...

    C Programming write two functions, similar to what you see in the sample program. The first will ask the user to enter some information (I have included the type in parentheses) First Name (char[]) Last Name (char[]) Age (int) Height in Inches (double) Weight in Pounds (double) You will use pass-by-reference to modify the values of the arguments passed in from the main(). Remember that arrays require no special notation, as they are passed by reference automatically, but the other...

  • Learn the example C program in Folio consisting of three files (a.c, a.h, main.c) and complete this exercise. You need t...

    Learn the example C program in Folio consisting of three files (a.c, a.h, main.c) and complete this exercise. You need to understand function, pointer and the selection sort algorithm. Write a C program that consists of three files mysort.h, mysort.c and myMain.c. Below is the mysort.h prototype #include <stdlib.h> #include <stdio.h> void generateNums(int *myarr, int len); void sortNums(int *myarr, int len); mysort.h must contain only the function declarations (prototypes) listed above generateNums function should generate len random integers in the...

  • C++ Program Int Main First Please Write one program that does the following: 1.       1.   Ask the...

    C++ Program Int Main First Please Write one program that does the following: 1.       1.   Ask the user for ten (10) grades, and store the data in an array.  Compute the average of all the grades.  Print the original ten grades and the average. a.       Declare an integer array with the name of “grades” of size 10 in the main function. b.      Create a function called “getGrades” that prompts the User for the grades and puts them in an integer array.                                                                i.      The function will receive...

  • I keep getting an error code in my C++ program. It says "[Error] 'strlen' was not...

    I keep getting an error code in my C++ program. It says "[Error] 'strlen' was not declared in this scope" in my main.cpp. Here are my codes. main.cpp #include <iostream> #include <string> #include "functions.h" using namespace std; //definition of the main function. //takes arguments from the command-line. int main(int argc, char *argv[]) { //Determine if you have enough arguments. //If not, output a usage message and exit program if (argc<2 || (argc == 2 && argv[1][0] == '-')) { //call...

  • C++ program Correctly complete the following assignment. Follow all directions. The main purpose is to show...

    C++ program Correctly complete the following assignment. Follow all directions. The main purpose is to show super and sub class relationships with an array of super media pointers to sub class objects and dynamic binding. The < operator will be overloaded in the super class so all subclasses can use it. The selection sort method will be in the main .cpp file because it will sort the array created in main. The final .cpp file, the three .h header class...

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