1 / 30

Arrays

Learn about arrays, how they help organize large amounts of information, and how to use them effectively. Explore array declaration, initialization, input/output, and array processing. Understand one-dimensional and two-dimensional arrays.

hmatthews
Download Presentation

Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Arrays

  2. Arrays Arrays are objects that help us organize large amounts of information

  3. The entire array has a single name Each value has a numeric index scores 79 87 94 82 67 98 87 81 74 91 Arrays • An array is an ordered list of values 0 1 2 3 4 5 6 7 8 9 An array of size N is indexed from zero to N-1 This array holds 10 values that are indexed from 0 to 9

  4. Arrays • A particular value in an array is referenced using the array name followed by the index in brackets • For example, the expression scores[2] refers to the value 94 (the 3rd value in the array) • That expression represents a place to store a single integer and can be used wherever an integer variable can be used

  5. Arrays • For example, an array element can be assigned a value, printed, or used in a calculation: scores[2] = 89; scores[first] = scores[first] + 2; mean = (scores[0] + scores[1])/2; printf ("Top = %d”, scores[5]);

  6. Arrays • The values held in an array are called array elements • The element type can be a primitive type or a structure. • Therefore, we can create an array of integers, or an array of characters, or an array of String objects, etc.

  7. Declaring Arrays • The scores array could be declared as follows: int scores[10] ; • The type of the variable scores is an array of integers • The variable scores is set to a new blank array that can hold 10 integers

  8. Declaring Arrays • Some examples of array declarations: double prices[500] ; int factor[12] , age[6]; char codes[30] ;

  9. Bounds Checking • Once an array is created, it has a fixed size • That is, the index value must be in bounds (0 to N-1)

  10. problem Bounds Checking • For example, if the array codes can hold 100 values, it can be indexed using only the numbers 0 to 99 • If count has the value 100, then the following reference will cause a problem: for (int index=0; index <= 100; index++) codes[index] = index*50 + epsilon;

  11. Initializer Lists • An initializer list can be used to instantiate and initialize an array in one step • Examples: int units[] = {147, 323, 89, 933, 540, 269, 97, 114, 298, 476}; char letterGrades[] = {'A', 'B', 'C', 'D', ’F'};

  12. Initializer Lists • Note that when an initializer list is used: • no size value is specified • The size of the array is determined by the number of items in the initializer list • An initializer list can only be used only in the array declaration

  13. Array Input/ Output • We typically use for loops for any kind of array processing. • To input an array, one by one: for (i=0; i<10 ; i++ ) { printf(“ Enter element %d : “, i ); scanf ( “ %d “, &scores[i] ); }

  14. Array Output • To display an array, one element per line: for (i=0; i<10 ; i++ ) { printf(“ scores [%d] : %d\n“, i , scores[i] ); }

  15. Example • double X[ ]= {16.0, 12.0, 6.0, 8.0, 2.5, 12.0, 14.0, -54.5} • int i=5; • printf(“%f”, x[4]); • printf(“%f”, x[i]+1); • printf(“%f”, x[i+1]); • printf(“%f”, x[2*i]);

  16. Example • double X[ ]= {16.0, 12.0, 6.0, 8.0, 2.5, 12.0, 14.0, -54.5} • int i=5; • printf(“%f”, x[2*i-3]); • printf(“%f”, x[(int) x[4]]); • printf(“%f”, x[i++]); • printf(“%f”, x[--i]); • x[i]= x[i+1];

  17. Example int Squre[10], I; for (I=0; I<10; I++) Squre[I]=I * 2 ;

  18. Example Find the output of the following program: #include<stdio.h> int main() { int q[]= {3, 1, 4, 2}; float n[]={3.0, 1.0, 2.0, 7.0}, s=0; for(int j=0; j<3; j++) s+= n[q[j]-1]; printf(“%f”, s); return 0; }

  19. Find the output for(j=0; j<7;j++) A[j]=7-j; for(p=6; p>3; p--){ K=p-1; While(k>2){ T = A[p-k]; A[p-k] = A[7-p+k] A[7-p+k]= T } printf(“%d”, A[p]); }

  20. Trace the program int X[7] = {12, 25, 4, 9, 0, -1, 12} for (int j=5; j>=0; ) { printf(“%d”, X[j+1]); j--; }

  21. Find the output int A[7], sum =0; for(int j=0; j<7;j++) A[j]=7-j*3; for(int p=6; p>3; p--){ printf(“%d”, A[p]); sum=sum + A[p]; if(A[p]<2) printf(“%d”, p); } double Avg = sum / 7; printf(“%f”, Avg);

  22. Example Write a program that reads in an array of 10 elements, gets their average, and then display the deviation of each element from that average.

  23. Example • Write a program that reads in an array of 10 integers (range 0 - 50, strict) and then draws a bar chart of the values: 10 ********** 5 ***** 15 *************** ….

  24. one dimension two dimensions Two-Dimensional Arrays • A one-dimensional array stores a list of elements • A two-dimensional array can be thought of as a table of elements, with rows and columns

  25. Declaration • int A[3][3] • double B[3][3] • float C[6][6]

  26. Using • To use 2-dimensional array we use 2 for-loop as for(i=0; i<3;i++) for(j=0; j<3; j++) A[i][j]=i*j;

  27. Trace the program int A [3][3]= {{1, 7, 12}, {-2, 0, 4}, {45, 5, 4}} for(int i=1; i<3; i++) for(int j = 1; j<3; j=j+i) printf(“%d”, A[i][j]);

  28. Program • Write a program that read two matrices of size 5x5. and add two matrices to another one which will be displayed.

  29. Example • Write a C program to find the average of vector elements in 2-D matrix. • For example, you are given matrix A[5][5] and index 3, so you will print the average of elements in raw 3

  30. The End

More Related