basics of one dimensional array
TRANSCRIPT
Arrays - One Dimensional
1
Instructor
Dhiviya Rose J , AP-Sr. Scale | SoCSEUniversity of Petroleum and Energy Studies
INFO 106 – Computer Programming
Recap
2
INFO 106 – Computer Programming
Variables Vs Array3
int a,b;
a=10;b=20;c=30;
int marks[5];
marks[0]=51;marks[1]=62;marks[2]=43;marks[3]=74;marks[4]=55;
INFO 106 – Computer Programming
IndexDefinition of Arrays.
Types of Arrays (1-D and 2-D)
1-D Arrays:
-Array Declaration.
-Accessing Elements of an array.
-Entering Data into an array.
-Reading data from an array.
-Array Initialization.
-Array Elements in Memory.
4
INFO 106 – Computer Programming
Introduction•Arrays
▫Structures of related data items▫Static entity – same size throughout
program▫Derived data types
▫Group of consecutive memory locations ▫Same name and data type
5
INFO 106 – Computer Programming
•To refer to an element, specify▫Array name▫Position number/ Index
•Format:arrayname[ position number ]
▫First element at position 0▫n element array named c:
c[ 0 ], c[ 1 ]...c[ n – 1 ]
6
Name of array (All elements of the array have the same name, c)
Position number or Index of the element within array c
c[6]
-4560
721543-89
062-31
645378
c[0]c[1]c[2]c[3]
c[11]c[10]c[9]c[8]c[7]
c[5]c[4]
7
int marks[5];
marks[0]=51;marks[1]=62;marks[2]=43;marks[3]=74;marks[4]=55;
Array Name= ?Array Size=?Index Range=?No of Elements in the Array=?
INFO 106 – Computer Programming
Characteristics of Array8
INFO 106 – Computer Programming
•Array elements are like normal variables
c[0] = 3;printf( "%d", c[0] );
▫Perform operations in subscript. c[0] = 10;C[1] = 20;C[2] = c[0] + c[1]printf( “%d”, c[2] );
9
INFO 106 – Computer Programming
Types of Arrays•One Dimensional Array (1D)
•Two Dimensional Array (2D)
•Multi Dimensional Array
10
INFO 106 – Computer Programming
Declaring Arrays – 1D•When declaring arrays, specify
▫Name▫Type of array▫Number of elementsarrayType arrayName[ numberOfElements ];
▫Examples:int c[10]; float myArray[20];
11
INFO 106 – Computer Programming
Initializing Arrays – 1D• Initializers
int n[5] = { 1, 2, 3, 4, 5 };
▫If not enough initializers, rightmost elements become 0int n[ 5 ] = { 0 }
All elements 0▫If too many a syntax error is produced syntax error▫C arrays have no bounds checking
• If size omitted, initializers determine itint n[ ] = { 1, 2, 3, 4, 5 };
▫5 initializers, therefore 5 element array
12
INFO 106 – Computer Programming
Initializing at Run time - scanfvoid main(){int ans,myarr[2];
printf(“Enter the myarr[0]”);scanf(“%d”,&myarr[0];
printf(“Enter the myarr[1]”);scanf(“%d”,&myarr[1];
ans=myarr[0]+myarr[1];printf(“The added answer is %d”,ans);}
13
INFO 106 – Computer Programming
Accessing elements array using for– 1D
14
INFO 106 – Computer Programming
Application of Arrays•Given a list of test scores, determine the
maximum and minimum scores.
•Read in a list of student names and rearrange them in alphabetical order (sorting).
•Given the height measurements of students in a class, output the names of those students who are taller than average.
15
INFO 106 – Computer Programming
16