In: Computer Science
Write a C++ program based on the cpp file below
++++++++++++++++++++++++++++++++++++
#include
using namespace std;
// PLEASE PUT YOUR FUNCTIONS BELOW THIS LINE
// END OF FUNCTIONS
void printArray(int array[], int count)
{
cout << endl << "--------------------"
<< endl;
for(int i=1; i<=count; i++)
{
if(i % 3 == 0)
cout << " " <<
array[i-1] << endl;
else
cout << " " <<
array[i-1];
}
cout << endl << "--------------------"
<< endl;
}
int main()
{
int index;
// first test array (uncomment to use)
int array[200] = {400, -45, -300, 50, -300, 90};
int count = 6;
// second test array
//int array[200] = {900, 34, 233, 90};
//int count = 4;
// third test array
//int array[200] = {10, 140, 32, -3, 12, -3, 21, 12};
//int count = 8;
cout << "BEFORE" << endl;
printArray(array, count);
index = findLastMin(array, count);
cout << "Index found at: " << index
<< endl;
deleteMin(array, count);
cout << "AFTER" << endl;
printArray(array, count);
return 0;
}
++++++++++++++++++++++++++++++++++++
INSTRUCTION
You will implement a function that will involve
finding the last instance of the minimum in an array
of integers and returning the index of this item.
The array will be of integers that can be both positive
and negative. Also, there may be repeated values in the
array.
EXAMPLES
==========
Array = 400, -45, -300, 50, -300, 90
index is: 4
Array = 900, 34, 233, 90
index is: 1
=====================================
Part 2
Find the last instance of the minimum in the
given array and delete it from the array.
You can use the part II find function to get an index.
Don't change the order of the other elements in the array.
Upload the midterm6.cpp file to the d2l assignment
folder before the due time.
EXAMPLES
==========
BEFORE
Array = 400, -45, -300, 50, -300, 90
AFTER
Array = 400, -45, -300, 50, 90
Program1:Write a program to find the index of the last minimum element in tha array
In the program we need to find the index of the last minimum element in the array.
Step-1:Declare a variable "min" and assign it to the maximum value of the integer datatype with the help of <climits> header file.
Step-2:Start iterating the array from the starting.
Step-3:Compare the first element of the array with the min variable,if the value of first element of the array is less than min value then update the min value to the array[0]
Step-4:Use another variable "location" to store the index of the minimum element.
Step-5:Repeat step-3 for all the elements in the array and update the min and location value accordingly.
Step-6:If there are repeated values consider the last value as minimum value if it is the mimimum element.
Step-7:Print the array with help of "printArray" function
Step-8:Print the location value and it is the index of the lastminimum element in the array.
Program
#include <iostream>
#include<climits>
using namespace std;
void printArray(int array[],int count)
{
cout<<"Array = ";
for(int i=0;i<count;i++)
{
cout<<array[i]<<"
";
}
cout<<endl;
}
int minindex(int arr[],int count)
{
int min=INT_MAX;
int location;
for(int i=0;i<count;i++)
{
if(arr[i]<=min)
{
min=arr[i];
location=i;
}
}
return location;
}
int main()
{
int count=6,count1= 4,count2=8;
int array[200] = {400, -45, -300, 50, -300, 90};
int array1[200] = {900, 34, 233, 90};
int array2[200]={10, 140, 32, -3, 12, -3, 21, 12};
printArray(array,count);
cout<<"index
is:"<<minindex(array,count)<<endl;
printArray(array1,count1);
cout<<"index
is:"<<minindex(array1,count1)<<endl;
printArray(array2,count2);
cout<<"index
is:"<<minindex(array2,count2)<<endl;
return 0;
}
output:
Program2:Find the last instance of the minimum in the given array and delete it from the array.
In this program we need to find the index of the minimum element and delete that element.
Step-1:Declare a variable "min" and assign it to the maximum value of the integer datatype with the help of <climits> header file.
Step-2:Start iterating the array from the starting.
Step-3:Compare the first element of the array with the min variable,if the value of first element of the array is less than min value then update the min value to the array[0]
Step-4:Use another variable "location" to store the index of the minimum element.
Step-5:Repeat step-3 for all the elements in the array and update the min and location value accordingly.
Step-6:If there are repeated values consider the last value as minimum value if it is the mimimum element.
Step-7:After finding the index of the minimum element,assign the value at the minimum element to the variable "x"
Step-8:Check whether the location of minimum element is less than the size of the array.
Step-9:If it is less than the array size,decrement the size of the array by one.
Step-10:Start iterating from the location of the min element to the end of the array by updating the array values.
Step-11:print the updated array with the help of "printArray" function.
Program
#include <iostream>
#include<climits>
using namespace std;
void printArray(int array[],int count)
{
cout<<"Array = ";
for(int i=0;i<count;i++)
{
cout<<array[i]<<"
";
}
cout<<endl;
}
int deletemin(int array[],int count)
{
int min=INT_MAX,location,x;
for(int i=0;i<count;i++)
{
if(array[i]<=min)
{
min=array[i];
location=i;
}
}
x=array[location];
if (location < count)
{
count = count - 1;
for (int j=location; j<count; j++)
array[j] = array[j+1];
}
return count;
}
int main()
{
int count=6,count1= 4,count2=8,n;
int array[200] = {400, -45, -300, 50, -300, 90};
int array1[200] = {900, 34, 233, 90};
int array2[200]={10, 140, 32, -3, 12, -3, 21, 12};
cout<<"BEFORE"<<endl;
printArray(array,count);
n=deletemin(array,count);
cout<<"AFTER"<<endl;
printArray(array,n);
cout << endl << "--------------------" <<
endl;
cout<<"BEFORE"<<endl;
printArray(array1,count1);
n=deletemin(array1,count1);
cout<<"AFTER"<<endl;
printArray(array1,n);
cout << endl << "--------------------" <<
endl;
cout<<"BEFORE"<<endl;
printArray(array2,count2);
n=deletemin(array2,count2);
cout<<"AFTER"<<endl;
printArray(array2,n);
return 0;
}
output