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...
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 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) { }
Write a MIPS function using a MARS 4.5 complier named void makeRandomArray(int arr[], int size) that...
Write a MIPS function using a MARS 4.5 complier named void makeRandomArray(int arr[], int size) that generates and stores a specified number of random numbers each of whose values are between 0 and 1000 in an array
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)...
int f2 (int n) j = 0; while (j <n) {for (int i = 0; i...
int f2 (int n) j = 0; while (j <n) {for (int i = 0; i <n; ++ i) {cout << "j =" + j; j = j + 5; }}
Debug this code getting segmentation faults. //array_utils.h int contains(const int *arr,int size , int k); int...
Debug this code getting segmentation faults. //array_utils.h int contains(const int *arr,int size , int k); int containsWithin(const int *arr,int size , int k,int i , int j); int *paddedCopy(const int *arr,int oleSize , int newSize); void reverse(int *arr,int size); int *reverseCopy(const int *arr,int size); //array_utils.c #include"array_utils.h" #include<stdlib.h> int contains(const int *arr,int size , int k){    int i=0;    for(i=0;i<size;i++){        if(arr[i] == k)return 1;    }    return 0; } int containsWithin(const int *arr,int size , int k,int...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT