DATASET
-
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; }
-
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; }
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
-
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; }
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
-
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