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

Use C++ to solve this set(num1Value,num1Index ; num2Value,num2Index ;... numXVal

ID: 3667048 • Letter: U

Question

Use C++ to solve this

set(num1Value,num1Index;num2Value,num2Index;...numXValue,numXIndex) : Sets the current array. The array consists of simple structures that have TWO integer values within them, a value and an index. Notice that one “item” is declared with two numbers that have a comma between them. Then the items are separated by semicolons. You will sort based on the value field alone but individual structures must remain intact as you sort. In other words if one item is {57,2} then you should never be splitting those two values apart. You move the whole structure as one entity as you sort, even though '57' is the only value you look at when comparing items. All subsequent commands that you process will operate on this data. Note that this line may be VERY long in the input file. Set can be called at any time to change to a new data set. Do NOT display anything in response to this command, just update the current array.

Note that there is not a semicolon after the very last parameter.

All numbers within structures will be valid integer values.

This is reiterated in the following command descriptions, but when you are going to sort

this array you first make a copy of it, leaving the original data intact.

You may want to use your MyString class from a previous homework assignment to

assist with parsing of this command.

• Below is an example of what the structure declaration for an item in your array might look like:

struct Item {

int Value;

int Index; };

insertion : Sorts the current array using insertion sort and displays the results ON ONE LINE. Duplicate the array before sorting it, so that the original remains the same. In other words duplicate current newArr and sort newArr. See information in the notes section about how you need to display the sorted array contents.

Explanation / Answer

#include<iostream>

using namespace std;

struct Item {
int value;
int index;
};

int main(){
  
   int length = 3;
   Item a[length];
   Item data[length];
   int i, j, k, temp;
   int index,value;
cout<<"enter the elements ";
for (i = 0; i <length; i++)
{
       cout<<"Enter index for "<<(i+1)<<" : ";
       cin>>index;
       cout<<"Enter value for "<<(i+1)<<" : ";
       cin>>value;
data[i]={index,value};
}
for (i = 0; i <length; i++)
{
a[i]=data[i];
}
  
for (i = 1; i < length; i++)
{
for (j = i; j >= 1; j--)
{
if (a[j].value < a[j-1].value)
{
temp = a[j].value;
a[j].value = a[j-1].value;
a[j-1].value = temp;
}
else
break;
}
}
cout<<"sorted array "<<endl;
for (k = 0; k <length; k++)
{
   cout<<a[k].value<<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