2075 2

CSC115 ยท 2075 ยท 10 marks

Define array?What are the benefits of using array?Write a program to add two matrices using array.[1+2+7]

Define array?What are the benefits of using array?Write a program to add two matrices using array.[1+2+7]

C ProgrammingChapter 6: ArraysIntroduction to Array

Quick answer

An array is a collection of similar (same data type) elements stored in contiguous memory locations, referred to by a single variable name and accessed using an index (subscript).

Array: Definition, Benefits, and Matrix Addition Program


1. Definition of Array [1 mark]

Syntax:

data_type array_name[size];

Example:

int marks[5];  /* declares an array of 5 integers */

2. Benefits of Using Array [2 marks]

BenefitExplanation
Organized StorageMultiple values of the same type are stored under one variable name, keeping data organized.
Easy AccessAny element can be directly accessed using its index, e.g., arr[2].
Less CodeInstead of declaring many separate variables, a single array declaration handles all values.
Loop FriendlyArrays work efficiently with loops to process large amounts of data quickly.
Supports 2D DataTwo-dimensional arrays allow representation of matrices and tables naturally.

3. Program to Add Two Matrices Using Array [7 marks]

Logic:

  • Take two matrices A and B of the same size (r x c).
  • Add corresponding elements: C[i][j] = A[i][j] + B[i][j]
  • Display the resultant matrix C.

Program:

#include<stdio.h>
#include<conio.h>

void main()
{
    int A[10][10], B[10][10], C[10][10];
    int i, j, r, c;

    clrscr();

    /* Input: number of rows and columns */
    printf("Enter number of rows: ");
    scanf("%d", &r);

    printf("Enter number of columns: ");
    scanf("%d", &c);

    /* Input elements of Matrix A */
    printf("\nEnter elements of Matrix A:\n");
    for(i = 0; i < r; i++)
    {
        for(j = 0; j < c; j++)
        {
            printf("A[%d][%d] = ", i, j);
            scanf("%d", &A[i][j]);
        }
    }

    /* Input elements of Matrix B */
    printf("\nEnter elements of Matrix B:\n");
    for(i = 0; i < r; i++)
    {
        for(j = 0; j < c; j++)
        {
            printf("B[%d][%d] = ", i, j);
            scanf("%d", &B[i][j]);
        }
    }

    /* Addition of two matrices */
    for(i = 0; i < r; i++)
    {
        for(j = 0; j < c; j++)
        {
            C[i][j] = A[i][j] + B[i][j];
        }
    }

    /* Display the resultant matrix C */
    printf("\nResultant Matrix after Addition:\n");
    for(i = 0; i < r; i++)
    {
        for(j = 0; j < c; j++)
        {
            printf("%d\t", C[i][j]);
        }
        printf("\n");
    }

    getch();
}

Sample Output:

Enter number of rows: 2
Enter number of columns: 2

Enter elements of Matrix A:
A[0][0] = 1
A[0][1] = 2
A[1][0] = 3
A[1][1] = 4

Enter elements of Matrix B:
B[0][0] = 5
B[0][1] = 6
B[1][0] = 7
B[1][1] = 8

Resultant Matrix after Addition:
6       8
10      12

Explanation of Key Steps:

StepDescription
int A[10][10]Declares a 2D array to store matrix elements
Nested for loop for inputReads each element row by row using scanf
C[i][j] = A[i][j] + B[i][j]Adds corresponding elements of both matrices
Nested for loop for outputDisplays the result matrix in proper row-column format using printf and \t

Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.