MCS-011 Problem Solving and Programming

Admin | First year, Semester1

Chapters

Newsletter

Array Subscripts

A subscript is used to refer to individual elements in an array, ranging from 0 to SIZE-1.

Example:

char country[] = "India";

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


Array Refrences:


Element no.Subscriptcountry Arrayvaluestud Array

value

10country[0]
'I'stud[0]
1
21country[1]
'n'stud[1]
2
32country[2]
'd'stud[2]
3
43country[3]
'i'stud[3]
4
54country[4]
'a'stud[4]
5


Subscripts provide a way to access and manipulate individual elements within an array.

Array Declaration

Key Features of Arrays:

  • Definition: Arrays are data structures that store a group of elements of the same data type.
  • Naming: All elements share the same name, differentiated by an index.
  • Access: Elements can be accessed randomly using a numeric index.
  • Simplicity: Arrays are simple and have been used for decades, offering great utility.
  • ADT List: Frequently associated with the Abstract Data Type (ADT) list.


Array Declaration

Syntax: data-type array_name [constant-size];  where, Data-type: The type of elements stored and Constant-size: The number of elements.


Examples:

  1. int num[80];
  2. float farr[500];
  3. static int iarr[80];


Restrictions:

  1. Fixed Size: The storage amount must be specified at compile time
  2. Homogeneity: All elements in an array must be of the same data type.


Size Specification

Use symbolic constants for array sizes for easier modifications. Example:

#define SIZE 50

int arr[SIZE];

Array Initialization

Array Initialization in C

Arrays can be initialized at the time of declaration with values enclosed in braces and separated by commas.

Syntax. data-type array-name[size] = {val1, val2, ..., valn}; where,

val1: Value for the first element, val2: Value for the second element, valn: Value for the nth element.

Examples:

  1. int digits[10] = {1,2,3,4,5,6,7,8,9,10};
  2. int digits[] = {1,2,3,4,5,6,7,8,9,10};
  3. float temperature[10] = {31.2, 22.3, 41.4, 33.2, 23.3, 32.3, 41.1, 10.8, 11.3, 42.3}


Character Array Initialization:

Character arrays are used for strings in C, automatically including a null character \0 at the end.

Examples:

char thing[4] = "TIN"; // Equivalent to {'T', 'I', 'N', '\0'}

char thing[] = "TIN";  // Automatically includes '\0'


Initialization at declaration simplifies assigning values to arrays and handles strings with null termination automatically.

Array Subscripts

A subscript is used to refer to individual elements in an array, ranging from 0 to SIZE-1.

Example:

char country[] = "India";

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


Array Refrences:


Element no.Subscriptcountry Arrayvaluestud Array

value

10country[0]
'I'stud[0]
1
21country[1]
'n'stud[1]
2
32country[2]
'd'stud[2]
3
43country[3]
'i'stud[3]
4
54country[4]
'a'stud[4]
5


Subscripts provide a way to access and manipulate individual elements within an array.

Processing the Arrays

In certain applications, arrays need initial values and can be defined globally or as static local arrays. The provided example demonstrates summing marks from two subjects for three students and calculating their averages.

Example:

/* Program to display the average marks of 3 students */ 

#include <stdio.h>

#define SIZE 3

main() {

    int i;

    float stud_marks1[SIZE], stud_marks2[SIZE], total_marks[SIZE], avg[SIZE];

    // Input marks for subject 1

    printf("\n Enter the marks in subject-1 out of 50 marks: \n");

    for(i = 0; i < SIZE; i++) {

        printf("Student no. =%d Enter the marks= ", i+1);

        scanf("%f", &stud_marks1[i]);

    }

    // Input marks for subject 2

    printf("\n Enter the marks in subject-2 out of 50 marks \n");

    for(i = 0; i < SIZE; i++) {

        printf("Student no. =%d Please enter the marks= ", i+1);

        scanf("%f", &stud_marks2[i]);

    }

    // Calculate and display averages

    for(i = 0; i < SIZE; i++) {

        total_marks[i] = stud_marks1[i] + stud_marks2[i];

        avg[i] = total_marks[i] / 2;

        printf("Student no.=%d, Average= %f\n", i+1, avg[i]);

    }

}


O/p:

Enter the marks in subject-1 out of 50 marks:

Student no. = 1 Enter the marks= 23

Student no. = 2 Enter the marks= 35

Student no. = 3 Enter the marks= 42

Enter the marks in subject-2 out of 50 marks:

Student no. = 1 Enter the marks= 31

Student no. = 2 Enter the marks= 35

Student no. = 3 Enter the marks= 40

Student no. = 1 Average= 27.000000

Student no. = 2 Average= 35.000000

Student no. = 3 Average= 41.000000

Multi-dimensional Arrays

Multi-dimensional arrays are arrays with more than one dimension, used to represent data in a grid or table format.

Syntax:

datatype array_name[size1][size2]...[sizeN];

where datatype is the type of elements (e.g., int, double), and size1, size2, ..., sizeN are the sizes of each dimension.

Example:

An 8x8 grid can be represented using a two-dimensional array, similar to algebraic notation in chess.

Note: Arrays can have multiple dimensions, but higher dimensions increase memory usage. For instance, a 20x20x20x20 array of double-precision numbers uses about 1.28 MB.


Two-Dimensional Arrays

Two-dimensional arrays use two indices, e.g., array[rows][columns]. The upper left element is array[0][0], the next is array[0][1], etc. Multi-dimensional arrays are stored linearly in memory, with the last index varying fastest.

Syntax for a two-dimensional array: datatype array_name[size1][size2];. Example for an 8x8 integer array: int chessboard[8][8];

Example: 

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


Results in:

table[0][0] = 1;

table[0][1] = 2;

table[0][2] = 3;

table[1][0] = 4;

table[1][1] = 0;

table[1][2] = 0;


No Bounds Checking: C does not check array bounds, so accessing out-of-bounds elements can cause errors. It is the programmer's responsibility to ensure valid indices are used.

About John Doe

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

Report an issue

Related Posts

3 Comments

John Doe

5 min ago

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

Reply

John Doe

5 min ago

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

Reply