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

Write a program that uses four functions. The first function should load an arra

ID: 3686286 • Letter: W

Question

Write a program that uses four functions. The first function should load an array of 100 elements using a random number generator. You can use a range of your choosing. Understanding that the smaller the range the more repetition you will have in the numbers generated. The second function should sort the array from lowest the highest value. The third function should search the array for a specific number entered by the user. The third function should output the number of times that the input number was found and what was the array location of each instance of the number. The third function should also have an output statement that the number did not exist in the array if that is what happens. The last function should display the array both before and after it is sorted. Use system("pause"); commands after each function so that the user can easily see the results.

Explanation / Answer

#include<iostream>
#include <time.h>
#include<stdlib.h>
using namespace std;
int size=10;
void setInput(int n[])
{
    srand (time(NULL));
    for(int i=0;i<size;i++)
    {
    n[i]= rand() % 40 + 1;
    }
}
void sortArray(int n[])
{
    int temp;
    for(int i=0;i<size;i++)
    {
        for(int j=0;j<size;j++)
        {
            if(n[i]<n[j])
            {
                temp=n[i];
                n[i]=n[j];
                n[j]=temp;
            }
        }
    }
}
void searchArray(int n[],int element)
{
    int cnt=0,rep[size];
    for(int i=0;i<size;i++)
    {
    if(n[i]==element)
    {
    rep[cnt]=i;
    cnt++;
    }
    }
    if(cnt==0)
    cout<<endl<<"Sorry, no such elemtn found"<<endl;
    else
    {
        cout<<endl;
        cout<<"Element is repeated for "<<cnt<<"times"<<endl;
        cout<<"Element found at following indexes: ";
        for(int j=0;j<cnt;j++)
        cout<<rep[j]<<" ";
    }
}
void print(int n[])
{
cout<<"Array before sorting"<<endl;
for(int i=0;i<size;i++)
cout<<n[i]<<" ";
sortArray(n);
cout<<endl;
cout<<"After sorting"<<endl;
for(int i=0;i<size;i++)
cout<<n[i]<<" ";
}  
int main()
{
    int n[size];
    setInput(n);
    print(n);
    searchArray(n,21);
   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