Question

In: Computer Science

Send an element from 2d array to a function using pointer to pointer. c++ I am...

Send an element from 2d array to a function using pointer to pointer. c++

I am having an issue with being able to send specific elements to the swap function.

#include <iostream>
using namespace std;

void swap(int **a, int **b){
   int temp = **a;
   **a=**b;
   **b=temp;
}

void selSort(int **arr, int d){
   for(int i =0; i<d-1; i++){
       int min = *(*arr+i);
       int minin = i;
       for(int j =i+1; j<d; j++){
           if(*(*arr+j) < min){
               min = *(*arr+j);
               minin = j;
           }
           swap(*&(arr+i),*&(arr+minin));
       }
   }

}

int main() {
   int array[2][2] = {4,3,2,1},
       *ptr = &array[0][0],
       **pptr = &ptr;
   int size = sizeof(array)/sizeof(int);


   for(int i =0; i<size; i++){
           cout <<*(*pptr+i)<<endl;
       }

   selSort(pptr, size);

   for(int i =0; i<size; i++){
           cout <<*(*pptr+i)<<endl;
       }


   return 0;
}

Solutions

Expert Solution

#include <iostream>
using namespace std;

void swap(int **a, int **b){
int temp = **a;
**a=**b;
**b=temp;
}

void selSort(int **arr, int d){
for(int i =0; i<d-1; i++){
int min = *(*arr+i);
int minin = i;
for(int j =i+1; j<d; j++){
if(*(*arr+j) < min){
min = *(*arr+j);
minin = j;
}
swap(*(*arr+i),*(*arr+minin));

// *arr derference value and add to min and then *(*arr+i) is the address which is passed
}
}

}

int main() {
int array[2][2] = {4,3,2,1},
*ptr = &array[0][0],
**pptr = &ptr;
int size = sizeof(array)/sizeof(int);

   cout<<"Before Sorting: \n"; // msg you can also delete it if want
for(int i =0; i<size; i++){
cout <<*(*pptr+i)<<endl;
}

selSort(pptr, size);
   cout <<"After Sorting: \n";
for(int i =0; i<size; i++){
cout <<*(*pptr+i)<<endl;
}


return 0;
}

/* OUTPUT */


Related Solutions

Using c++ to find all the peaks elements in a 2d array, each element has eight...
Using c++ to find all the peaks elements in a 2d array, each element has eight neighbours
How to write a C++ of CountingSort function using 2D vector? CountingSort(vector > array) Input #...
How to write a C++ of CountingSort function using 2D vector? CountingSort(vector > array) Input # of rows: 2 Input Row 1: 9 8 7 6 3 2 1 5 4 Input Row 2: 1 2 4 3 5 6 9 8 7 Output 1,2,3,4,5,6,7,8,9 1,2,3,4,5,6,7,8,9
Create a function output() in C that takes the pointer to the array and its size...
Create a function output() in C that takes the pointer to the array and its size and prints the arrays' contests. (function prototype : void output(int *arrayPtr, int size);
I am writing a scheme function that pairs an element to every element in a list,...
I am writing a scheme function that pairs an element to every element in a list, and I have the following code so far: (define (pair-element e l)    (cond ((null? l) (list e)) (else (cons e (car l)) (pair-element (cdr l))))) (pair-element 4 `(4 3 5)) What is my error? I'm getting a pair element mismatch when I run it. I need to do this without using built-in functions. Thanks!
I am running this code using C to count the words in char array, but somehow...
I am running this code using C to count the words in char array, but somehow it keeps counting the total characters + 1. Any solution to fix this problem? #include <stdio.h> int main() {    char input[1001];    int count =0;    fgets(input, 1001, stdin);    char *array = input;    while(*array != '\0')       {        if(*array == ' '){            array++;        }        else{        count++;        array++;...
THE FOLLOWING IS CODED IN C Write a function that sets each element in an array...
THE FOLLOWING IS CODED IN C Write a function that sets each element in an array to the sum of the corresponding elements in two other arrays. That is, if array 1 has the values 2,4, 5, and 8 and array 2 has the values 1, 0, 4, and 6, the function assigns array 3 the values 3, 4, 9, and 14. The function should take three array names and an array size as arguments. Test the function in a...
In C create an array of 4 integers. Assign a pointer to the array. Use the...
In C create an array of 4 integers. Assign a pointer to the array. Use the pointer to find the average value of the elements in the array and display the results on the screen.
HOW TO PRINT THE CONTENTS OF A TWO-DIMENSIONAL ARRAY USING POINTER ARITHMETIC (USING FUNCTIONS) (C++) Fill...
HOW TO PRINT THE CONTENTS OF A TWO-DIMENSIONAL ARRAY USING POINTER ARITHMETIC (USING FUNCTIONS) (C++) Fill out the function definition for "void print_2darray_pointer(double *twoDD, int row, int col)". Should match the output from the function void print_2darray_subscript(double twoDD[][ARRAY_SIZE], int row, int col) # include using namespace std;    const int ARRAY_SIZE = 5;    const int DYNAMIC_SIZE = 15;    const int TIC_TAC_TOE_SIZE = 3;    // function definitions:    void print_2darray_subscript(double twoDD[][ARRAY_SIZE], int row, int col)        //...
1) Write a function that receives a pointer to an array along with the size of...
1) Write a function that receives a pointer to an array along with the size of the array, it returns the largest number in the array. 2) Write a function that receives a string and returns the length of it.
I am building a tik tac toe board using python and the function I am using...
I am building a tik tac toe board using python and the function I am using is not working to check to see if the board is full. I'll include the code for the board and the function to check if it is full. X's and O's are being inserted and the function is suppose to stop running when is_full(board) returns True. ch is either x or o board = []    for i in range(3):     board.append([])     for j...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT