Wednesday, April 30, 2025

C Program - Find Largest Element of an Array

This code finds the largest element in an array of integers by iterating through the array and comparing each element to a variable called "largest_atm".

It starts by assuming that the first element in the array is the largest so far, and then it checks each subsequent element to see if it is larger. If it is, then it updates the value of "largest_atm" to be that element instead. After iterating through the entire array, it returns the value of "largest_atm".

In the "main" function, an array of integers is declared and initialized with some values.

The number of elements in the array is determined using the sizeof operator, which returns the total size of the array in bytes. Since each element in the array is an integer, and an integer takes up 4 bytes of memory, the total size of the array in bytes is equal to the number of elements multiplied by 4.

Therefore, the size of the array in bytes is divided by the size of one element in bytes to get the number of elements in the array. This value is then passed along with the array to the "find_largest" function, which returns the largest element in the array.

This value is then printed to the console using printf. Finally, the main function returns 0 to indicate successful program execution.

//largest elements

#include <stdio.h>

int find_largest(int some_array[], int number_of_elements)
{
    int x, largest_atm;

    largest_atm = some_array[0];

    for (x = 1; x < number_of_elements; x++)
        if (some_array[x] > largest_atm)
            largest_atm = some_array[x];

    return largest_atm;
}

int main()
{

    int some_array[] = {5, 25, 243, 1, -10, -5, 500};

    int number_of_elements = sizeof(some_array)/sizeof(some_array[0]);

    printf("Largest Element %d \n", find_largest(some_array, number_of_elements));

    return 0;
}

Result: 

Largest Element 500

Process returned 0 (0x0)   execution time : 0.031 s
Press any key to continue.

Here's a line-by-line explanation of the code: 

#include <stdio.h>

This line includes the standard input/output library, which is needed for functions like printf. 

int find_largest(int some_array[], int number_of_elements)
{
    int x, largest_atm;

    largest_atm = some_array[0];

    for (x = 1; x < number_of_elements; x++)
        if (some_array[x] > largest_atm)
            largest_atm = some_array[x];

    return largest_atm;
}

This is the implementation of the "find_largest" function. It takes two arguments: an array of integers "some_array" and the number of elements in the array "number_of_elements". It declares two integer variables, "x" and "largest_atm", and initializes "largest_atm" to the first element in the array.

It then loops through the rest of the elements in the array, comparing each element to "largest_atm" and updating "largest_atm" if the element is larger.

After iterating through the entire array, it returns the value of "largest_atm". 

int main()
{

    int some_array[] = {5, 25, 243, 1, -10, -5, 500};

    int number_of_elements = sizeof(some_array)/sizeof(some_array[0]);

    printf("Largest Element %d \n", find_largest(some_array, number_of_elements));

    return 0;
}

This is the "main" function of the program. It first declares an array of integers called "some_array" and initializes it with some values.

It then determines the number of elements in the array by dividing the total size of the array in bytes (which is given by "sizeof(some_array)") by the size of one element in bytes (which is given by "sizeof(some_array[0])"). It then calls the "find_largest" function, passing in "some_array" and "number_of_elements" as arguments, and prints the result to the console using printf.

Finally, it returns 0 to indicate successful program execution.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...