Unit 2 (1)
Unit 2 (1)
"We have a list of 1000 students' marks of an integer type. If using the basic data type (int),
we will declare something like the following…"
int main(void)
{
int studMark1, studMark2, studMark3, studMark4, …, …, studMark998,
stuMark999, studMark1000;
…
…
return 0;
}
By using an array, we just declare like this,
int studMark[1000];
The size of the above arrays is 5 which is automatically deduced by the compiler.
return 0;
}
ARRAYS
Single or One Dimensional array is used to represent and store data in a linear form.
Array having only one subscript variable is called One-Dimensional array
It is also called as Single Dimensional Array or Linear Array.
Declaration of one-dimensional array(Single dimensional array)
Syntax:
2 4 34 3 4
a[0] a[1] a[2] a[3] a[4]
The various ways of initializing arrays are as follows:
1. Initializing all elements of array(Complete array initialization)
2. Partial array initialization
3. Initialization without size
4. String initialization
10 20 30 40 50
2. Partial array initialization
If the number of values to be initialized is less than the size of array then it is called as partial array initialization.
In such a case elements are initialized in the order from 0th element.
The remaining elements will be initialized to zero automatically by the compiler.
Example:
int a[5]={10,20};
10 20 0 0 0
char s[5]=”ABCD”;
We can observe that string length is 4,but size is 5 because to store NULL character we need one more location
A B C D \0
/* program to read N elements from keyboard and to
print N elements on screen */
#include<stdio.h>
int main()
{
int i,n,a[10];
printf("enter number of array elements\n");
scanf("%d",&n);
printf("enter array elements\n");
for(i=0; i<n;i++)
{
scanf("%d",&a[i]);
}
printf("array elements are\n");
for(i=0; i<n;i++)
{
printf("%d\t",a[i]);
}
getch();
return 0;
}
Create a programme to print an array's element sum
data_type array_name[m][n];
Where,
data_type: It will tell the type of data that has to be stored in each element.
array_name: It will tell the name of the array with which it will be referenced in the whole program.
m: It is the number of rows.
n: It is the number of columns.
The array will have a total of m x n elements.
For example, the 2D array in x of 20 rows and 10 columns will be declared like this.
int x[20][10];
Initialization of 2D Array in C
Once declared, initializing a 2D array breathes life into it, filling its cells with initial values. This can be done in several ways,
including:
•At declaration, by directly specifying the values in curly braces:
•int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};
•array_name[x][y]
Examples of 2 Dimensional Array in C
Example 1: Iterating Over a 2D Array Output Example:
grid[0][0] = 10
#include<stdio.h> grid[0][1] = 20
int main(){ grid[0][2] = 30
int i=0, j=0; grid[0][3] = 40
int grid[2][4] = {{10, 20, 30, 40}, {15, 25, 35, 45}}; grid[1][0] = 15
// Looping through a 2D array grid[1][1] = 25
for(i = 0; i < 2; i++){ grid[1][2] = 35
for(j = 0; j < 4; j++){ grid[1][3] = 45
printf("grid[%d][%d] = %d \n", i, j, grid[i][j]);
}
}
return 0;
}
Example 2: Filling a 2D Array with User Input and Displaying It
#include <stdio.h> Enter value for matrix[0][0]: 5
void main () Enter value for matrix[0][1]:
{ 10
int matrix[2][4],i,j; Enter value for matrix[0][2]:
for (i = 0; i < 2; i++) 15
{ Enter value for matrix[0][3]:
for (j = 0; j < 4; j++) 20
{ Enter value for matrix[1][0]:
printf("Enter value for matrix[%d][%d]: ", i, j); 25
scanf("%d", &matrix[i][j]); Enter value for matrix[1][1]:
} 30
} Enter value for matrix[1][2]:
printf("\nDisplaying the matrix:\n"); 35
for(i = 0; i < 2; i++) Enter value for matrix[1][3]:
{ 40
for (j = 0; j < 4; j++)
{ Displaying the matrix:
printf("%d\t", matrix[i][j]); 5 10 15 20
} 25 30 35 40
printf("\n");
}
}
Array of Strings
An array of strings can be defined as-
An array of strings is a two-dimensional array of character-type arrays where each character array (string) is null-
terminated.
Declare and Initialize an Array of Strings
To declare an array of strings, you need to declare a two-dimensional array of character types, where the first subscript is
the total number of strings and the second subscript is the maximum size of each string.
To initialize an array of strings, you need to provide the multiple strings inside the double quotes separated by the
commas.
Syntax
char strings [no_of_strings] [max_size_of_each_string];
Example
Let us declare and initialize an array of strings to store the names of 10 computer languages, each with the maximum length
of 15 characters.
char langs [10][15] = { "PYTHON", "JAVASCRIPT", "PHP", "NODE JS", "HTML", "KOTLIN", "C++", "REACT JS",
"RUST", "VBSCRIPT" };
Printing An Array of Strings
A string can be printed using the printf() function with %s format specifier. To print each string of an array of strings, you can
use the for loop till the number of strings.
return 0;
}
How an Array of Strings is Stored in Memory?
We know that each char type occupies 1 byte in the memory. Hence, this array will be allocated a block of 150 bytes. Although
this block is contagious memory locations, each group of 15 bytes constitutes a row.
Assuming that the array is located at the memory address 1000, the logical layout of this array can be shown as in the following
figure −
An Array of Strings with Pointers
To use the memory more efficiently, we can use the pointers. Instead of a 2D char array, we declare a 1D array of "char *" type.
char *langs[10] = {
"PYTHON", "JAVASCRIPT", "PHP",
"NODE JS", "HTML", "KOTLIN", "C++",
"REACT JS", "RUST", "VBSCRIPT"
};
In the 2D array of characters, the strings occupied 150 bytes. As against this, in an array of pointers, the strings occupy far less
number of bytes, as each string is randomly allocated memory as shown below −
#include <stdio.h>
return 0;
}