0% found this document useful (0 votes)
0 views25 pages

Arrays in c

This document provides an overview of arrays and strings in programming, detailing their definitions, declarations, initializations, and examples of usage. It explains single-dimensional and two-dimensional arrays, including how to access and manipulate their elements, as well as string handling and common string library functions. Additionally, it includes programming exercises related to arrays and strings for practical understanding.

Uploaded by

crisnasoby9c
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
0 views25 pages

Arrays in c

This document provides an overview of arrays and strings in programming, detailing their definitions, declarations, initializations, and examples of usage. It explains single-dimensional and two-dimensional arrays, including how to access and manipulate their elements, as well as string handling and common string library functions. Additionally, it includes programming exercises related to arrays and strings for practical understanding.

Uploaded by

crisnasoby9c
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 25

Module 2

ARRAYS & STRINGS

Badharudheen P
Assistant Professor,
Dept. of CSE, MESCE, Kuttippuram
Introduction

❑ Arrays
❑ A collection of elements of the same type that are
referenced by a common name
❑ Compared to the basic data type (int, float & char) it is
an aggregate or derived data type
Arrays
❑ Consider the following case:

"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 studMark0, studMark1, studMark2, ...,


studMark999;
Arrays
❑ Can you imagine how long we have to write the declaration
part by using normal variable declaration?

void main(void)
{
int studMark1, studMark2, studMark3, …,
…, …, studMark995, studMark996,
studMark997, studMark998, studMark999,
studMark1000;


}
Arrays
❑ By using an array, we just declare like this,
int studMark[1000];
❑ This will reserve 1000 contiguous memory locations for
storing the students’ marks.
Arrays
❑ We can use index/subscript to identify each element or
location in the memory.
❑ Hence, if we have an index of jIndex,
studMark[jIndex] would refer to the jIndexth
element in the array of studMark.

❑ For example, studMark[0] will refer to the first element


of the array.
❑ Thus by changing the value of jIndex, we could refer to
any element in the array.
Arrays
❑ Array elements are like normal variables

c[ 0 ] = 3;
printf( "%d", c[ 0 ] );

❑ Perform operations in subscript. If x equals 3

c[ 5 - 2 ] => c[ 3 ] => c[ x ]
Array Declaration
❑ When declaring arrays, specify
❑ Name
❑ Type of array
❑ Number of elements
❑ arrayType arrayName[numberOfElements];

❑ Examples:
❑ int c[ 10 ];
❑ float myArray[ 100 ];

❑ Declaring multiple arrays of same type


❑ int b[ 100 ], x[ 27 ];
Array Initialization
int n[ 5 ] = { 1, 2, 3, 4, 5 };

int n[ 5 ] = { 0 } //All elements 0

❑ If size omitted, initializers determine it


int n[ ] = { 1, 2, 3, 4, 5 };
❑ 5 initializers, therefore 5 element array
Array Example
#include<stdio.h>
void main()
{
int a[10], i;
printf("Enter the array elements: ");
for(i = 0; i < 10; i++)
scanf("%d", &a[i]);

printf("The array elements are: ");


for(i = 0; i < 10; i++)
printf("%d\n", a[i]);
}
Question

1. Write a program to find the average of array elements.


2. Program to print all even numbers in an array
3. Program to print array in reverse order
4. Program to find the position of an element in an array
5. Program to reverse an array
Strings
❑ Otherwise known as character arrays
❑ can be initialized as: char string1[] = "first";
❑ Null character '\0' terminates strings
❑ string1 actually has 6 elements, it is equivalent to
char string1[]={'f','i','r','s','t','\0'};
❑ Can access individual characters
string1[ 3 ] is character ‘s’
❑ Array name is address of array, so & not needed for scanf
scanf( "%s", string1 );
❑ Can write beyond end of array, be careful
Strings
#include <stdio.h>
void main()
{
char string1[ 20 ];
int i;
printf(" Enter a string: ");
scanf( "%s", string1 );
printf( "string1 is: %s\n”, string1);

/* or you can print using a loop */


for ( i = 0; string1[ i ] != '\0'; i++ )
printf( "%c ", string1[ i ] );
}
String Library Functions
❑ strlen: to find the string length

int strlen(str);

❑ For eg:
int size;
char str[20];
size = strlen(str);
String Library Functions
❑ strcat: to concatenate two strings

strcat(str1, str2);
❑ concatenates str2 with str1

❑ strncat: to concatenate with first n characters

strncat(str1, str2, n);


❑ concatenates first n characters of str2 with str1
String Library Functions
❑ strcpy: to copy the content of one string to another
strcpy(dest, source);

❑ strncpy: to copy the first n characters of one string to another


strncpy(dest, source, n);
String Library Functions
❑ strcmp: to compare two strings
int strcmp(str1, str2);

❑ strncmp: to compare the first n characters


int strcmp(str1, str2, n);
Question

1. Write a program to count number of characters in a string


2. Program to count the number of occurrences of a particular
character in a given string. Also print the positions.
3. Program to check whether the given string is palindrome or not
Two Dimensional/2D Arrays
❑ A two dimensional array has two subscripts/indexes.
❑ The 1st subscript refers to the row, and the 2nd, to the column.
❑ Its declaration has the following form:
type array_name[row size][column size];

❑ For examples,
int a[3][4]; // 3 rows and 4 columns
float matrix[20][25]; //20 rows and 25 columns
Two Dimensional/2D Arrays
Column 0 Column 1 Column 2 Column 3

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

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

Column subscript

Array name

Row subscript
Two Dimensional/2D Arrays
❑ Initialization
1 2
int b[ 2 ][ 2 ] = { { 1, 2 }, { 3, 4 } };
3 4

1 0
❑ If not enough, unspecified elements set to zero
int b[ 2 ][ 2 ] = { { 1 }, { 3, 4 } }; 3 4

❑ Referencing elements:
Specify row, then column
printf( "%d", b[ 0 ][ 1 ] );
Two Dimensional/2D Arrays
❑ To assign initial string values for the 2D array

char Name[6][10] = {"Mr. Bean", "Mr. Bush",


"Nicole", "Kidman", "Arnold", "Jodie"};

❑ Here, we can initialize the array with 6 strings, each with


maximum 9 characters long.
Two Dimensional/2D Arrays
Two Dimensional/2D Arrays
#include<stdio.h>
main()
{
char s[5][20];
int i, j;

printf("Enter the strings");


for(i = 0; i <= 4; i++)
scanf("%s", s[i]);

for(i = 0; i <= 4; i++)


printf("%s\n", s[i]);
}
Question

1. Write a program to add two integer matrices


2. Write a program print the position of an element in a 2D array

You might also like