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

Lecture 11

This document discusses arrays in C programming. It defines an array as a group of elements of the same data type that allows storing multiple values in a single variable. Key points include: 1) Arrays allow storing and accessing related data collectively rather than declaring separate variables. 2) Array elements are accessed using an index number and share the same data type. 3) Arrays occupy contiguous blocks of memory, and the size must be specified in the declaration. 4) Example programs demonstrate declaring and initializing an array, accessing elements using indexes, and outputting element values.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Lecture 11

This document discusses arrays in C programming. It defines an array as a group of elements of the same data type that allows storing multiple values in a single variable. Key points include: 1) Arrays allow storing and accessing related data collectively rather than declaring separate variables. 2) Array elements are accessed using an index number and share the same data type. 3) Arrays occupy contiguous blocks of memory, and the size must be specified in the declaration. 4) Example programs demonstrate declaring and initializing an array, accessing elements using indexes, and outputting element values.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Array

• Offers a simple way of grouping like variables for easy access


• It is a group of elements having same data type
• An array is a collective name given to a group of ‘similar quantities’
• Arrays in C share a few common attributes
• Variables in an array share the same name
• Variables in an array share the same data type
• Individual variables in an array are called elements
• Elements in an array are accessed with an index number

1
Cont.

Cout<<“x= ”<<x;

• Ordinary variables are capable of holding only one value at a time


• There are situations in which we would want to store more than
one value at a time in a single variable

2
Cont.
• For example, suppose we want to arrange the percentage marks
obtained by 100 students in ascending order
• In such a case we have two options to store these marks in memory:
• Declare 100 variables to store percentage marks obtained by 100
different students, i.e. each variable containing marks of single student
int m1, m2, m3 ……… m100;
• Declare one variable (called array or subscripted variable) capable of
storing or holding all the hundred values

3
Array declaration

int marks[10];

type Array name size


Like any other variable, arrays occupy memory space
marks [10] Index of elements in array

0 1 2 3 4 5 6 7 8 9

500 504 508 512 516 520 524 528 532 530
Memory address of array elements 4
How to access array elements
int x; int marks[10];
x= 2; marks[0] = 2;
Cout<<x; marks[1] = 3;
Cout<<x; Cout<<marks[2])
Cout<<“marks [2] = ”<< marks[2])
x 43
2 Output
695 x = 43 Output
marks [2] = 16
0 1 2 3 4 5 6 7 8 9
2 3 16
500 504 508 512 516 520 524 528 532 530
5
Points to remember
• Array is a collection of elements having same data type
• Memory allocate to array elements are continuous
• Array size must be mentioned in array declaration ( int marks
[10]; )
• Array index always starts with 0
• In 10 elements array, index of first elements is 0 and index of
last element is 9
• Array element can be access using array index

6
A Simple Program Using Array
• Write a program that take 10 integer from user and then display
those integers

Write a program

7
Array initialization

You might also like