📜  程序使用动态内存分配在数组中查找最大的元素

📅  最后修改于: 2021-05-19 18:34:04             🧑  作者: Mango

给定一个由N个整数组成的数组arr [] ,任务是使用动态内存分配在给定的数组中找到最大的元素。

例子:

方法:这里的想法是使用动态内存来搜索给定数组中的最大元素。请按照以下步骤解决问题:

  1. N个元素和一个指针来存储N个元素的地址
  2. 动态地为N个元素分配内存。
  3. 将元素存储在分配的内存中。
  4. 通过使用指针比较值,遍历数组arr []以找到所有数字中最大的元素。

下面是上述方法的实现:

C
// C program for the above approach
#include 
#include 
 
// Function to find the largest element
// using dynamic memory allocation
void findLargest(int* arr, int N)
{
    int i;
 
    // Traverse the array arr[]
    for (i = 1; i < N; i++) {
 
        // Update the largest element
        if (*arr < *(arr + i)) {
            *arr = *(arr + i);
        }
    }
 
    // Print the largest number
    printf("%d ", *arr);
}
 
// Driver Code
int main()
{
    int i, N = 4;
 
    int* arr;
 
    // Memory allocation to arr
    arr = (int*)calloc(N, sizeof(int));
 
    // Condition for no memory
    // allocation
    if (arr == NULL) {
        printf("No memory allocated");
        exit(0);
    }
 
    // Store the elements
    *(arr + 0) = 14;
    *(arr + 1) = 12;
    *(arr + 2) = 19;
    *(arr + 3) = 20;
 
    // Function Call
    findLargest(arr, N);
    return 0;
}


C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the largest element
// using dynamic memory allocation
void findLargest(int* arr, int N)
{
 
    // Traverse the array arr[]
    for (int i = 1; i < N; i++) {
 
        // Update the largest element
        if (*arr < *(arr + i)) {
            *arr = *(arr + i);
        }
    }
 
    // Print the largest number
    cout << *arr;
}
 
// Driver Code
int main()
{
    int N = 4;
    int* arr;
 
    // Memory allocation to arr
    arr = new int[N];
 
    // Condition for no memory
    // allocation
    if (arr == NULL) {
        cout << "No memory allocated";
    }
 
    // Store the elements
    *(arr + 0) = 14;
    *(arr + 1) = 12;
    *(arr + 2) = 19;
    *(arr + 3) = 20;
 
    // Function Call
    findLargest(arr, N);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG{
 
// Function to find the largest element
// using dynamic memory allocation
static void findLargest(int []arr, int N)
{
    // Traverse the array arr[]
    for (int i = 1; i < N; i++)
    {
        // Update the largest element
        if (arr[0] < (arr[i]))
        {
            arr[0] = (arr[i]);
        }
    }
 
    // Print the largest number
    System.out.print(arr[0]);
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 4;
    int []arr;
 
    // Memory allocation to arr
    arr = new int[N];
 
    // Condition for no memory
    // allocation
    if (arr.length < N)
    {
        System.out.print("No memory allocated");
    }
 
    // Store the elements
    arr[0] = 14;
    arr[1] = 12;
    arr[2] = 19;
    arr[3] = 20;
     
    // Function Call
    findLargest(arr, N);
}
}
 
// This code is contributed by shikhasingrajput


Python3
# Python3 program for
# the above approach
 
# Function to find the largest element
# using dynamic memory allocation
def findLargest(arr, N):
   
    # Traverse the array arr
    for i in range(1, N):
       
        # Update the largest element
        if (arr[0] < (arr[i])):
            arr[0] = (arr[i]);
 
    # Print largest number
    print(arr[0]);
 
# Driver Code
if __name__ == '__main__':
    N = 4;
     
    # Memory allocation to arr
    arr = [0] * N;
 
    # Condition for no memory
    # allocation
    if (len(arr) < N):
        print("No memory allocated");
 
    # Store the elements
    arr[0] = 14;
    arr[1] = 12;
    arr[2] = 19;
    arr[3] = 20;
 
    # Function Call
    findLargest(arr, N);
 
# This code is contributed by shikhasingrajput


C#
// C# program for the above approach
using System;
class GFG{
 
// Function to find the largest
// element using dynamic memory allocation
static void findLargest(int []arr,
                        int N)
{
  // Traverse the array []arr
  for (int i = 1; i < N; i++)
  {
    // Update the largest element
    if (arr[0] < (arr[i]))
    {
      arr[0] = (arr[i]);
    }
  }
 
  // Print the largest number
  Console.Write(arr[0]);
}
 
// Driver Code
public static void Main(String[] args)
{
  int N = 4;
  int []arr;
 
  // Memory allocation to arr
  arr = new int[N];
 
  // Condition for no memory
  // allocation
  if (arr.Length < N)
  {
    Console.Write("No memory allocated");
  }
 
  // Store the elements
  arr[0] = 14;
  arr[1] = 12;
  arr[2] = 19;
  arr[3] = 20;
 
  // Function Call
  findLargest(arr, N);
}
}
 
// This code is contributed by Rajput-Ji


输出:
20







时间复杂度: O(N)
辅助空间: O(1)

想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。