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

Arrays: Store A Single Value at A Time Store Roll No. of 100 Students?

An array is a collection of similar data types stored in contiguous memory locations that can be accessed using an index. Arrays allow storing multiple values of the same type rather than just one value. The key advantages of arrays are code optimization, ease of traversal and sorting using loops. Arrays have a fixed size defined at declaration. A two-dimensional array can be defined as an array of arrays to store data in rows and columns, accessed using two indices.

Uploaded by

Mohd Umair
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Arrays: Store A Single Value at A Time Store Roll No. of 100 Students?

An array is a collection of similar data types stored in contiguous memory locations that can be accessed using an index. Arrays allow storing multiple values of the same type rather than just one value. The key advantages of arrays are code optimization, ease of traversal and sorting using loops. Arrays have a fixed size defined at declaration. A two-dimensional array can be defined as an array of arrays to store data in rows and columns, accessed using two indices.

Uploaded by

Mohd Umair
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Arrays

The normal variable allows us to store a single value at a time, what if we want to store roll
no. of 100 students?

For this task, we have to declare 100 variables, then assign values to each of them.

What if there are 10000 students or more?


As you can see declaring that many variables for a single entity (i.e student) is not a good
idea. In a situation like these arrays provide a better way to store data.

Def:An array is defined as the collection of similar type of data items/elements


stored at contiguous memory locations.

Arrays are the derived data type in C programming language which can store the
primitive type of data such as int, char, double, float, etc.

The array is the simplest data structure where each data element can be
randomly accessed by using its index number.
Syntax:  datatype array_name[size];
datatype: It denotes the type of the elements in the array.

array_name: Name of the array. It must be a valid identifier.

size: Number of data elements an array can hold/store.


example of array declarations:
int num[100];
float temp[20];
char ch [50];
 num is an array of type int, which can only store 100 data elements of type int.
 temp is an array of type float, which can only store 20 data elements of type float.
 ch is an array of type char, which can only store 50 data elements of type char.
The individual elements in the array:
num[0], num[1], num[2], ....., num[99]
temp[0], temp[1], temp[2], ....., temp[19]
ch[0], ch[1], ch[2], ....., ch[49]
int   marks[9]; //Array variable which stores 9 data elements of same
//integer data items
Here, int is the datatype, marks are the arrayname, and 9 is the arraysize.

int marks[9] = { 40, 55 , 63, 17 , 22 , 68 , 89 , 97 , 89}

int age= 20; // Normal variable which stores only 1 data element.
Arrays
Def:An array is defined as the collection of similar type of data items stored at
contiguous memory locations.

A contiguous array is just an array stored in an unbroken block of memory, to


access the next value in the array, we just move to the next memory address.
----Example----
int   marks[4]; //Array variable which stores 9 data elements of
same integer data items
Here, int is the datatype, marks are the arrayname, and 4 is the array size.
int marks[4] = { 80, 55 , 75, 67};
marks
Data elements 80 55 75 67
Index values 0 1 2 3
Memory allocation
2000 2002 2004 2006
because int 2 bytes
Few keynotes:
• Arrays have 0 as the first index, not 1. In this
example, marks[0] is the first element.
• If the size of an array is n, to access the last element, the n-
1 index is used. In this example, marks[8]
• Suppose the starting address of marks[0] is 2120d. Then, the
address of the marks[1] will be 2122d. Similarly, the address
of marks[2] will be 2124d and so on.

• This is because the size of a int is 2 bytes.


Declarations and Size of an Array Variables:

int   marks[9]; //Array variable which stores 9 data elements of same integer data items
float salary[5]; //Array variable which stores 5 data elements of same float data items
char name[30]; //Array variable which stores 30 data elements of same characters data items

int sum; // Normal variable which stores only 1 data element

--------------------Note----------------
Memory Sizes: For Normal Variable int – 2 bytes, float – 4 bytes, char – 1
byte
• marks[9] has 9 integer data elements only so 18(9 x 2) bytes of Memory
size it holds.
• X[5] has 5 float data elements only so 20(5 x 4) bytes of Memory size it
holds.
• name[30] has 30 character elements only so 30(30 x 1) bytes of
Memory size it holds.
• sum is one normal variable of integer data element only so 2 bytes of
Memory size it holds.
Declarations and Initialization of an Array Variables:
// Array declaration by initializing elements
1.int x[3]={20,30,5};
2. int arr[] = { 10, 20, 30, 40 };
// Compiler creates an array of size 4.
// above is same as  "int arr [4] = {10, 20, 30, 40}“
3.int arr[7] = { 10, 20, 30, 40 };
 // Compiler creates an array of size 7, initializes first
// 4 elements as specified by user and rest 3 elements as 0.
//above 3. is same as  "int arr[] = {10, 20, 30, 40, 0, 0,0 };"
4. int arr[5];
    arr[0] = 5;
    arr[2] = -10; 5 0 -10 5 0
    arr[3] = arr[0]; 0 1 2 3 4
5. int size=10;
int marks[size];
Declarations and Initialization of an Array Variables:
// Array declaration and initializing /accepting through keyboard
6. int number[5], i=0;
printf(“Enter 5 data elements in an array\n”);
while( i<5)
{
scanf(“%d”, &number[i]); // at i = 0, number[0] = keyboard value

i++; // at i = 1, number[1] = keyboard value


// at i = 2, number[2] = keyboard value
}

number

Data elements 5 -10 5 0 0


Index values 0 1 2 3 4
Simple C Program on Arrays
#include <stdio.h>
void main()
{
    int num[5];//just Declared an Array
clrscr();
    num[0]=25;//Initializing the values into an array
num[1]= -55;
num[2]= 79;
num[3] = 345;
  printf(“%d”, num[2]);//takes index value of 2 and prints 79
printf(“%d”, num[0]); //takes index value of 0 and prints 25
printf(“%d”, num[3]); //takes index value of 3 and prints 345
getch();
}

num
Data elements 25 -55 79 345 0
Index values 0 1 2 3 4
Simple C Program on Arrays
#include <stdio.h>
void main()
{
    // Array declaration by initializing it 
    // with more elements than specified size.
    int m1[] = { 75,80,90 };
    int m2[5] = { 60,70,80,90,75};
  int x=20;
printf(“%d”, x);
printf(“%d”, m1[2]);//takes index value of 2 and prints 90
printf(“%d”, m1[0]); //takes index value of 0 and prints 75
printf(“%d”, m2[2]); //takes index value of 2 and prints 80
} m1
Data elements 75 80 90

Index values 0 1 2

m2
Data elements 60 70 80 90 75
Index values 0 1 2 3 4
Printing array values using LOOPs
#include <stdio.h>
void main()
{
     int num[5];//just Declared an Array
int x=0;
clrscr();
   num[0]=25;//Initializing the values into an array
num[1]= -55;
num[2]= 79;
num[3] = 345;

while(x<5) // Prints 5 values on Monitor/OUTPUT device


{
printf(“ %d\t ”, num[i]);
x++;
}
getch(); num
} Data elements 25 -55 79 345 0

Index values 0 1 2 3 4
Simple C Program on Arrays
#include <stdio.h>
void main()
{
    int num[5];//just Declared an Array

int i=0;
int x=0;
clrscr();
   printf(“Enter 5 elements in to an array \n”);

while(i<5) // Accepting 5 values from Keyboard


{
scanf(“ %d ”, &num[i]);
i++;
}
while(x<5) // Prints 5 values on Monitor/OUTPUT device
{
printf(“ %d\t ”, num[i]);
x++;
}
getch(); num
}
Data elements 70 40 3333 -453 -32

Index values 0 1 2 3 4
Arrays
Advantage of C Array
1) Code Optimization: Less code to the access the data.
2) Ease of traversing: By using the for loop, we can retrieve the
elements of an array easily.
3) Ease of sorting: To sort the elements of the array, we
need a few lines of code only.
4) Random Access: We can access any element randomly using the
array.
Disadvantage of C Array
1) Fixed Size: Whatever size, we define at the time of declaration
of the array, we can't exceed the limit. So, it doesn't grow the size
dynamically like LinkedList which we will learn later.
Arrays are of two types
1. single dimensional array(1D)
2. two dimensional array(2D)
Two Dimensional Array in C
The two-dimensional array can be defined as an array of arrays.
The 2D array is organized as matrices which can be represented
as the collection of rows and columns.

Declaration of two dimensional Array in C


The syntax to declare the 2D array is given below.

data_type array_name[rows][columns];  

Consider the following example.

int  X[3][4];  

Here, 3 is the number of rows, and 4 is the number of columns.


Index Values and the Storage of 2D Array
int  X[3][4];  
Declaration and Initialization of 2D Array 
Initialization of 2D Array in C

In the 1D array, we don't need to specify the size of the array if


the declaration and initialization are being done simultaneously.

However, this will not work with 2D arrays. We will have to


define at least the second dimension of the array.

The two-dimensional array can be declared and defined in the


following way.

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

You might also like