0% found this document useful (0 votes)
29 views34 pages

Arrays

Arrays allow storing multiple values in a single variable by allocating consecutive memory locations; arrays must contain elements of the same data type while ordinary variables store one value at a time; and multi-dimensional arrays represent data in a table format accessed using multiple indices unlike single-dimensional arrays which

Uploaded by

ranjitit1984
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views34 pages

Arrays

Arrays allow storing multiple values in a single variable by allocating consecutive memory locations; arrays must contain elements of the same data type while ordinary variables store one value at a time; and multi-dimensional arrays represent data in a table format accessed using multiple indices unlike single-dimensional arrays which

Uploaded by

ranjitit1984
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 34

What is an array?

An array is a consecutive series of variables that share


one variable name

The individual data items in an array must all be of the


same data type, accessed using an index

Often used when dealing with multiple data items


possessing common characteristics

Ex. 24 hourly temperature readings might be stored in


array named temperature
Difference Between Ordinary Variable & Arrays

Ordinary variable store one value at a time

Arrays are used for storing more than one value at a


time in a single variable name
1-D Array Declaration
To declare a 1-D Array, we specify 1 size
data_type array_name [size];

For Eg.
int number[30];
float marks[60];
char name[15];
Single Integer

a=5; 2005 5
integer Array
1 2 3 4 7 12 13 15 89 ……….

[2001] [2003] [2005] [2007] [2009] [2011] [2013] [2015] [2017]


………
int my_array[20]={1,2,3,4,7,12,13,15,89};

These are the memory locations in


which integer type data elements are
stored.
char a;

a= ‘5’; 5
2006
Character Array
N I T R K L \0

[2001] [2002] [2003] [2004] [2005] [2006] [2007]

char college_name[7]={‘N’,’I’,’T’,’R’,’k’,’l’,’\0’};

Each character occupy 1 byte in memory.


Offset
Address int x[4];
x[2]=23;

342901 ? 0 X Identifier

342905 ? 1

342909 23 2

342913 ? 3

Value
Another array example

//Find the lowest number in the array num[10]


main()
{
int num[10]={4,9,11,2,13,77,8,19,21,1};
int min, i;
min = num[0];
for (i=1; i<10; i++) {
if (num[i] < min)
min = num[i];
}
printf(“The minimum is %d”, min);
}
Array example
/* which type of integer is valid in array declaration*/
#include <stdio.h>
int main(void)
{
int x[5]; /* x[5] is array initialization /

x[0]=23; /* valid */
x[2.3]=5; /* invalid: index is not an int */
return 0;
}
Examples
int x [5] = { 1,2,3,4,5 }; size 10 bytes
creates array with elements 0-4 values 1-5

int x [5] = {4,3}; size 10 bytes


creates array with elements 0-4 values 4,3,0,0,0

int x [ ] = {1,2,3}; size 6 bytes


creates array with elements 0-2 values 1,2,3

char c[4] = {‘M’, ‘o’ , ‘o’ }; size 4 bytes


creates array with elements 0-3 values M o o NULL
Dimensionality
Determined by the number of subscripts of an
array.

• x[i]

• y[i][j]

• z[i][j][k]
Some more tips..
double a[n];
× Size can not be a Variable

int a[-5.2], a[-6]


× index always +ve integer

Convinient to use symbolic constants


#define N 100
int array_num[N];
Single vs multidimensional
Represented by one row of Represented by a table of data
data

Declaration: Declaration:
datatype name_of_array[20]; datatype name_of_array[20][20];

Initialization: Initialization:
name_of_array[20]={1,2,3,4……16}; name_of_array[20][20]={{1,2,3,4},
{2,3,4,5},{5,6,7,8}};
Examples
• Assuming we have the following array b:
0 1

0 1 0
1 3 4

printf ("%d", b[0][0]); /* prints 1 */


printf ("%d", b[1][0]); /* prints 3 */
printf ("%d", b[1][1]); /* prints 4 */
2D-Array example
/*print Even and Odd no’s among 3x3 matrix/
#include<stdio.h>
main( )
{
int x, i, j, a[i][j], even=0, odd=1;
for( i=0;i<3; i++ )
{
for(j=0;j<3;j++ )
{
Continue …
printf( “enter a no”);
scanf ( “%d”, &a[i][j]);

if( a[i][j]%2== 0 )
{
printf( “ even= %d”, a[i][j] );
else
printf( “ odd= %d”,a[i][j] );
}
}
What is string?
Array of characters.

‘\0’ in string signifies end of the string in memory.

Strings is enclosed within double quotes.


Initializing Strings
2 ways

char month1[]={‘j’,’a’,’n’,’u’,’a’,’r’,’y’};

char month1[]=“january”;
Example
#include<stdio.h>
main()
{
char strc[]=“this is a string literal”;
printf(“%s”,strc); only name of the array
} format specifier for string
this is a string literal
Array of strings
#include<stdio.h>
const int Num=5;
const int length=9;
main()
{
int j;
char Names[Num][length]={“Tejaswi”,”Prasad”,
”Prashant”,”Anand”, “Priya”};
for(j=0;j<Num;j++) printf(“%s\n”,Names[j]);
Output:
Tejaswi
Prasad
Prasanth
Anand
Input/output
gets(string) performs input operation .

puts(string) performs output operation.


Example
#include<string.h>
#include<stdio.h>
main()
{ char numstr[20];
puts("enter the string");
gets(numstr);
puts(numstr);
}
String functions

strlen( ) strcmp( ) strcpy( ) strcat( )

Read This:
strncpy()
strncat()
strstr()
#include<stdio.h>
main()
{
char ar[20]=“Arvind”;
printf(“%d”,strlen(ar));
}
#include<stdio.h>
#include<string.h>
void main()
{ int i;
char name[20]=“home";
char name1[20]="sweet";
i=strcmp(name,name1); // name<name1
printf("%d”,i);
}

-11
Example
#include<stdio.h>
#include<string.h>
void main()
{ char name1[20];
char name2[20]=“harry”;
strcpy(name1,name2);
printf(“%s”,name1);
}

harry
Example
#include<stdio.h>
#include<string.h>
main( )
{int i;
char name1[20]="home";
char name2[20]="sweet";
strcat(name2,name1);
printf("%s",name1); }
sweethome
Assignments - IV
WAP to insert one integer element at a given position in
the array of integers

WAP to delete a particular element from a group of


elements, elements are arranged in an array

WAP to copy one string into another and count the


number of characters copied.

WAP that would sort a list of names in alphabetical order


WAP ,that will read and store the details of a list
of students in the format
Roll No. Name Marks obtained
23 Ankit 45
24 Priya 55
produce the following output
(a) Alphabetical list of names, roll numbers and
marks obtained
(b) List sorted on roll numbers
(c) List sorted on marks(rank wise list)
Write a program to create a directory of students
with roll numbers. The program should display
the roll number for a specified name and vice-
versa

Write a program to replace a particular word by


another word in a given string, for example in the
sentence “ I m a good student”, replace “good”
with GOOD.

You might also like