0% found this document useful (0 votes)
15 views7 pages

Data Structures With Competitive Coding1

Dsa notes

Uploaded by

240301370001
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)
15 views7 pages

Data Structures With Competitive Coding1

Dsa notes

Uploaded by

240301370001
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/ 7

Data Structures with

Competitive Coding

By
Dr. Prasanta Kumar Sahoo
Professor, Dept. of CSE, AU
ARRAYS (Linear)
• An array is a collection of elements of the same type that are referenced by
a common name.
• Compared to the basic data type (int, float) it is an aggregate or derived
data type.
• All the elements of an array occupy a set of contiguous memory locations.
• Why need to use array type?
• Consider the following issue:
• "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, ...studMark999
ARRAYS (Linear)
• Can you imagine how long we have to write the declaration part by using
normal variable declaration?
• int main(void)
•{
• int studMark1, studMark2, studMark3, studMark4, …, …, studMark998,
stuMark999, studMark1000;
•…
•…
• return 0;
•}
ARRAYS (Linear)
• A single or one dimensional array declaration has the following form,
• array_element_data_type array_name[array_size];
• Here, array_element_data_type defines the base type of the array,
which is the type of each element in the array.
• array_name is any valid C identifier name that obeys the same rule
for the identifier naming.
• array_size defines how many elements the array will hold.
ARRAY INITIALIZATION

• An array may be initialized at the time of declaration.


• Initialization of an array may take the following form,
• type array_name[size] = {a_list_of_value};
• For example:
• int idNum[7] = {1, 2, 3, 4, 5, 6, 7};
• float fFloatNum[5] = {5.6, 5.7, 5.8, 5.9, 6.1};
• char chVowel[6] = {'a', 'e', 'i', 'o', 'u', '\0'};
• The first line declares an integer array idNum and it immediately assigns the values 1, 2, 3, ..., 7 to
idNum[0], idNum[1], idNum[2],..., idNum[6] respectively.
• The second line assigns the values 5.6 to fFloatNum[0], 5.7 to fFloatNum[1], and so on.
• Similarly the third line assigns the characters 'a' to chVowel[0], 'e' to chVowel[1], and so on. Note again, for
characters we must use the single apostrophe/quote (') to enclose them.
• Also, the last character in chVowel is NULL character ('\0').
TWO DIMENSIONAL/2D ARRAYS
• A two dimensional array has two subscripts/indexes.
• The first subscript refers to the row, and the second, to the column.
• Its declaration has the following form,
• data_type array_name[1st dimension size][2nd dimension size];
• For examples,
• int xInteger[3][4];
• float matrixNum[20][25];
• The first line declares xInteger as an integer array with 3 rows and 4 columns.
• Second line declares a matrixNum as a floating-point array with 20 rows and
25 columns.
THINGS THAT YOU MUST
CONSIDER WHILE INITIALIZING A
2D ARRAY
• We already know, when we initialize a normal array (or you can say one dimensional
array) during declaration, we need not to specify the size of it. However that’s not the
case with 2D array, you must always specify the second dimension even if you are
specifying elements during the declaration.
• /* Valid declaration*/
• int abc[2][2] = {1, 2, 3 ,4 }
• /* Valid declaration*/
• int abc[][2] = {1, 2, 3 ,4 }
• /* Invalid declaration – you must specify second dimension*/
• int abc[][] = {1, 2, 3 ,4 }
• /* Invalid because of the same reason mentioned above*/
• int abc[2][] = {1, 2, 3 ,4 }

You might also like