Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please use the same function prototypes as I have, including the same function n

ID: 3744852 • Letter: P

Question

Please use the same function prototypes as I have, including the same function name. You can just copy the following 7 prototypes directly into the prototype section of your program (before main):

bool die( const string & msg );

void input( unsigned a[], unsigned elements );

void output( const unsigned a[], unsigned elements );

unsigned howMany( const unsigned a[], unsigned elements, unsigned min, unsigned max );

bool allSame( const unsigned a[], unsigned elements );

bool allDifferent( const unsigned a[], unsigned elements );

unsigned changes( const unsigned a[], unsigned elements );

Don’t declare any global variables

bool die( const string & msg );

die’s job is to output an alarming message that includes the text of msg, and then terminate the program without returning to its caller by calling exit. You may write your die exactly as I wrote mine, or you may write yours differently:

              bool die( const string & msg ){

                             cout <<"Fatal error: " <<msg <<endl;

                             exit( EXIT_FAILURE );

              }

void input( unsigned a[], unsigned elements );

input’s job is, for each element of a, prompt the user with the element’s subscript, and input an unsigned value into that array element. If the input of any element fails, input should complain and die (by calling your die function).

An example run of your program (with the user input in bold), where elements == 3, might go as:

              [0]: 47

              [1]: 1234

              [2]: 88

In this example, the array had 3 elements, and the input function, based on the numbers the user typed, set the array to {47, 1234, 88}.

void output( const unsigned a[], unsigned elements );

output’s job is to output the elements of a, separated by commas. No input. Don’t output a comma before the first element or after the last element.

For instance, if the array passed to your output function were {47, 1234, 88}, then your output function would output

              47, 1234, 88

unsigned howMany( const unsigned a[], unsigned elements, unsigned min, unsigned max );

No I/O (Input/Output). howMany just returns the number of elements in a that are in the range [min, max] — that is, the number of elements that are >= min and <= max.

For example, if howMany were passed ({8, 9, 8, 7, 5, 3, 123456789, 4}, 8, 4, 8 ), then it would return 5.

For another example, if howMany were passed ({1,2,3,4,5}, 5, 3, 2), then it would return 0 (there’s no way a number can be both >= 3 and <= 2, so no number qualifies).

bool allSame( const unsigned a[], unsigned elements );

We return true iff (if and only if) all the elements in the array are the same. Let’s say that if the array has 0 elements, then all the elements are the same.

For instance, allSame( {8, 8, 8, 8, 8}, 5 ) would true

For instance, allSame( {8, 8, 9, 8, 8}, 5 ) would return false;

bool allDifferent( const unsigned a[], unsigned elements );

We return true iff all the elements in the array are different from every other element. Let’s say that if the array has 0 elements, then all the elements are different.

For instance, allDifferent( {1, 5, 3, 5, 2}, 5 ) would false

For instance, allDifferent( {1, 5, 3, 80000, 2}, 5 ) would return true;

unsigned changes( const unsigned a[], unsigned elements );

changes’s job is to go through the array from beginning to end, counting how many times the data changes its direction between increasing and decreasing. For instance, if the data is increasing, then decreasing, then increasing, then decreasing, changes will return 3. That is,

              changes( {3, 5, 10, 9, 50, 100, 1000, 14}, 8 )

returns 3 because

              3 5 10 is increasing

              10 9 is decreasing

              9 50 100 1000 is increasing

              1000 14 is decreasing

When the data is the same from one element to the next, that doesn’t count as a change in direction.

              changes( {9, 9, 9, 9, 5, 5, 5, 5, 3, 4, 7, 7, 7, 10, 10, 11, 11, 8, 8, 8}, 20 )

returns 2 because the sequence changes direction at the 3 (from decreasing to increasing), and then changes direction at the 11’s (from increasing to decreasing).

Explanation / Answer

bool die( const string & msg ){

cout <<"Fatal error: " <<msg <<endl;

exit( EXIT_FAILURE );

}

void input(unsigned a[],unsigned elements)
{
for(unsigned i=0;i<elements;i++)
{ cin>>x;
if(x<0){ //unsigned values should be greater than 0.
cout <<"Enter a valid input" <<endl;
die("invalid input");
}
else{
a[i]=x;

}
}
}

void output( const unsigned a[], unsigned elements ){
  
for(unsigned i=0;i<elements-1;i++) //outputing upto elements-1 size as after last element there will
cout<<cout<<a[i]<<","; //be no comma
  
  
cout<<a[elements-1];
}


unsigned howMany( const unsigned a[], unsigned elements, unsigned min, unsigned max ){
unsigned count=0;
for(unsigned i=0;i<elements;i++)  
{
if((a[i]-min>0||a[i]-min==0)&& (max>a[i]||max==a[i]))//for elements to be in any range
{ //min<=elements<=max should hold
count++;
}
}
return count;
}

bool allSame( const unsigned a[], unsigned elements ){
  
set<unsigned> oneset;
for(unsigned i=0;i<elements<i++){//set stores elements uniquely,means a multiple occurence of any  
oneset.insert(a[i]); // element is stored only once
}
  
if(oneset.size()==1)//if size of set is maximum i.e == 1,then all elements are same
return true;
else
return false;
}


bool allDifferent( const unsigned a[], unsigned elements ){
set<unsigned> oneset;
for(unsigned i=0;i<elements<i++){
oneset.insert(a[i]);//set stores elements uniquely,means a multiple occurence of any elemet is
} //only once stored
  
if(oneset.size()==elements) //if size of set is maximum i.e == elements ,then all elements are different
return true;
else
return false;
  
  
}

unsigned changes( const unsigned a[], unsigned elements ){
  
unsigned count=0;
for(unsigned i=1;i<elements-1<i++){ //if the array is increasing or decreasing there will be alrernate if((a[i]-a[i-1]>0 && a[i+1]-a[i]<0)||(a[i]-a[i-1]<0 && a[i+1]-a[i]>0)){//changeof signs while traversing count++; //an array
  
// k=a[i];
}
}
return count;
  
}