60 likes | 285 Views
Introduction To Arrays. Arrays are designed to handle large amounts of data of the same type in an organized manner. Using arrays permits us to set aside a group of memory locations that we can then manipulate as a single entity or have direct access to any component.
E N D
Introduction To Arrays Arrays are designed to handle large amounts of data of the same type in an organized manner. Using arrays permits us to set aside a group of memory locations that we can then manipulate as a single entity or have direct access to any component. Arrays may be used for alphabetizing a list of names, analyzing a list of test scores, manipulating character data, and keeping an inventory. Let us at this point work with a list of five integers: 18, 17, 21, 18, and 19. Prior to this chapter, we would have declared five variables--A, B, C, D, and E--and assigned them appropriate values. This would have produced five values in memory each accessed by a separate identifier. int Data [5];
Array Cells printf ("EvenNos = &%x\n", EvenNos); printf ("EvenNos = &%x\n", &EvenNos); ? ? int Data [5]; The five individual portions of the array are referred to as cells or components or elements of the array. ? ? EvenNos = &2753454 EvenNos = &2753454 ? &2753454 Data
8 EvenNos[0] = 0; EvenNos [1] = 2; EvenNos [2] = 4; EvenNos [3] = 6; EvenNos [4] = 8; Filling The Array 6 4 2 0 int EvenNos[5]; &2753454 EvenNos for (Index = 0; Index < 10; Index ++) EvenNos [Index] = Index * 2;
for (Index = 4; Index >= 0; Index --) { printf (" |--------|\n"); printf ("%d : EvenNos [%d] => %x | %3d |\n", Index, Index, &EvenNos[Index], EvenNos[Index]); } printf (" |--------|\n"); 8 Displaying The Array 6 4 2 0 short int EvenNos[5]; &2753454 EvenNos
Use the define statement for the MAX_NOS so that it is easier to change the size of your arrays as programs grow. # define MAX_NOS 100 int EvenNos [MAX_NOS]; for (Index = MAX_NOS - 1; Index >= 0; Index --) EvenNos [Index] = 2 * Index; for (Index = MAX_NOS - 1; Index >= 0; Index --) { printf(" |--------|\n"); printf ("%3d : EvenNos [ %2d ] => %x | %3d |\n", Index,Index, &EvenNos [Index], EvenNos [Index]); } printf (" |--------|\n"); 8 Defining Constants Add Flexability 6 4 2 EvenNos 0
Use the define statement for the MAX_NOS so that it is easier to change the size of your arrays as programs grow. # define MAX_NOS 100 int EvenNos [MAX_NOS]; for (Index = MAX_NOS - 1; Index >= 0; Index --) EvenNos [Index] = 2 * Index; for (Index = MAX_NOS - 1; Index >= 0; Index --) { printf(" |--------|\n"); printf ("%3d : EvenNos [ %2d ] => %x | %3d |\n", Index,Index, &EvenNos [Index], EvenNos [Index]); } printf (" |--------|\n"); 8 cc 6 4 2 EvenNos 0