C Program To Find First and Second Biggest Element In An Array

Lets write a C program to find first and second biggest element/number in an array, without sorting it.

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

Example: Expected Output

Enter 5 unique integer numbers
5
2
6
4
3
First Big: 6
Second Big: 5
array with size 5

Important Note:
This code only works for inputs which has unique integer numbers. If there are duplicate integer numbers which is biggest in the array, then it’ll show the same number for both first and second biggest element. We can fix it, but it’s out of scope of this problem statement. Just know the limitation of this code.

Ex: a[5] = {2, 4, 5, 3, 5};
In this case, both first big and second big will have value 5.

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


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

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

Source Code: C Program To Find First and Second Biggest Element In An Array

#include<stdio.h>

#define N 5

int main()
{
    int a[N], i, fbig, sbig;

    if(N < 3)
    {
        printf("Please have an array with at least 2 elements\n");
        return(0);
    }

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

    (a[0] > a[1]) ? (fbig = a[0], sbig = a[1]) :
                    (fbig = a[1], sbig = a[0]);

    for(i = 2; i < N; i++)
    {
        if(fbig < a[i])
        {
            sbig = fbig;
            fbig = a[i];
        }
        else if(sbig < a[i])
        {
            sbig = a[i];
        }
    }

    printf("First Big: %d\nSecond Big: %d\n", fbig, sbig);

    return 0;
}

Output 1:
Enter 5 unique integer numbers
5
2
6
4
3
First Big: 6
Second Big: 5

Output 2:
Enter 5 unique integer numbers
2
4
5
7
9
First Big: 9
Second Big: 7

In above source code we’re making use of macros to assign array size.

Logic To Find First and Second Biggest In An Array

First we assign biggest of a[0] and a[1] to variable fbig and the second biggest to sbig.
1. Using if else

    if(a[0] > a[1])
    {
        fbig = a[0];
        sbig = a[1];
    }
    else
    {
        fbig = a[1];
        sbig = a[0];
    }

2. Using Ternary/Conditional Operators

    (a[0] > a[1]) ? (fbig = a[0], sbig = a[1]) :
                    (fbig = a[1], sbig = a[0]);

In both the cases the logic is same. If a[0] is greater than a[1], then value present at a[0] will be assigned to fbig, and a[1] will be assigned to sbig. If a[0] is not greater than a[1], then a[1] will be assigned to fbig and a[0] will be assigned to sbig.

Related Read:
Biggest of Two Numbers Using Ternary Operator: C

for loop
Since both a[0] and a[1] are already sorted out, the comparison must start from a[2]. So we initialize the loop counter variable i to 2 and loop through until i < N and for each iteration we increment the value of i by 1.

Inside for loop
If value of fbig is less than the fetched element of the array, then we transfer the value of fbig to sbig and assign the new array element value to fbig.

Else if, the fetched element of the array is less than fbig but greater than sbig, then we assign the value of the array element to sbig.

    for(i = 2; i < N; i++)
    {
        if(fbig < a[i])
        {
            sbig = fbig;
            fbig = a[i];
        }
        else if(sbig < a[i])
        {
            sbig = a[i];
        }
    }

Once control exits for loop, we print the values present in fbig and sbig.

Explanation With Example

If int a[6] = {2, 4, 5, 7, 9, 8};
Here a[0] = 2 and a[1] = 4;
After executing below code:

    (a[0] > a[1]) ? (fbig = a[0], sbig = a[1]) :
                    (fbig = a[1], sbig = a[0]);

fbig will have a[1], which is 4.
sbig will have a[0], which is 2.

indexa[index]sbigfbig
2a[2] = 545
3a[3] = 757
4a[4] = 979
5a[5] = 88

At the end of for loop, fbig will have 9 and sbig will have 8.

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

Positive or Negative or Zero Using Macros: C Program

C Program to check whether the user input integer number is positive, negative or zero using Macros and ternary / Conditional operator.

Related Read:
Positive or Negative or Zero Using Ternary Operator: C Program

Logic

If user input number is greater than 0, then the number is positive. If user input number is less than 0, then the number is negative. If neither of the above 2 conditions are true, then the number is zero.

Video Tutorial: Positive or Negative or Zero Using Macros: C Program


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

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

Source Code: Positive or Negative or Zero Using Macros: C Program

#include<stdio.h>

#define SIGN(num) ( (num > 0) ? \
                   printf("POSITIVE") : \
                   (num < 0) ? printf("NEGATIVE") : \
                   printf("ZERO"))

int main()
{
    int num;

    printf("Enter a number\n");
    scanf("%d", &num);

    SIGN(num);

    return 0;
}

Output 1:
Enter a number
5
POSITIVE

Output 2:
Enter a number
-2
NEGATIVE

Output 3:
Enter a number
0
ZERO

Here the macro template SIGN(num) will be replaced by its corresponding macro expansion ( (num > 0) printf(“POSITIVE”) : (num < 0) ? printf(“NEGATIVE”) : printf(“ZERO”)) by preprocessor. And if user input number is greater than 0, it’ll print POSITIVE else it’ll print NEGATIVE. If neither of those 2 conditions are true, then it’ll print ZERO.

In our program we’re using Macro Continuation (\) Preprocessor Operator: C Program in macro expansion to break from the line and continue the logic in new/next line.

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

C Program To Determine Leap Year or Not using Macros

In this video tutorial lets learn how to determine user input year is a leap year or not, using Macros and nested ternary / conditional operator.

Related Read:
C Program To Check Leap Year
C Program To Check Leap Year Using Ternary Operator
Using Macro Template In Macro Expansion: C Program

Leap Year Logic

1. If a year is century year(year ending with 00) and is perfectly divisible by 400, then it’s a leap year.

2. If a year is not a century year, and is perfectly divisible by 4, then it’s a leap year.

Video Tutorial: Determine Leap Year or Not using Macros: C Program


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

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

Source Code: C Program To Determine Leap Year or Not using Macros

#include<stdio.h>

#define NOT_LEAP(x) printf("%d is not leap year\n", x)
#define LEAP_YEAR(x) printf("%d is leap year\n", x)

#define LEAP(x) ( (x % 100 == 0 && x % 400 == 0) ? LEAP_YEAR(x) : \
                 ( (x % 4 ==0) ? LEAP_YEAR(x) : NOT_LEAP(x)) )

int main()
{
    int year;

    printf("Enter a year\n");
    scanf("%d", &year);

    LEAP(year);

    return 0;
}

Output 1:
Enter a year
2018
2018 is not leap year

Output 2:
Enter a year
2019
2019 is not leap year

Output 3:
Enter a year
2020
2020 is leap year

Output 4:
Enter a year
2023
2023 is not leap year

Output 5:
Enter a year
2024
2024 is leap year

In above program we’re writing NOT_LEAP(x) macro to display a message that the user input year is not a leap year. LEAP_YEAR(x) macro is used to display message that the user input year is a leap year.

We use both NOT_LEAP(x) and LEAP_YEAR(x) macro names or macro templates inside LEAP(x) macro expansion.

We’re also using macro continuation(\) to break the line and continue writing the logic in next line in macro expansion of LEAP(x).

List of some leap years

2000
2004
2008
2012
2016
2020
2024
2028
2032
2036
2040
2044
2048

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

Biggest of 3 Numbers using Macros: C Program

In this video lets see how we can make use of Macros and ternary / conditional operator to find biggest of three numbers.

Related Read:
Biggest of 3 Numbers Using Ternary Operator: C
Preprocessors In C Programming Language

What we learn in this video tutorial?

We learn how to make use of nested ternary / conditional operator in macro definition. And how to use macro continuation( \ ) preprocessor operator.

Video Tutorial: Biggest of 3 Numbers using Macros: C Program


[youtube https://www.youtube.com/watch?v=CYWSkQ2-kp4]

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

Source Code: Biggest of 3 Numbers using Macros: C Program

#include<stdio.h>

#define BIGGEST(x, y, z) ( (x > y && x > z) ? x : ( y > z) ? y : z )

int main()
{
    int a, b, c;

    printf("Enter 3 integer numbers\n");
    scanf("%d%d%d", &a, &b, &c);

    printf("Biggest of 3 numbers is %d\n", BIGGEST(a, b, c));

    return 0;
}

Output:
Enter 3 integer numbers
20
50
60
Biggest of 3 numbers is 60

Here we’re writing logic inside macro expansion. Wherever macro template is found in our source code, preprocessor replaces that macro template with macro expansion and the compiler compiles the code like normal source code.

Nested Ternary / Conditional Operator

Here we are using nested conditional operator. First we check if value of a is greater b and c, if true, value of a will be returned orelse we check if b is greater than c, if true, value of b will be returned orelse value of c will be returned.

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

Macros With Arguments: C Program

In this video tutorial lets see how to write macros with arguments. We’ll illustrate finding biggest of 2 numbers by using “Macros with arguments” concept.

Note: Do not leave space between macro template name and the argument list / parenthesis. Also make sure to write the macro expansion inside parenthesis.

Related Read:
Preprocessors In C Programming Language
C Program To Find Area of Circle Using Macro Expansion

Video Tutorial: Macros With Arguments: C Program (Biggest of 2 Numbers)


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

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

Source Code: Macros With Arguments: C Program

#include<stdio.h>

#define MAX(x, y) ( x > y ? x : y )

int main()
{
    int a, b;

    printf("Enter 2 numbers\n");
    scanf("%d%d", &a, &b);

    printf("Biggest of %d and %d is %d\n", a, b, MAX(a, b));

    return 0;
}

Output 1:
Enter 2 numbers
14
5
Biggest of 14 and 5 is 14

Output 2:
Enter 2 numbers
5
14
Biggest of 5 and 14 is 14

Functions return result after evaluating the expression, but Macros are different. Before compiler compiles the program, preprocessor replaces all the occurrence of Macro template with Macro expansion. So in macros, it won’t return the result, but instead the entire macro expansion gets placed in place of macro template.

So in above C program, printf() becomes:

printf("Biggest of %d and %d is %d\n", a, b, ( a > b ? a : b ));

Related Read:
Biggest of Two Numbers Using Ternary Operator: C

Check Below 2 Source code and it’s output

No Parenthesis around macro expansion

#include<stdio.h>

#define SQUARE(n) n * n

int main()
{
  printf("%f", 64/SQUARE(4));

  return 0;
}

Output:
64

Parenthesis around macro expansion

#include<stdio.h>

#define SQUARE(n) (n * n)

int main()
{
  printf("%f", 64/SQUARE(4));

  return 0;
}

Output:
4

In C programming and in mathematics in general, each arithmetic operation has different precedence. Hence by using parenthesis around macro expansion, it gets the highest precedence.

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