Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. DATASET

DATASET

Scheduled Pinned Locked Moved C / C++ / MFC
c++data-structurestutorialquestion
4 Posts 4 Posters 36 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Aryan Gupta 2024
    wrote on last edited by
    #1

    I know my code is correct. But is my code completing all the demands of the question. Q2: Let A[n] be an array of n distinct integers. If i < j and A[i] > A[j], then the pair (i, j) is called an inversion of A. Write a C/C++ program that determines the number of inversions in any permutation on n elements in O(n lg n) worst-case time. (Hint: Modify merge sort) Example: A = {4, 1, 3, 2} output is 4 #include int total_inversions(int arr[], int n, int count); int main(){ int arr[] = {4, 1, 3, 2}; int n = sizeof(arr) / sizeof(arr[0]); int count = 0; count = total_inversions(arr, n , count); printf("%d", count); return 0; } int total_inversions(int arr[], int n, int count){ for(int i = 0; i < n-1; i++){ for(int j = i+1; j < n; j++){ if (arr[i] > arr[j]){ count++; } } } return count; }

    D Richard DeemingR 2 Replies Last reply
    0
    • A Aryan Gupta 2024

      I know my code is correct. But is my code completing all the demands of the question. Q2: Let A[n] be an array of n distinct integers. If i < j and A[i] > A[j], then the pair (i, j) is called an inversion of A. Write a C/C++ program that determines the number of inversions in any permutation on n elements in O(n lg n) worst-case time. (Hint: Modify merge sort) Example: A = {4, 1, 3, 2} output is 4 #include int total_inversions(int arr[], int n, int count); int main(){ int arr[] = {4, 1, 3, 2}; int n = sizeof(arr) / sizeof(arr[0]); int count = 0; count = total_inversions(arr, n , count); printf("%d", count); return 0; } int total_inversions(int arr[], int n, int count){ for(int i = 0; i < n-1; i++){ for(int j = i+1; j < n; j++){ if (arr[i] > arr[j]){ count++; } } } return count; }

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      You're going to have to ask your teacher that question.

      Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles. Dave Kreskowiak

      1 Reply Last reply
      0
      • A Aryan Gupta 2024

        I know my code is correct. But is my code completing all the demands of the question. Q2: Let A[n] be an array of n distinct integers. If i < j and A[i] > A[j], then the pair (i, j) is called an inversion of A. Write a C/C++ program that determines the number of inversions in any permutation on n elements in O(n lg n) worst-case time. (Hint: Modify merge sort) Example: A = {4, 1, 3, 2} output is 4 #include int total_inversions(int arr[], int n, int count); int main(){ int arr[] = {4, 1, 3, 2}; int n = sizeof(arr) / sizeof(arr[0]); int count = 0; count = total_inversions(arr, n , count); printf("%d", count); return 0; } int total_inversions(int arr[], int n, int count){ for(int i = 0; i < n-1; i++){ for(int j = i+1; j < n; j++){ if (arr[i] > arr[j]){ count++; } } } return count; }

        Richard DeemingR Offline
        Richard DeemingR Offline
        Richard Deeming
        wrote on last edited by
        #3

        Aryan Gupta 2024 wrote:

        I know my code is correct. But is my code completing all the demands of the question.

        Technically, if it's not "completing all the demands of the question", then it's not "correct". :) For example, this random number generator code[^] is "correct" in the sense that it compiles and works. But it's not "correct" in the sense of fulfilling the requirements of an RNG function.


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

        CPalliniC 1 Reply Last reply
        0
        • Richard DeemingR Richard Deeming

          Aryan Gupta 2024 wrote:

          I know my code is correct. But is my code completing all the demands of the question.

          Technically, if it's not "completing all the demands of the question", then it's not "correct". :) For example, this random number generator code[^] is "correct" in the sense that it compiles and works. But it's not "correct" in the sense of fulfilling the requirements of an RNG function.


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #4

          'Your' :-D random generator code is simply fantastic!

          "In testa che avete, Signor di Ceprano?" -- Rigoletto

          In testa che avete, signor di Ceprano?

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups