Question
c++ code

Lab Exercises This assignment consists of two exercises. Exercise 1- Count Inversions Exercise Objectives Define and call fun
0 0
Add a comment Improve this question Transcribed image text
Answer #1

C++ PROGRAM:

#include<iostream>

using namespace std;

// Define a constant value called A_SIZE with value of 10.

#define A_SIZE 10

/** Define a functions

* (1) Define a function called Read that accept an integers array and its size.

* this function ask the user to enter 10 integers to initialize Arr.*/

void Read(int Arr[] , int size){

    // using input/output statement

    cout<<"Enter "<<size<<" integers :"<<endl;

    for(int i=0; i<size; i++){

        cin>>Arr[i];

    }

}

/**Define a function called Inversions that counts

* and prints the Inversions in Arr. Two elements

* a[i] and a[j] form an inversion if a[i]>a[j] and i<j*/

void Inversions(int Arr[]){

    int count = 0;

    cout<<"******************************"<<endl;

    // using nested loop

    // nested for loop for index i and j

    for(int i=0; i<A_SIZE-1; i++){

        for(int j=1; j<A_SIZE; j++){

           // if a[i]>a[j] and i<j

            if(Arr[i]>Arr[j] && i<j){

                // print the pairs

                cout<<"("<<Arr[i]<<" , "<<Arr[j]<<")-";

                count++;// count the inversions

            }

        }

    }

    // print the total number of inversions

    cout<<"\n******************************"<<endl;

    cout<<"Number of inversions : "<<count;

}

// main() function

int main()

{

    // using Array

    // Declare an integer 1D array of size A_SIZE called Arr.

    int Arr[A_SIZE];

    // calling function Read()

    Read(Arr, A_SIZE);

    // calling function Inversions()

    Inversions(Arr);

    return 0;

}

OUTPUT:

Enter 10 integers : 1 2 3 5 4 1 8 6 4 5 (2, 1)-(3, 1)-(5, 4)-(5, 1)-(5, 4)-(4 , 1)-(8 , 6)-(8 , 4)-(8 , 5)-(6 , 4)-(6 , 5)- NEnter 5 integers : 2 4 1 3 5 (2, 1)-(4 , 1)-(4 , 3)- Number of inversions : 3

NOTE: If you don't understand any step please let me know at once.

Add a comment
Know the answer?
Add Answer to:
c++ code Lab Exercises This assignment consists of two exercises. Exercise 1- Count Inversions Exercise Objectives...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using C programming

    Using C, create a data file with the first number being an integer. The value of that integer will be the number of further integers which follow it in the file. Write the code to read the first number into the integer variable how_many.Please help me with the file :((This comes from this question:Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory allocation) and have it pointed to by a pointer (of type int...

  • Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory...

    Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory allocation) and have it pointed to by a pointer (of type int * ) named ptr_1. Use ptr_1 to assign the number 7 to that dynamically allocated integer, and in another line use printf to output the contents of that dynamically allocated integer variable. Write the code to dynamically allocate an integer array of length 5 using calloc or malloc and have it pointed...

  • Trying to do this in C++ Question 1: 1. Implement the function: int minInArray (int arr[],...

    Trying to do this in C++ Question 1: 1. Implement the function: int minInArray (int arr[], int arrSize) This function is given arr, an array of integers, and its logical size, arrSize. When called, t returns the minimum value in arr. Write a program that reads from the user a sequence of 20 integers (unnecessarily different from one another) into an array, and outputs the minimum value, and all the indices it appears in the array. 2. Your program should...

  • How can I convert the following C code to MIPS Assembly? +++++++++++++++++++++++++++++++++ MIPS main program ++++++++++++++++++++++++++++++++...

    How can I convert the following C code to MIPS Assembly? +++++++++++++++++++++++++++++++++ MIPS main program ++++++++++++++++++++++++++++++++ .data # Defines variable section of an assembly routine. array: .word x, x, x, x, x, x, x, x, x, x # Define a variable named array as a word (integer) array # with 10 unsorted integer numbers of your own. # After your program has run, the integers in this array # should be sorted. .text # Defines the start of the code...

  • 1. Write a C function named find that receives a one-dimensional array of type integer named...

    1. Write a C function named find that receives a one-dimensional array of type integer named arr and its size of type integer. The function fills the array with values that are the power of four (4^n) where n is the index. After that, the function must select a random index from the array and move the array elements around the element stored in the randomly selected index Example arr = [1, 4, 16, 64, 256) if the randomly selected...

  • Write a C function  f such that … function  f accepts, as input, … a two-dimensional array of...

    Write a C function  f such that … function  f accepts, as input, … a two-dimensional array of integers in which each row has three columns the number of rows in the array function  f returns the sum of the odd integers in the given array of integers and returns zero if there are no odd integers in the array COMPILER STACK TRACE None PROGRAM EXECUTION STACK TRACE None COMPILER COMMAND LINE ARGUMENTS -lm INPUT OF THE TEST CASE 1 #define NUM_ROWS 5...

  • daily21. Computing 1. C programming Please provide comments and write pre-condition and post-condition for the function....

    daily21. Computing 1. C programming Please provide comments and write pre-condition and post-condition for the function. Description Create a project called Daily21. Add a source file called daily21.c into the project. In this exercise, you will practice working with an array of integers. In the main function, you first need to create an integer array with 10 elements in t. Use a loop to read 10 integers from the user and store the values to the array (in the order...

  • In basic c develop the code below: (a) You will write a program that will do...

    In basic c develop the code below: (a) You will write a program that will do the following: prompt the user enter characters from the keyboard, you will read the characters until reading the letter ‘X’ You will compute statistics concerning the type of characters entered. In this lab we will use a while loop. We will read characters from stdin until we read the character ‘X’. Example input mJ0*5/]+x1@3qcxX The ‘X’ should not be included when computing the statistics...

  • JAVA Objectives: 1. Apply linear search algorithm 2. Apply select sort algorithm 3. Apply array iteration...

    JAVA Objectives: 1. Apply linear search algorithm 2. Apply select sort algorithm 3. Apply array iteration skill Problem description: Write the following eight methods and write a main function to test these methods // return the index of the first occurrence of key in arr // if key is not found in arra, return -1 public static int linearSearch(int arr[], int key) // sort the arr from least to largest by using select sort algorithm public stati void selectSort(int arr[])...

  • Array-based Queue Lecture 6 Two Class Exercises | Class Exercise #1 - Create an array-based queue that holds value...

    Array-based Queue Lecture 6 Two Class Exercises | Class Exercise #1 - Create an array-based queue that holds values of double data type. 1.) Create a program that produces the following output OUTPUT: Q Quit Enter your choice: e Enter an item: 1.1 E Enqueue D Dequeue s-show queue ← showMenuO function called in main) OQuit // screen clears-.. continue enqueuing.screen clearing with each iteration Enter your choice: e Queue is full. E Enqueue D Dequeue s Show queue 0...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT