0% found this document useful (0 votes)
21 views

Module 3 Array

An array is a linear data structure that stores elements of the same type contiguously in memory. Arrays allow storing multiple elements of the same type and accessing them using an index. Multi-dimensional arrays extend the concept to multiple indices and can represent matrices. Common string functions like strlen(), strcpy(), strcmp() and strcat() allow manipulating C strings.

Uploaded by

ABHISHEK TR
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Module 3 Array

An array is a linear data structure that stores elements of the same type contiguously in memory. Arrays allow storing multiple elements of the same type and accessing them using an index. Multi-dimensional arrays extend the concept to multiple indices and can represent matrices. Common string functions like strlen(), strcpy(), strcmp() and strcat() allow manipulating C strings.

Uploaded by

ABHISHEK TR
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 52

Introduction:

● An array is a linear and homogeneous data structure


● An array permits homogeneous data.
● It means that similar types of elements are stored contiguously
in the memory under one variable name.
● An array can be declared of any standard or custom data type.
Pictorial representation of C Programming Arrays
Index or Subscript Variable:

● Individual data items can be accessed by the name of the array


and an integer enclosed in square bracket called subscript
variable / index
● Subscript Variables helps us to identify the item number to be
accessed in the contiguous memory
What is Contiguous Memory?

● When Big Block of memory is reserved or allocated then that


memory block is called as Contiguous Memory Block.
● Alternate meaning of Contiguous Memory is continuous memory.
● Suppose inside memory we have reserved 1000-1200 memory
addresses for special purposes then we can say that these 200
blocks are going to reserve contiguous memory.
Contiguous Memory Allocation
1. Two registers are used while implementing the contiguous memory scheme.
These registers are base register and limit register.

2. When process try to refer a part of the memory then it will firstly refer the base
address from base register and then it will refer relative address of memory
location with respect to base address.
Array Terminologies:
Characteristics of an array:
1. The declaration int a [5] is nothing but creation of five variables of integer
types in memory instead of declaring five variables for five values.
2. All the elements of an array share the same name and they are
distinguished from one another with the help of the element number.
3. The element number in an array plays a major role for calling each element.
4. Any particular element of an array can be modified separately without
disturbing the other elements.
5. Any element of an array a[ ] can be assigned or equated to another ordinary
variable or array variable of its type.
6. Array elements are stored in contiguous memory locations.
Types of Array
1. Single Dimensional Array / One Dimensional Array

2. Multi Dimensional Array


Multi Dimensional Array:

1. Array having more than one subscript variable is called Multi-Dimensional


array.
2. Multi Dimensional Array is also called as Matrix.
1. Two Dimensional Array requires Two Subscript Variables
2. Two Dimensional Array stores the values in the form of matrix.
3. One Subscript Variable denotes the “Row” of a matrix.
4. Another Subscript Variable denotes the “Column” of a matrix.
Two Dimensional Arrays
Initialization:
Declaration and use of 2D Arrays:

int a[3][4];
for(i=0;i<row,i++)
for(j=0;j<col,j++) {
printf("%d",a[i][j]); }
Two-Dimensional Arrays: Summary with Sample Example:
Memory Representation:
1. 2-D arrays are stored in contiguous memory location row wise.
2. 3 X 3 Array is shown below in the first Diagram.
3. Consider 3×3 Array is stored in Contiguous memory location which
starts from 4000.
4. Array element a[0][0] will be stored at address 4000 again a[0][1]
will be stored to next memory location i.e. Elements stored row-wise
5. After Elements of First Row are stored in appropriate memory
locations, elements of next row get their corresponding memory
locations.
6. This is integer array so each element requires 2 bytes of memory.
Basic Memory Address Calculation: a[0][1] = a[0][0] + Size of Data Type
Method 1: Initializing all Elements row wise

For initializing 2D Array we need to assign values to each element of an array


using the below syntax.

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


Combine and Initializing 2D Array
Initialize all Array elements but initialization is much straight forward.

All values are assigned sequentially and row-wise

int a[3][2] = {1 , 4 , 5 , 2 , 6 , 5 };
Some Elements could be initialized
int a[3][2] = { { 1 }, { 5 , 2 }, { 6 } };

Uninitialized elements will get default 0 value. In this case we have declared and
initialized 2-D array like this
Accessing 2D Array Elements:
1. To access every 2D array we requires 2 Subscript variables.

2. i – Refers the Row number

3. j – Refers Column Number

4. a[1][0] refers element belonging to first row and zeroth column


How it Works?
1. For Every value of row Subscript , the column Subscript incremented from 0 to
n-1 columns

2. i.e. For Zeroth row it will accept zeroth, first, second column (a[0][0], a[0][1],
a[0][2]) elements

3. In Next Iteration Row number will be incremented by 1 and the column number
again initialized to 0.

4. Accessing 2-D Array: a[i][j] Element From i th Row and j th Column


Example programs for practice

:1. C Program for addition of two matrices

2. C Program to find transpose of 3 X 3 Matrix

3. C Program to Multiply two 3 X 3 Matrices

4. C Program to check whether matrix is magic square or not?


C Program for addition of two matrices
Magic square
String and Character Array
String is a sequence of characters that are treated as a single data item
and terminated by a null character '\0'
C does not support strings as a data type.

A string is actually a one-dimensional array of characters in C language.


String Declaration

char s[5];
Declaring and Initializing a string variables:
#include <stdio.h>

int main () {

char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};

printf("Message for You : %s\n", greeting );

return 0;

}
String Initialization
Read String from the user
To get a value of string variable use the two
different types of formats.

Using scanf() function as: scanf(“%s”, string


variable);

Using gets() function as : gets(string


variable);
scanf() function to read a string.

The scanf() function reads the sequence of characters until it encounters whitespace (space, newline, tab, etc.).

#include <stdio.h>
int main()
{
char name[20];
printf("Enter name: ");
scanf("%s", name);
printf("Your name is %s.", name);
return 0;
}
use fgets() function to read a line of string.

#include <stdio.h>
#define MAX 15

int main()
{
char buf[MAX];
fgets(buf, MAX, stdin);
printf("string is: %s\n", buf);

return 0;
}
How to read a line of text?
use gets() function to read a line of string. And, use puts() to display the string.

#include <stdio.h>
#define MAX 15
void main()
{
char Arr[MAX];
printf("Enter a string: ");
gets(Arr);
printf(" entered String is");
puts(Arr);
//printf("string is: %s\n", Arr);
}
#include<stdio.h>

int main()
{
char text[100];
int i;

printf("Enter any string: ");


gets(text);

printf("Entered string is: ");


for(i=0;text[i]!='\0';i++)
{
printf("%c",text[i]);
}
printf("\n");
return 0;
}
Both fgets() and gets() functions can be used for reading string input from
standard input.

The main difference between fgets() function and gets() function is that
fgets() function allows the user to specify the maximum number of
characters to read
Commonly Used String Functions

● strlen() - calculates the length of a string

● strcpy() - copies a string to another

● strcmp() - compares two strings

● strcat() - concatenates two strings


● strlen() - calculates the length of a string
#include <stdio.h>
#include <string.h>
int main()
{
char len[100];
printf("Enter string: \n ");
gets(len);

// using the %zu format specifier to print size_t


printf("Length of string is = %zu \n",strlen(len));
// printf("Length of string b = %zu \n",strlen(b));

return 0;
}
● strcpy() - copies a string to another

● The strcpy() function copies the string pointed by source (including the

null character) to the destination.

● The strcpy() function also returns the copied string.


● strcpy() - copies a string to another

#include<stdio.h>
#include<string.h>

int main()
{
char str1[100];
char str2[20];
int i;

printf("Enter any string: ");


gets(str1);
// copying str1 to str2
strcpy(str2, str1);
printf("copied string is ");
puts(str2); // C programming
return 0;
}
● strcmp() - compares two strings
The strcmp() function compares the character of both the
strings.
If the first character of both the strings are same, then this
process of comparison will continue until all the characters are
compared or the pointer points to the null character '\0'.
#include <stdio.h>
#include<string.h>
int main()
{
char str1[20]; // declaration of char array
char str2[20]; // declaration of char array
int value; // declaration of integer variable
printf("Enter the first string : ");
scanf("%s",str1);
printf("Enter the second string : ");
scanf("%s",str2);
// comparing both the strings using strcmp() function
value=strcmp(str1,str2);
if(value==0)
printf("strings are same");
else
printf("strings are not same");
return 0;
}
● strcat() - concatenates two strings

The strcat(first_string, second_string) function concatenates two strings


and result is returned to first_string.
#include <stdio.h>
#include<string.h>
int main()
{
char str1[20]; // declaration of char array
char str2[20]; // declaration of char array
int value; // declaration of integer variable
printf("Enter the first string : ");
scanf("%s",str1);
printf("Enter the second string : ");
scanf("%s",str2);
// comparing both the strings using strcmp() function
strcat(str1,str2);
printf("Value of first string is: %s",str1);
return 0;
}

You might also like