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

Write a c++ program that contains the following function a) A functions that tak

ID: 3720955 • Letter: W

Question

Write a c++ program that contains the following function

a) A functions that takes in two arrays ( both can be of any size), and fills up the first array with input from a file and the second with input from the user.

b) A function that takes in two integer variables and swaps the content of the variables

c) A function that takes in an unsorted array of any size and an integer key, and returns true or false if the key is in the array using Binary Search. (You can use code to make any adjustment needed to use binary search)

d) A function that takes in an unsorted array of any size and uses bubble sort to arrange in descending order (from largest to smallest).

e) A function that takes in an array of any size and prints the content of the array in one line.

f) A function that takes in an array of any size and an integer key. The function should find every occurrence of the key and replace it with 9999 and shift to the end of the list.

ex. 34 11 92 78 62 6 55. With 11 as the key, the new list should look like 34, 92, 78, 62, 6, 55, 999. (Note: This is not a swap, but a left shift for elements after the key, use the idea of bubble sort or insertion sort)


Please use c++ code. Nothing too advanced

Explanation / Answer

//cpp program to handling functions

//including header files
#include<iostream>
#include <fstream>
#include <sstream>


using namespace std;
//swapfun() function swaps values of two variables
void swapfun(int &n1,int &n2)
{
    int tmp;
    tmp=n1;
    n1=n2;
    n2=tmp;
}
//len() returns length of an array
int len(int ary[])
{
    int i=0;
    while(ary[i])
        i++;
    ary[i]='';
    return i;
}
//print() method displays values in the array
void print(int ary[])
{
    int i,sz=len(ary);
    for(i=0;i<sz;i++)
        cout<<ary[i]<<" ";
}
//sorts array in descending order in this method
void sorting(int ary1[])
{
    int i,j,n=len(ary1);
    for(i=0;i<n;i++)
        for(j=i+1;j<n;j++)
            if(ary1[i]<ary1[j])
                swapfun(ary1[i],ary1[j]);


}
//fun1 takes file data into array1 and user data into array2
void fun1(int ary1[],int ary2[])
{
ifstream inpfile;
string line;
inpfile.open("inp.txt");
int i=0,n;
if(inpfile.is_open())
while(getline(inpfile,line))
{
     cout<<line;
     stringstream val(line);
     val>>ary1[i];
     i++;
}
cout<<"how many values want to enter into array 2 : ";
cin>>n;
cout<<"Enter "<<n<<" values"<<endl;
for(i=0;i<n;i++)
{
     cin>>ary2[i];
}
cout<<"displaying values of array1 from file :"<<endl;
print(ary1);
cout<<" displaying values of array2 from user :"<<endl;
print(ary2);
}
//returns true if value found in the array through binary search
bool binsearch(int ary[],int key)
{
    int sz=len(ary);
    int low=0,mx=sz,mid=(low+sz)/2;
    sorting(ary);
    while(low<mx)
    {

        if(ary[mid]==key)
        return true;
    else if (ary[mid]<key)
        {
            low=mid;
            mid=(low+sz)/2;
        }
        else
        {
            mx=mid;
            mid=(low+sz)/2;
        }
    }
    return false;
}
//shifting right side to last in an array
void shift(int ary[],int pos)
{
    int i,sh=ary[pos],sz=len(ary);
    for(i=pos;i<sz-1;i++)
    {
        ary[i]=ary[i+1];
    }
    ary[i]=sh;
}
//replacing key value with 9999 then shifting..
void replaceshift(int ary[],int key)
{
    int i,sz=len(ary),sh=9999;
    for(i=0;i<sz;i++)
    {
        if(ary[i]==key)
        {
            ary[i]=sh;
            shift(ary,i);
        }

    }

}
//main method to call methods to test and display results
int main()
{
int ary1[100],ary2[100];
int key,n1=34,n2=56,i,j;
cout<<"start of program:"<<endl;
fun1(ary1,ary2);
cout<<"Before swapping n1="<<n1<<" n2="<<n2;
swapfun(n1,n2);
cout<<"After swapping n1="<<n1<<" n2="<<n2;
sorting(ary1);
cout<<"sorted array1 :"<<endl;

print(ary1);
cout<<"search of 55 in ary2 "<<binsearch(ary2,55);
replaceshift(ary2,22);
print(ary2);
return 0;
}//end of main


//input file inp.txt

//output screenshot is shown below

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