Create a function called min that receives two parameters, an int* and int size. The function should return the minimum value in the array. Assume that the array passed to min will always have at least one element. Your code should only use pointer arithmetic. Do not use [] notations.

Respuesta :

ijeggs

Answer:

public static int findMin(int []intAray, int n){

   int i=0;

   int min = intAray[i];

   for(i=0; i<n; i++){

       if(intAray[i]<min){

           min =intAray[i];

       }

   }

   return min;

}

Explanation:

  • Using Java programming Language,
  • The method findMin() is created two parameters, an array and n for the length of the array
  • Using a for loop, the method finds the smallest element in the array by comparing each element
  • See a complete java code with a main method below:

public class num2 {

   public static void main(String[] args) {

       //Create an Array and initialize

      int []intArry = {2,3,4,1,5,10};

       System.out.println("The minimum element is "+findMin(intArry,6));

   }

//The Method findMin

public static int findMin(int []intAray, int n){

   int i=0;

   int min = intAray[i];

   for(i=0; i<n; i++){

       if(intAray[i]<min){

           min =intAray[i];

       }

   }

   return min;

}

}

Answer:

#include <iostream>

using namespace std;

int min(int *arr, int size) {

   int smallest = *arr;

   for (int i = 0; i < size; ++i) {

       if (*arr < smallest)

           smallest = *arr;

       arr++;

   }

   return smallest;

}

int main() {

   int arr[] = {-1, 22, 54, 33, -40, 67, 8, 15}, size = 8;

   cout << "Array:";

   for (int i = 0; i < size; ++i) {

       cout << " " << *(arr + i);

   }

   cout << endl << "Min: " << min(arr, size) << endl;

   return 0;

}

ACCESS MORE
EDU ACCESS