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

1. Write a function that takes an integer array and the array\'s size as paramet

ID: 3556184 • Letter: 1

Question

1. Write a function that takes an integer array and the array's size as parameters. The function will check if the array is sorted. If it is, it will return a 1. If it is not sorted it will return 0. The function should not sort the array.

2. Write a functions that takes a string as its only parameter.It then sorts the charcters in the strings in alphabetical order. Since a string is passed by reference there is no need to retun the string. Note: a string is simply an array of characters with an additional element at the end. Thus if the string has ten letters the size of the character array is eleven. The first letter is still index 0 and so on.

Explanation / Answer

// 1. Write a function that takes an integer array and the array's size as parameters.
// The function will check if the array is sorted. If it is, it will return a 1. If it is not sorted it will return 0.
// The function should not sort the array.
#include<iostream>
using namespace std;

int is_sorted(int array[],int size)
{
     bool is_sorted_ascend= true;
   bool is_sorted_descend= true;
   for(int i=0; i<size-1; i++)
   {
       if(array[i] > array[i+1])
       {
       is_sorted_ascend= false;
       break;
       }
   }
   for(int i=0; i<size-1; i++)
   {
       if(array[i] < array[i+1])
       {
       is_sorted_descend= false;
       break;
       }
   }
   return static_cast<int> (is_sorted_ascend || is_sorted_descend);
}


int main()
{
int array[] = {1,2,2,4,5,6};
cout << is_sorted(array,6) << endl;
int array2[] = {11,2,-2,-4,-5,-6};
cout << is_sorted(array2,6) << endl;
int array3[] = {1,2,2,4,5,6,3,4};
cout << is_sorted(array3,8) << endl;
return 0;
}

// 2. Write a functions that takes a string as its only parameter.
// It then sorts the charcters in the strings in alphabetical order.
// Since a string is passed by reference there is no need to retun the string.
// Note: a string is simply an array of characters with an additional element at the end.
// Thus if the string has ten letters the size of the character array is eleven. The first letter is still index 0 and so on.

#include<iostream>
using namespace std;
void sort_string(string& str)
{
     for(int i=0; i<=str.length()-2; i++)
   {
       int pos = i;
       for(int j=i; j<=str.length()-1; j++)
       {
           if(str[j] < str[pos])
           pos = j;
       }
       if(pos != i)
        {
       char temp = str[pos];
       str[pos] = str[i];
       str[i] = temp;
        }
   }
}

int main()
{
string str = "asdfghjkl";
cout << "Before sort string is " << str << endl;
sort_string(str);
cout << "After sort string is " << str << endl;
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote