APC - Practical - Unit-1 - Sem - 2

 

APC - Practical - Unit-1 - Sem - 2


1.

Aim: Write a program to declare 1 – D array and assign the value to array elements statically.

Program:

#include <stdio.h>

#include <conio.h>

 

void main()

{

    int arr[] = {1, 2, 3, 4, 5};

    printf("%d\n%d\n%d\n%d\n%d\n", arr[0], arr[1], arr[2], arr[3], arr[4]);

 

    getch();

}

 

Output:

1

2

3

4

5

 

Please follow me for more practicals....

 


2.

Aim: Write a program to read 1-D array elements from user and print it dynamically.

Program:

#include <stdio.h>

#include <conio.h>

 

void main()

{

    int arr[100], size, i;

 

    printf("Enter the size of an array = ");

    scanf("%d", &size);

 

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

    {

        printf("arr[%d] = ", i);

        scanf("%d", &arr[i]);

    }

 

    printf("\nArray Elements =  ");

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

    {

        printf("%d  ", arr[i]);

    }

 

    getch();

}

 

Output:

Enter the size of an array = 5

arr[0] = 1

arr[1] = 2

arr[2] = 3

arr[3] = 4

arr[4] = 5

 

Array Elements =  1  2  3  4  5

 

Please follow me for more practicals....

 


3.

Aim: Write a program to find minimum and maximum number from 1-D array.

Program:

#include <stdio.h>

#include <conio.h>

 

void main()

{

    int arr[100], size, i, max, min;

 

    printf("Enter the size of an array = ");

    scanf("%d", &size);

 

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

    {

        printf("arr[%d] = ", i);

        scanf("%d", &arr[i]);

    }

 

    max = min = arr[0];

 

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

    {

        if (arr[i] > max)

        {

            max = arr[i];

        }

        if (arr[i] < min)

        {

            min = arr[i];

        }

    }

 

    printf("\nMaximum element = %d", max);

    printf("\nMinimum element = %d", min);

 

    getch();

}

 

Output:

Enter the size of an array = 5

arr[0] = 1

arr[1] = 2

arr[2] = 3

arr[3] = 4

arr[4] = 5

 

Maximum element = 5

Minimum element = 1

 

Please follow me for more practicals....

 


4.

Aim: Write a program to search to given number in the 1-D array or not.

Program:

#include <stdio.h>

#include <conio.h>

 

void main()

{

    int arr[100], size, i, element;

 

    printf("Enter the size of an array = ");

    scanf("%d", &size);

 

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

    {

        printf("arr[%d] = ", i);

        scanf("%d", &arr[i]);

    }

 

    printf("\nEnter the element to search = ");

    scanf("%d", &element);

 

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

    {

        if (arr[i] == element)

        {

            break;

        }

    }

 

    if (i == size)

    {

        printf("\nSorry! %d not found.", element);

    }

    else

    {

        printf("\nElement %d is found at position %d.", element, i);

    }

 

    getch();

}

 

Output:

Enter the size of an array = 5

arr[0] = 1

arr[1] = 2

arr[2] = 3

arr[3] = 4

arr[4] = 5

 

Enter the element to search = 3

 

Element 3 is found at position 2.

 

Please follow me for more practicals....

 

5.

Aim: Write a program to insert an element in 1-D array at specified place.

Program:

#include <stdio.h>

#include <conio.h>

 

void main()

{

    int arr[100], size, i, element, position;

 

    printf("Enter the size of an array = ");

    scanf("%d", &size);

 

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

    {

        printf("arr[%d] = ", i);

        scanf("%d", &arr[i]);

    }

 

    printf("\nEnter the element to add = ");

    scanf("%d", &element);

    printf("Enter the position to add = ");

    scanf("%d", &position);

 

    for (i = size - 1; i >= position; i--)

    {

        arr[i + 1] = arr[i];

    }

 

    arr[position] = element;

    size += 1;

 

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

    {

        printf("\narr[%d] = %d", i, arr[i]);

    }

 

    getch();

}

 

Output:

Enter the size of an array = 5

arr[0] = 1

arr[1] = 2

arr[2] = 3

arr[3] = 4

arr[4] = 5

 

Enter the element to add = 20

Enter the position to add = 2

 

arr[0] = 1

arr[1] = 2

arr[2] = 20

arr[3] = 3

arr[4] = 4

arr[5] = 5

 

 

Please follow me for more practicals....

 

6.

Aim: Write a program to delete an element from 1-D array.

Program:

#include <stdio.h>

#include <conio.h>

 

void main()

{

    int arr[100], size, i, position;

 

    printf("Enter the size of an array = ");

    scanf("%d", &size);

 

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

    {

        printf("arr[%d] = ", i);

        scanf("%d", &arr[i]);

    }

 

    printf("\nEnter the position to delete element = ");

    scanf("%d", &position);

 

    for (i = position; i < size; i++)

    {

        arr[i] = arr[i + 1];

    }

 

    size -= 1;

 

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

    {

        printf("\narr[%d] = %d", i, arr[i]);

    }

 

    getch();

}

 

Output:

Enter the size of an array = 5

arr[0] = 1

arr[1] = 2

arr[2] = 3

arr[3] = 4

arr[4] = 5

 

Enter the position to delete element = 2

 

arr[0] = 1

arr[1] = 2

arr[2] = 4

arr[3] = 5

 

 

 

Please follow me for more practicals....

 

7.

Aim: Write a program to merge two 1-D array a and b into c array that contains all elements from array a and b.

Program:

#include <stdio.h>

#include <conio.h>

 

void main()

{

    int a[50], b[50], c[100], size, i, j = 0;

 

    printf("Enter the size of an array = ");

    scanf("%d", &size);

 

    printf("\nEnter the elements of 1st array:\n");

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

    {

        printf("a[%d] = ", i);

        scanf("%d", &a[i]);

    }

    printf("\nEnter the elements of 2nd array:\n");

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

    {

        printf("a[%d] = ", i);

        scanf("%d", &b[i]);

    }

 

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

    {

        c[j] = a[i];

        j++;

    }

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

    {

        c[j] = b[i];

        j++;

    }

 

    printf("\nElements of array c:");

    for (i = 0; i < j; i++)

    {

        printf("\nc[%d] = %d", i, c[i]);

    }

 

    getch();

}

 

Output:

Enter the size of an array = 2

 

Enter the elements of 1st array:

a[0] = 1

a[1] = 2

 

Enter the elements of 2nd array:

a[0] = 3

a[1] = 4

 

Elements of array c:

c[0] = 1

c[1] = 2

c[2] = 3

c[3] = 4

 

 

Please follow me for more practicals....

 

8.

Aim: Write a program to read 5 array elements and sort them into ascending order.

Program:

#include <stdio.h>

#include <conio.h>

 

void main()

{

    int arr[50], size, i, j;

 

    printf("Enter the size of an array = ");

    scanf("%d", &size);

 

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

    {

        printf("a[%d] = ", i);

        scanf("%d", &arr[i]);

    }

 

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

    {

        for (j = i; j < size; j++)

        {

            if (arr[i] > arr[j])

            {

                arr[i] = arr[i] + arr[j];

                arr[j] = arr[i] - arr[j];

                arr[i] = arr[i] - arr[j];

            }

        }

    }

 

    printf("\nElements after ascending sort:");

    for (i = 0; i < j; i++)

    {

        printf("\nc[%d] = %d", i, arr[i]);

    }

 

    getch();

}

 

Output:

Enter the size of an array = 5

a[0] = 6

a[1] = 2

a[2] = 3

a[3] = 5

a[4] = 1

 

Elements after ascending sort:

c[0] = 1

c[1] = 2

c[2] = 3

c[3] = 5

c[4] = 6

 

 

Please follow me for more practicals....

 

9.

Aim: Write a program to read 5 array elements and sort them into descending order.

Program:

#include <stdio.h>

#include <conio.h>

 

void main()

{

    int arr[50], size, i, j;

 

    printf("Enter the size of an array = ");

    scanf("%d", &size);

 

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

    {

        printf("a[%d] = ", i);

        scanf("%d", &arr[i]);

    }

 

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

    {

        for (j = i; j < size; j++)

        {

            if (arr[i] < arr[j])

            {

                arr[i] = arr[i] + arr[j];

                arr[j] = arr[i] - arr[j];

                arr[i] = arr[i] - arr[j];

            }

        }

    }

 

    printf("\nElements after descending sort:");

    for (i = 0; i < j; i++)

    {

        printf("\nc[%d] = %d", i, arr[i]);

    }

 

    getch();

}

 

Output:

Enter the size of an array = 5

a[0] = 6

a[1] = 2

a[2] = 3

a[3] = 5

a[4] = 1

 

Elements after descending sort:

c[0] = 6

c[1] = 5

c[2] = 3

c[3] = 2

c[4] = 1

 

 

Please follow me for more practicals....

 

10.

Aim: Write a program to declare 2-D array and assign the value to array elements statically.

Program:

#include <stdio.h>

#include <conio.h>

 

void main()

{

    int arr[2][3] = {{1, 2, 3}, {4, 5, 6}}, i, j;

 

    for (i = 0; i < 2; i++)

    {

        for (j = 0; j < 3; j++)

        {

            printf("%d ", arr[i][j]);

        }

        printf("\n");

    }

 

    getch();

}

 

Output:

1 2 3

4 5 6

 

 

Please follow me for more practicals....

 

11.

Aim: Write a program to read 2-D array from user and print it dynamically.

Program:

#include <stdio.h>

#include <conio.h>

 

void main()

{

    int arr[50][50], i, j, rowSize, columnSize;

 

    printf("\nEnter the row size = ");

    scanf("%d", &rowSize);

    printf("Enter the column size = ");

    scanf("%d", &columnSize);

 

    printf("\nEnter the array elements:\n");

    for (i = 0; i < rowSize; i++)

    {

        for (j = 0; j < columnSize; j++)

        {

            printf("arr[%d][%d] = ", i, j);

            scanf("%d", &arr[i][j]);

        }

    }

 

    printf("\nArray elements:\n");

    for (i = 0; i < rowSize; i++)

    {

        for (j = 0; j < columnSize; j++)

        {

            printf("%d ", arr[i][j]);

        }

        printf("\n");

    }

 

    getch();

}

 

Output:

Enter the row size = 2

Enter the column size = 3

 

Enter the array elements:

arr[0][0] = 1

arr[0][1] = 2

arr[0][2] = 3

arr[1][0] = 4

arr[1][1] = 5

arr[1][2] = 6

 

Array elements:

1 2 3

4 5 6

 

 

Please follow me for more practicals....

 

12.

Aim: Write a program to accept the value in 2-D array a[][] and display the sum of all the elements.

Program:

#include <stdio.h>

#include <conio.h>

 

void main()

{

    int a[50][50], i, j, rowSize, columnSize, sum = 0;

 

    printf("Enter the row size = ");

    scanf("%d", &rowSize);

    printf("Enter the column size = ");

    scanf("%d", &columnSize);

 

    printf("\nEnter the array elements:\n");

    for (i = 0; i < rowSize; i++)

    {

        for (j = 0; j < columnSize; j++)

        {

            printf("arr[%d][%d] = ", i, j);

            scanf("%d", &a[i][j]);

        }

    }

 

    for (i = 0; i < rowSize; i++)

    {

        for (j = 0; j < columnSize; j++)

        {

            sum += a[i][j];

        }

    }

 

    printf("\nSum of elements = %d", sum);

 

    getch();

}

 

Output:

Enter the row size = 2

Enter the column size = 2

 

Enter the array elements:

arr[0][0] = 1

arr[0][1] = 2

arr[1][0] = 3

arr[1][1] = 4

 

Sum of elements = 10

 

 

Please follow me for more practicals....

 

13.

Aim: Write a program to add two Matrix.

Program:

#include <stdio.h>

#include <conio.h>

 

void main()

{

    int arr1[50][50], arr2[50][50], arr3[100][100], rowSize, columnSize, i, j;

 

    printf("Enter the row size = ");

    scanf("%d", &rowSize);

    printf("Enter the column size = ");

    scanf("%d", &columnSize);

 

    printf("\nEnter the 1st array elements:\n");

    for (i = 0; i < rowSize; i++)

    {

        for (j = 0; j < columnSize; j++)

        {

            printf("arr[%d][%d] = ", i, j);

            scanf("%d", &arr1[i][j]);

        }

    }

    printf("\nEnter the 2nd array elements:\n");

    for (i = 0; i < rowSize; i++)

    {

        for (j = 0; j < columnSize; j++)

        {

            printf("arr[%d][%d] = ", i, j);

            scanf("%d", &arr2[i][j]);

        }

    }

 

    for (i = 0; i < rowSize; i++)

    {

        for (j = 0; j < columnSize; j++)

        {

            arr3[i][j] = arr1[i][j] + arr2[i][j];

        }

    }

 

    printf("\nSum of 1st and 2nd array:\n");

    for (i = 0; i < rowSize; i++)

    {

        for (j = 0; j < columnSize; j++)

        {

            printf("%d ", arr3[i][j]);

        }

        printf("\n");

    }

    getch();

}

 

Output:

Enter the row size = 2

Enter the column size = 2

 

Enter the 1st array elements:

arr[0][0] = 1

arr[0][1] = 2

arr[1][0] = 3

arr[1][1] = 4

 

Enter the 2nd array elements:

arr[0][0] = 1

arr[0][1] = 2

arr[1][0] = 3

arr[1][1] = 4

 

Sum of 1st and 2nd array:

2 4

6 8

 

 

Please follow me for more practicals....

 

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.