Question

In: Computer Science

Consider the following code: void swap(int arr[], int i, int j) {        int temp = arr[i];...

Consider the following code:

 

void swap(int arr[], int i, int j) {

       int temp = arr[i];

       arr[i] = arr[j];

       arr[j] = temp;

}

void function(int arr[], int length)

{

       for (int i = 0; i<length / 2; i++)

              swap(arr, i, (length / 2 + i) % length);

}

If the input to the function was 
   int arr[] = { 6, 1, 8, 2, 5, 4, 3, 7 };
   function(arr,8);
What values would be stored in the array after calling the function?

What is the output of the following code?  (as always indicate "error" if there is any kind of runtime or compile-time error or "infinite" if there is an infinite loop)

 

void swap(int &a, int &b)

{

       int t = a;

       a = b;

       b = t;

}

void function(int arr[], int arr2[], int length)

{

       for (int i = 0; i<length; i++)

       {

              swap(arr[i], arr[(i + arr2[i]) % length]);

       }

}

int main()

{

       int arr1[] = { 5, 2, 3, 7, 1, 6, 8, 4 };

       int arr2[] = { 1, 4, -2, -1, 2, 7, 2, 1 };

       function(arr1, arr2, 8);

       for (int a : arr1)

              cout << a << " ";

}

Solutions

Expert Solution

1) Values in the array after calling the function

Below is the program of the above output -

public class Main
{
    static void swap(int arr[], int i, int j) {

       int temp = arr[i];

       arr[i] = arr[j];

       arr[j] = temp;

    }
    
    static void function(int arr[], int length)
    {
        for (int i = 0; i<length / 2; i++)
            swap(arr, i, (length / 2 + i) % length); 
    }
        public static void main(String[] args) {
                int arr[] = { 6, 1, 8, 2, 5, 4, 3, 7 };
        function(arr,8);
        for(int i=0;i<8;i++){
            System.out.print(arr[i] + " ");
       }
        }
}

2) Improvised Code

#include<iostream>
using namespace std;
void swap(int &a, int &b)
{
       int t = a;
       a = b;
       b = t;
}

void function(int arr[], int arr2[], int length)
{
       for (int i = 0; i<length; i++)
       {
              swap(arr[i], arr[(i + arr2[i]) % length]);
       }
}

int main(){
       int arr1[] = { 5, 2, 3, 7, 1, 6, 8, 4 };
       int arr2[] = { 1, 4, -2, -1, 2, 7, 2, 1 };
       function(arr1, arr2, 8);
      for (int a : arr1){
        cout<<a<<" ";
      }
}

Output

Drop a comment for any update or clarification, I would love to assist you.


Related Solutions

Consider the following program written in C syntax: void swap(int a, int b) { int temp;...
Consider the following program written in C syntax: void swap(int a, int b) { int temp; temp = a; a = b; b = temp;} void main() { int value = 2, list[5] = {1, 3, 5, 7, 9}; swap(value, list[0]); swap(list[0], list[1]); swap(value, list[value]); } For each of the following parameter-passing methods, what are all of the values of the variables value and list after each of the three calls to swap? Passed by value Passed by reference Passed...
#include <stdio.h> void printDistinct(int arr[], int c) { int i, j; printf("\nArray:\n"); // Picking all elements...
#include <stdio.h> void printDistinct(int arr[], int c) { int i, j; printf("\nArray:\n"); // Picking all elements one by one for (i = 0; i < c; i++) { // Checking if the picked element is already printed for (j = 0; j <= i; j++) { // If current element is already there in the array, break from j loop if (arr[i] == arr[j]) { break; } } // If it is not printed earlier and is within 10-100, then...
What is the ouput of the following code? void loop(int num) { for(int i = 1;...
What is the ouput of the following code? void loop(int num) { for(int i = 1; i < num; ++i) { for(int j = 0; j < 5; ++j) { cout << j; } } } int main() { loop(3); return 0; }
   private static void merge(int arr[], int l, int m, int r) {        //...
   private static void merge(int arr[], int l, int m, int r) {        // Find sizes of two subarrays to be merged        int n1 = m - l + 1;        int n2 = r - m;        /* Create temp arrays */        int L[] = new int[n1];        int R[] = new int[n2];        /* Copy data to temp arrays */        for (int i = 0; i...
Please paraphrase this c code #include <stdio.h> #include <stdlib.h> #include <string.h> void sortGrades(int arr[], int size,...
Please paraphrase this c code #include <stdio.h> #include <stdlib.h> #include <string.h> void sortGrades(int arr[], int size, int status, char names[][20]); void printer(int grades[], int size, char names[][20]); void sortNames(char arr[][20], int size, int status, int grades[]); void nameSearch(int grades[], int size, char names[][20]); void numSearch(int grades[], int size, char names[][20]); int main() { int i; int size; int option; do { printf("\n\nInput Number of Students or 0 to exit : "); scanf("%d", &size); if (size == 0) { break; }...
Please explain this code line by line void printperm(int *A,int n,int rem,int j) {    if(n==1)...
Please explain this code line by line void printperm(int *A,int n,int rem,int j) {    if(n==1)       {        for(int k=0;k<j;k++)        cout<<A[k]<<" + ";        cout<<rem<<"\n";        return;       }     for(int i=0;i<=rem;i++)    {          if(i<=rem)          A[j]=i;          printperm(A,n-1,rem-i,j+1);    } }
JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
JAVA please write this method public static void recursiveMergeSort(int[] arr) { }
JAVA please write this method public static void recursiveMergeSort(int[] arr) { }
Topic: Template template void arrayContents(const T *arr, int countSize); int main() { const int ACOUNT =...
Topic: Template template void arrayContents(const T *arr, int countSize); int main() { const int ACOUNT = 5; const int BCOUNT = 7; const int CCOUNT = 6; int a[ACOUNT] = {1, 2, 3, 4, 5}; double b[BCOUNT] = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7}; char c[CCOUNT] = "HELLO"; cout <<"Array A contents: \n"; arrayContents(a, ACOUNT); cout <<"Array B contents: \n"; arrayContents(b, BCOUNT); cout <<"Array C contents: \n"; arrayContents(c, CCOUNT); return 0; } template void arrayContents(const T *arr, int countSize)...
import java.util.*; class A { int i, j, k; public A(int i, int j, int k)...
import java.util.*; class A { int i, j, k; public A(int i, int j, int k) { this.i=i; this.j=j; this.k=k; } public String toString() { return "A("+i+","+j+","+k+")"; } } class Main { public static void main(String[] args) { ArrayList<A> aL=new ArrayList<A>(); Random rand= new Random(1000); //1000 is a seed value for (int p=0; p<10; p++) { int i = rand.nextInt(100); int j = rand.nextInt(200); int k = rand.nextInt(300); aL.add(new A(i, j, k)); } System.out.println("----- Original arraylist------"); for (A a: aL)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT