Download presentation
Presentation is loading. Please wait.
Published byNorma Hensley Modified over 8 years ago
1
Consultation Hours
2
Mubashir: – Tuesday from 12:30 to 1:30 with ease of Students. Zohaib – Wedneday b/w 9:30 -10:30 Location: TA Room (next to HOD Office)
3
Arrays
4
There are situations in which we would want to store more than one value at a time in a single variable. For example, suppose we wish to arrange the percentage marks obtained by 100 students in ascending order. – Construct 100 variables to store percentage marks obtained by 100 different students, i.e. each variable containing one student’s marks. – Construct one variable (called array or subscripted variable) capable of storing or holding all the hundred values.
5
it would be much easier to handle one variable than handling 100 different variables. Moreover, there are certain logics that cannot be dealt with, without the use of an array.
6
Array Declaration and Initialization in C int num[6]={2,4,12,5,45,5}; int n[]={2,4,12,5,45,5}; float p[]={1.5,2.5,3}; int k[3]; k[0]=1; k[1]=2; k[2]=3; type variable_name[lengthofarray]; double height[10]; float width[20]; int min[9]; char name[20]; All elements of the array should be of the same type In C Language, arrays starts at position 0.
7
Array Declaration and Initialization The elements of the array occupy adjacent locations in memory.
9
9 Arrays - No bounds checking // An incorrect program. Do Not Execute! int main() { int crash[10], i; for(i=0; i<100; i++) crash[i]=i; return 1; }
10
Linear/Sequential Search int a[]={1,2,5,4,7}; int key=4,n=5,flag=0; for(i=0; i<= n-1; i++){ if(a[i] == key){ flag=1; break; } if(flag == 0) printf("\nThe number is not in the list"); else printf("\nThe number is found");
11
Finding Min and Max Number // start min and max off as equal to the first number min = nums[0]; max = nums[0]; // iterate through nums int i; for(i = 1; i < nums_length; ++i) { // update max, if necessary if( nums[i] > max ) { max = nums[i]; } // update min, if necessary if(nums[i] < min) { min = nums[i]; }
12
Bubble Sort
15
Third Iteration
18
Bubble Sort for(i=0; i<n-1; i++){ // number of iterations for(j=0; j<(n-i-1); j++){ // number of pair wise comparisons if(array[j]>array[j+1]){ temp = array[j+1]; array[j+1] = array[j]; array[j] = temp; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.