Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1) Write a program that implements a recursive isSorted function. The recursive isSorted function should return true if the first n elements of the array

1) Write a program that implements a recursive isSorted function. The recursive isSorted function should return true if the first n elements of the array are sorted, and false otherwise. You should not need to change any code in main for your function to operate. You should also not use any global variables.

Here is a sample call:

int test1[] = {1,2,3};

int test2[] = {2,1,3};

cout << isSorted(test1,3) << endl; // Outputs 1 for true

cout << isSorted(test2,3) << endl; // Outputs 0 for false

2) In C++ we can specify the following for main:

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

These parameters are used when the program is invoked from the command line. The parameter argc tells us how many arguments are used in invoking the program. This includes the program name itself. For example if we run the program using:

./a.out

Then argc would equal 1 because the only parameter is the program name of ./a.out. If we run the program using:

./a.out 45 10

Then argc would equal 3 because there are three parameters (./a.out, 45, and 10). The argv parameter is an array of char pointers (i.e. cstrings). argv[0] points to a cstring that represents the first argument (e.g. ./a.out), argv[1] points to a cstring that represents the second argument (e.g. 45) and so on. Here is an example program that outputs all of the command line arguments. We haven't completely covered pointers yet, but you can think of this as basically an array of strings. You will probably want to run this on the unix machine or some other place where it is easy to supply command line arguments

#include

#include

using namespace std;

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

for (int i = 0; i < argc; i++) {

cout << argv[i] << endl; }

return 0;

}

Modify this program so that it multiplies all of the numbers sent in on the command line. It should take an arbitrary number of values. You can assume they are all integers. For example, here is a sample run:

user@trasformer:~/CSCE_A412$ ./a.out 2 4 6

To convert a cstring into an integer, you can use the atoi function. For example, atoi("20") returns back the integer 20.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Object Databases The Essentials

Authors: Mary E. S. Loomis

1st Edition

020156341X, 978-0201563412

Students also viewed these Databases questions

Question

Solve ex xe 5x-1 = 0.

Answered: 1 week ago

Question

=+(f2-tf1) du 0, since the integrand is nonnegative.

Answered: 1 week ago