Question

In: Computer Science

Write a C++ program based on the cpp file below ++++++++++++++++++++++++++++++++++++ #include using namespace std; //...

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

Solutions

Expert Solution

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


Related Solutions

Writing a squareroot program in C++ using only: #include <iostream> using namespace std; The program must...
Writing a squareroot program in C++ using only: #include <iostream> using namespace std; The program must be very basic. Please don't use math sqrt. Assume that the user does not input anything less than 0. For example: the integer square root of 16 is 4 because 4 squared is 16. The integer square root of 18 is 5 because 4 squared is 16 and 5 squared is 25, so 18 is bigger than 16 but less than 25.  
Writing an nth root program in C++ using only: #include using namespace std; The program must...
Writing an nth root program in C++ using only: #include using namespace std; The program must be very basic. Please don't use math sqrt or pow . For example, the 4th root of 16 is 2 because 2 * 2 * 2 * 2 = 16. The 4th root of 20 is 2 because 2 * 2 * 2 * 2 = 16 and 16 is less than 20, and 3 * 3 * 3 * 3 = 81, which...
Please write variables and program plan(pseudocode) of this C++ programming code: #include <iostream> using namespace std;...
Please write variables and program plan(pseudocode) of this C++ programming code: #include <iostream> using namespace std; void leapYear(int x); int main() { int x; cout << "Enter a year: "; cin >> x; leapYear (x);   return 0; } void leapYear(int x ) {    if (x % 400 == 0)    {    cout << "This is a leap Year";}    else if    ((x % 4 == 0) && (x % 100 != 0))    {    cout <<...
Take the following C++ program and translate it into Pep/9 assembly language #include using namespace std;...
Take the following C++ program and translate it into Pep/9 assembly language #include using namespace std; int age; char first, last; int main() {    cin >> age;    cin >> first >> last;    cout << "Your age " << age << endl;    cout << "Initials " << first << last << endl;    if (age >= 30)        cout << “Cannot trust\n”;    return 0; }
Question 1 Given the program below, what are the errors. #include <iostream> using namespace std; int...
Question 1 Given the program below, what are the errors. #include <iostream> using namespace std; int main() { int ind_square(int &); int x, *p; x=15; p = &x; ind_square(*p); } int ind_square(int &p) { *p = *p **p; } Question 1 options: C. No return for non-void function A. Indirection for the variables in ind_square requires pointer operand A and B B. Invalided use of address (&) symbol as a parameter for ind_squared A and C Question 2 Which statement...
Using Virtualbox in Debian, write a simple program (a single .cpp file) in Linux shell C++...
Using Virtualbox in Debian, write a simple program (a single .cpp file) in Linux shell C++ Rules: -Use fork(), exec(), wait(), and exit() _______________________________________________________________________________________________________________________________________________ -A line of input represents a token group. -Each token group will result in the shell forking a new process and then executing the process. e.g. cat –n myfile.txt // a token group -Every token group must begin with a word that is called the command(see example above). The words immediately following a command are calledarguments(e.g....
Using Virtualbox in Debian, write a simple program (a single .cpp file) in Linux shell C++...
Using Virtualbox in Debian, write a simple program (a single .cpp file) in Linux shell C++ Rules: -Use fork(), exec(), wait(), and exit() _______________________________________________________________________________________________________________________________________________ -A line of input represents a token group. -Each token group will result in the shell forking a new process and then executing the process. e.g. cat –n myfile.txt // a token group -Every token group must begin with a word that is called the command(see example above). The words immediately following a command are calledarguments(e.g....
I want to indent this c++ program #include<iostream> using namespace std; class Rectangle{ private: double width;...
I want to indent this c++ program #include<iostream> using namespace std; class Rectangle{ private: double width; double length; public: void setWidth(double); void setLength(double); double getWidth() const; double getLength() const; double getArea() const; double getPerimeter() const; bool isSquare() const; }; void Rectangle::setWidth(double w){ width = w; } void Rectangle::setLength(double l){ length = l; } double Rectangle::getWidth() const{ return width; } double Rectangle::getLength() const{ return length; } // Added Method definitions double Rectangle::getArea() const{ return (width * length); } double Rectangle::getPerimeter() const{...
Complete the C++ code #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; struct Cell {...
Complete the C++ code #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; struct Cell { int val; Cell *next; }; int main() { int MAX = 10; Cell *c = NULL; Cell *HEAD = NULL; srand (time(NULL)); for (int i=0; i<MAX; i++) { // Use dynamic memory allocation to create a new Cell then initialize the // cell value (val) to rand(). Set the next pointer to the HEAD and // then update HEAD. } print_cells(HEAD); }
write the algorithm for this the code?!. #include<iostream> using namespace std; #include<string.h> int main() { char...
write the algorithm for this the code?!. #include<iostream> using namespace std; #include<string.h> int main() { char plain[50], cipher[50]="", decrypt[50]=""; int subkeys[50], len;       cout<<"Enter the plain text:"<<endl; cin>>plain;    cout<<"Enter the first subkey:"<<endl; cin>>subkeys[0];    _strupr(plain);    len = strlen(plain);    /**********Find the subkeys**************/    for(int i=1; i<len; i++) { if ((plain[i-1]>='A') && (plain[i-1]<='Z')) { subkeys[i] = plain[i-1]-65; } }    /****************ENCRYPTION***************/       for(int i=0; i<len; i++) { if ((plain[i]>='A') && (plain[i]<='Z')) {    cipher[i] = (((plain[i]-65)+subkeys[i])%26)+65; }...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT