Array in C++ and 3-tuple representation of sparse Matrics

One-Dimensional Arrays

An array is essentially a finite ordered set of similar elements stored in contiguous memory locations. The simplest form is a one-dimensional array, declared with the general syntax:

data_type array_name[expression];

Examples of one-dimensional arrays:

int a[4] = {34, 60, 93, 2};
int b[] = {2, 3, 4, 5};
float c[] = {-4, 6, 81, -60};

Strings, a common array type, are arrays of characters terminated by a null character '\0'. A string constant is a one-dimensional array of characters ending with '\0'.

char str[] = "sentence\n";

Each character in a character array occupies one byte of memory, with the last character always being '\0'.

Multidimensional Arrays

The syntax for declaring a multidimensional array in C is:

data_type array_name[expr1][expr2] ... [exprn];

A two-dimensional array of size 3 × 5 is visualized as:

Row 0     a[0][0]   a[0][1]   a[0][2]   a[0][3]   a[0][4] 
Row 1     a[1][0]   a[1][1]   a[1][2]   a[1][3]   a[1][4] 
Row 2     a[2][0]   a[2][1]   a[2][2]   a[2][3]   a[2][4]

Arrays and Pointers

Consider the array declaration char p[10];. The addresses of the array and its first element are identical (p and &p[0]).

int *x, a[10];
x = a;
x[5] = 100;
*(x+5) = 100;

Both assignment statements place the value 100 in the sixth element of array a.

Sparse Matrices and 3-Tuple Representation

Matrices with numerous zero entries are called sparse matrices. A 3-tuple representation provides a concise way to represent them. For example, a 7 × 6 matrix:

       0    1    2    3    4    5
0      0    0    0    5    0    0
1      0    5    0    0    0    0
2      0    0    0    0    9    0
3      0    3    0    2    0    0
4      1    0    2    0    0    0
5      0    0    0    0    0    0
6      0    0    8    0    0    0

Is represented in 3-tuple form as:

7, 6, 8
0, 3, 5
1, 1, 4
2, 4, 9
3, 1, 3
3, 3, 2
4, 0, 1
4, 2, 2
6, 2, 8

Implementation in C

Here's a simple program in C that accepts a matrix as input and prints its 3-tuple representation:

#include <stdio.h>
void main() {
    int a[5][5], rows, columns, i, j, nz = 0;
    printf("Enter the order of the matrix (less than 5x5):\n");
    scanf("%d %d", &rows, &columns);
    printf("Enter the elements of the matrix:\n");
    for (i = 0; i < rows; i++)
        for (j = 0; j < columns; j++) {
            scanf("%d", &a[i][j]);
        }
    printf("The 3-tuple representation of the matrix is:\n");
    for (i = 0; i < rows; i++)
        for (j = 0; j < columns; j++) {
            if (a[i][j] != 0) {
                nz++;
            }
        }
    printf("%d %d %d\n", rows, columns, nz);
    for (i = 0; i < rows; i++)
        for (j = 0; j < columns; j++) {
            if (a[i][j] != 0) {
                printf("%d %d %d\n", i, j, a[i][j]);
            }
        }
}

This program accepts matrix input and prints its 3-tuple representation.

Applications of Arrays

Arrays are versatile and widely used in various scenarios:

  • Situations with a fixed number of items.
  • Ease of traversal, searching, and sorting.
  • Efficient manipulation compared to other data structures.
  • Appropriate for situations where the size can be predetermined.
  • Useful when insertions and deletions are minimal or not present, as they can lead to memory wastage or increased time complexity.

In essence, arrays serve as simple yet reliable solutions in a multitude of scenarios.

Next Post Previous Post
No Comment
Add Comment
comment url