C Program To Find Biggest Element of An Array

Lets write a C program to find biggest or the largest element in an array, without sorting the elements. And also print the position at which the biggest number is present in the array.

Related Read:
Find Biggest In An Array, Without Sorting It: C

Example: Expected Output

Enter 5 integer numbers
5
2
6
4
3

Biggest of 5 numbers is 6, at position 3

array with size 5

Video Tutorial: C Program To Find Biggest Element In An Array


[youtube https://www.youtube.com/watch?v=Ram2KW-n0Qc]

YouTube Link: https://www.youtube.com/watch?v=Ram2KW-n0Qc [Watch the Video In Full Screen.]

Source Code: C Program To Find Biggest Element of An Array

Method 1

#include<stdio.h>

#define N 5

int main()
{
    int a[N], i, big, pos;

    printf("Enter %d integer numbers\n", N);
    for(i = 0; i < N; i++)
    {
        scanf("%d", &a[i]);

        if(i == 0 || big < a[i])
        {
            big = a[i];
            pos = i + 1;
        }
    }

    printf("\nBiggest of %d numbers is %d, at position %d.\n", N, big, pos);

    return 0;
}

Output 1:
Enter 5 integer numbers
1
3
2
5
8

Biggest of 5 numbers is 8, at position 5.

Output 2:
Enter 5 integer numbers
10
2
5
9
3

Biggest of 5 numbers is 10, at position 1.

Logic To Find Biggest Element In An Array

We ask the user to input N integer numbers. N being a Macro. In above source code N is 5. So user enters 5 integer numbers. While the user inputs numbers we check if it’s the first number input by the user. If its the first number, then we assign that first number entered by the user to variable big and assign 1 to variable pos, indicating that its the first element in the array.

Next, for each consecutive iteration of the for loop we check if the new value entered by the user is greater than the value present in variable big. If it’s true, then we assign the new value entered by the user to variable big and also update the value of pos accordingly.

Once i < N condition is false, control exits for loop and we print the value present inside variable big and pos which will have biggest of N numbers or the biggest element of the array and position of that element in the array.

Explanation With Example

If int a[5] = {2, 4, 3, 4, 5};

    for(i = 0; i < N; i++)
    {
        scanf("%d", &a[i]);

        if(i == 0 || big < a[i])
        {
            big = a[i];
            pos = i + 1;
        }
    }
indexa[index]bigpos = index + 1
0a[0] = 221
1a[1] = 442
2a[2] = 3
3a[3] = 4
4a[4] = 555
5

a[5] = {2, 4, 3, 4, 5}
big = 5;
pos = 5;

Source Code: C Program To Find Biggest Element of An Array

Method 2

#include<stdio.h>

#define N 5

int main()
{
    int a[N], i, big, pos;

    printf("Enter %d integer numbers\n", N);
    for(i = 0; i < N; i++)
    {
        scanf("%d", &a[i]);
    }

    big = a[0];
    pos = 1;

    for(i = 1; i < N; i++)
    {
        if(big < a[i])
        {
            big = a[i];
            pos = i + 1;
        }
    }

    printf("\nBiggest of %d numbers is %d, at position %d.\n", N, big, pos);

    return 0;
}

Output 1:
Enter 5 integer numbers
1
5
6
3
0

Biggest of 5 numbers is 6, at position 3.

Output 2:
Enter 5 integer numbers
2
4
3
4
5

Biggest of 5 numbers is 5, at position 5

Method 2: LOGIC

Here we accept N integer numbers from the user. Next we assign the first element of the array to big and 1 to pos. Now we use another for loop to loop through the array. Inside for loop we check if the value present inside variable big is less than the value present at a[i]. If it’s true, then we assign the value of a[i] to big and value of (i+1) to variable pos.

At the end of for loop, variable big and pos will have biggest element of the array and the position at which this number is present in the array.

Note: Since we assign the value of first element of the array(which is present at location a[0]) to variable big, while comparing with other elements of the array, we start comparing from a[1] till a[N-1]. That’s the reason we’ve assigned initial value of i to 1 in the second for loop.

Since a[0] is assigned to variable big, there is no point in comparing big with a[0]. So we start comparison from a[1].

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Canvas Basics: HTML5

We shall start exploring more about Canvas element of HTML5.
It’s designed to draw graphics on the fly, using Javascript.

Canvas is like a placeholder for graphics. We place a context upon canvas, upon which we draw our graphics – lines, circles, triangle, pictures etc.

HTML file
index.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
< !DOCTYPE HTML>
<html>
 <head>
  <title>Canvas: HTML5</title>
  <meta charset="utf-8"/>
  <link href="myStyle.css" rel="stylesheet"/>
 </head>
 <body>
 
  <canvas id="holder" width="300" height="200">
    Please upgrade your browser!
  </canvas>
 
</body>
</html>

Here we have attached a style sheet to index.html
and we have the canvas element with an id, width and a height property applied to it.

We can specify some text in-between the opening and closing canvas tag, which shows up in the browser which doesn’t support HTML5’s canvas element.

CSS file
myStyle.css

1
2
3
canvas {
border: 3px groove black;
}

Here we’re applying some basic style to our canvas.

Since canvas is transparent by nature, we can’t see it unless we give it a border or fill some color into it. So, using CSS we apply 3px wide black border, for the purpose of illustration.

Canvas Basics: HTML5


[youtube https://www.youtube.com/watch?v=2lJqB9RikYo]

YouTube Link: https://www.youtube.com/watch?v=2lJqB9RikYo [Watch the Video In Full Screen.]



CSS width and height
We could even assign width and height property of canvas element via CSS, but it is not the proper way to do it.

We must give width and height property inside HTML document itself – as canvas property.

If you assign width and height via CSS, it’ll either stretch or shrink depending on the actual canvas width and height: which looks weird.

Basic XML: Starting With Root Tag

Video tutorial explains the basics of XML and some rules to be followed to write the XML elements.
Also illustrating the way the raw XML files are displayed on the modern browsers.

Its a tag based language much like HTML, the difference is we can makeup our own tags.
XML is used to structure and describe the information.

Header Information

1
< ?xml version="1.0" encoding="utf-8"?>

it informs about the xml version and the encoding, i.e., the standard encoding utf-8
This is optional. But still recommended by W3C
And it is automatically generated by many tools, so lets keep it! It doesn’t hurt :-)

Full code

1
2
3
4
5
< ?xml version="1.0" encoding="utf-8"?>
<xml>
  IBM, Oracle, Apple, Maestro, Microsoft ..
  <!-- Google, Yahoo! -->
</xml>

Each XML document must have only 1 root tag.
Multiple root tag gives errors.

Rules To Write Elements(Tags) Names
Can begin with underscore or letter ..followed by zero or more letters, digits, hyphens, periods and underscores.

Video Tutorial: Starting With Root Tag: Basic XML


[youtube https://www.youtube.com/watch?v=6KUzJSEixXI]

YouTube Link: https://www.youtube.com/watch?v=6KUzJSEixXI [Watch the Video In Full Screen.]



Output in the browsers
this is on Chrome

1
2
3
4
5
6
This XML file does not appear to have any style information associated with it. 
The document tree is shown below.
<companynames>
IBM, Oracle, Apple, Maestro, Microsoft ..
<!-- Google, Yahoo! -->
</companynames>