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

Cs112 - Programming Fundamental: Lecture # 27 - Arrays in C Syed Shahrooz Shamim

The document discusses one-dimensional arrays in C, which are fixed size collections of elements of the same data type that can be accessed using an index. It provides examples of array declarations and initialization, both at compile time by specifying values between curly braces and at run time by assigning values within a for loop. Arrays can represent lists of items like employee data, test scores, or student information.

Uploaded by

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

Cs112 - Programming Fundamental: Lecture # 27 - Arrays in C Syed Shahrooz Shamim

The document discusses one-dimensional arrays in C, which are fixed size collections of elements of the same data type that can be accessed using an index. It provides examples of array declarations and initialization, both at compile time by specifying values between curly braces and at run time by assigning values within a for loop. Arrays can represent lists of items like employee data, test scores, or student information.

Uploaded by

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

CS112 - PROGRAMMING FUNDAMENTAL

Lecture # 27 – Arrays in C
Syed Shahrooz Shamim
Junior Lecturer,
CS Department, UIT
What is Array?
• An array is a fixed-size sequential
collection of elements of same data types
that share a common name.
• It is simply a group of data types.
• An array is a derived data type.
• An array is used to represent a list of
numbers , or a list of names.
Example of Arrays
1. List of employees in an organization.
2. Test scores of a class of students.
3. List of customers and their telephone numbers.
4. List of students in the college.
– For Example, to represent 100 students in
college, can be written as
student [100]
– Here student is a array name and [100] is
called index or subscript.
Types of Arrays
1. One-dimensional arrays
2. Two-dimensional arrays
3. Multidimensional arrays
One-Dimensional Arrays
• A variable which represent the list of items using
only one index (subscript) is called one-
dimensional array.

• For Example , if we want to represent a set of five


numbers say(35,40,20,57,19), by an array variable
number, then number is declared as follows
int number [5] ;
One-Dimensional Arrays
• and the computer store these numbers as shown
below :
number [0]
number [1]
number [2]
number [3]
number [4]
• The values can be assigned to the array as follows :
number [0] = 35;
number [1] = 40;
number [2] = 20;
number [3] = 57;
number [4] = 19;
Declaration Of One-Dimensional Arrays

• The general form of array declaration is :


type array-name[size];
• Here the type specifies the data type of elements
contained in the array, such as int, float, or char.
• And the size indicates the maximum numbers of
elements that can be stored inside the array.
• The size should be either a numeric constant or a
symbolic constant.
Declaration Of One-Dimensional Arrays
Num[0] =data1
Data_type var_name[Expression]
Num[1] =data2

Num[2] =data3
Data Type is the type of elements to be stored in the array
Num[3] =data4

Var_name is the name of the array like any Num[4] =data5


Other variables
Num[5] =data6

Expression specifies the number of elements to be stored Num[6] =data7


In array
Num[7] =data8

Example int num[10]; Num[8] =data9

Num[9] =data10
Accessing One Dimensional Array Elements
#<Include<stdio.h>
#include<conio.h> OUTPUT
Arrray declaration
Void main()
{ Enter the array
Int a[10];
Clrscr(); 1 2 3 4from
Taking values 5 6 7user
8 9 10
Printf (“enter the array”);
For (i=0;i<10;i++)
{ Entered array is
Scanf(“%d”,&a[i]); 1
} Printing the values
2
Printf(“the entered array is”); 3
For(i=0;i<10;i++) 4
{ 5
printf(“%d”,a[i]);} 6
} 7
getch(); 8
} 9
10
9/24/2019
Example

int group [10] ;

• here int is type, group is a variable name , 10 is a


size of array and the subscripts (index) is start
from 0 to 9.
Initialization Of One-Dimensional Arrays

• An array can be stored in following stages :


1. At compile time
2. At run time
Compile time initialization
• In compile time initialization, the array is
initialized when they are declared.
• The general form of initialization of array is :
type array-name[size] = { list of values };
• The list of values are separated by commas.
Initialization Of One-Dimensional Arrays
Example:
int number[3] = {4,5,9};

• Here array number of size 3 and will assign 4 to


first element(number[0]), 5 is assign with second
element(number[1]) and 9 is assign with third
element(number[2]).

• If the number of values in the list is less than the


number of elements, then only that many elements
will be intialized. The remaining elements will be
set to zero automatically.
Initialization Of One-Dimensional Arrays
Example:
int number[ ] = {1,2,3,4};

• The character array can be initialized as follows :

char name[ ] = {‘j’,’o’,’h’,’n’,’\0’};

• The character array can also be initialized as


follows :
char name[ ] = “john”;
Initialization Of One-Dimensional Arrays
Run time initialization:
• In run time initialization, the array is explicitly initialize at
run time.
• This concept generally used for initializing large arrays.
• Example:
for(i=0; i < 100; i++)
{
if( i < 50)
sum[i] = 0.0;
else
sum[i] = 1.0;
}
• Here first 50 elements of the array sum are initialized to 0
and the remaining 50 elements are initialized to 1 at run
time.

You might also like