352,632 views
19 votes
19 votes
now, write a program to compute the minimum value in a dynamic array. look over class notes and older array problems to see how you wrote a similar program for a static array, the logic will remain the same. recall that to access element i in an array arr, we can use the pointer notation: *(arr i) that is equivalent to arr[i].

User AmbianBeing
by
2.9k points

1 Answer

27 votes
27 votes

Answer:

#include <stdio.h>

#include <stdlib.h>

// Function prototype

int minValue(int *arr, int size);

int main() {

// Ask the user for the size of the array

int size;

printf("Enter the size of the array: ");

scanf("%d", &size);

// Allocate memory for the array

int *arr = malloc(size * sizeof(int));

// Read the values from the user

printf("Enter the values: ");

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

scanf("%d", arr + i);

}

// Compute and print the minimum value

int min = minValue(arr, size);

printf("The minimum value is: %d\\", min);

// Free the allocated memory

free(arr);

return 0;

}

// Function to compute the minimum value in an array

int minValue(int *arr, int size) {

// Initialize the minimum value with the first element

int min = *arr;

// Iterate over the remaining elements and update the minimum value

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

int value = *(arr + i);

if (value < min) {

min = value;

}

}

// Return the minimum value

return min;

}

User Takema
by
2.9k points