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);    } }
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)...
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...
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; }}
Write a method public static void minMax(int[] arr) that takes an array of unique ints of...
Write a method public static void minMax(int[] arr) that takes an array of unique ints of length at least two as an argument, and swaps the smallest value of the array into the 0th position and swaps the largest value of the array into the last position. For example, if int[] a = {4, 3, 2, 6, 1, 5}, the method call minMax(a) should modify the array so that it is {1, 3, 2, 5, 4, 6}. The method should...
1. Consider the following code: public class Widget implements Serializable { private int x; public void...
1. Consider the following code: public class Widget implements Serializable { private int x; public void setX( int d ) { x = d; } public int getX() { return x; } writeObject( Object o ) { o.writeInt(x); } } Which of the following statements is true? I. The Widget class is not serializable because no constructor is defined. II. The Widget class is not serializable because the implementation of writeObject() is not needed. III. The code will not compile...
Programmer defined Function / Arrays: 1.Write a function for printing an array. void print_array (int arr[],...
Programmer defined Function / Arrays: 1.Write a function for printing an array. void print_array (int arr[], int size); 2. Write a function to generate an array of random integers. void random_array (int arr[], int size, int range); The scale of numbers should be 1 to range. 3. Write a function to find the sum of a sequence of number. int sum (int arr[], int size); 4. Write a function rotate(arr[], d, n) that rotates arr[] of size n by d...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT