1 / 13

Chapter 8

Chapter 8. Using Numeric Arrays. What Is an Array?. An array is a collection of data storage locations, each having the same data type and the same name. Each storage location in an array is called an array element . Types of Arrays. Two types :- 1. Single-Dimensional Arrays

aletta
Télécharger la présentation

Chapter 8

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. Chapter 8 Using Numeric Arrays

  2. What Is an Array? An array is a collection of data storage locations, each having the same data type and the same name. Each storage location in an array is called an array element.

  3. Types of Arrays Two types:- 1. Single-Dimensional Arrays Has only a single subscript. An example :- float expenses[12]; 2. Multidimensional Arrays Has more than one subscript. An example :- int checker[8][8];

  4. SINGLE DIMENSIONAL ARRAYS

  5. Numbering of S.D. Array Elements • Array elements are stored in sequential memory locations. • Variables are like individual folders, whereas an array is like a single folder with many compartments. • 12 elements of an array are numbered 0 through 11 • float expenses[12]; • The first array element is expenses[0], not expenses[1]

  6. Input and output from S.D. Arrays /* EXPENSES.C - Demonstrates use of an array */ #include <stdio.h> /* Declare an array to hold expenses, and a counter variable */ float expenses[13]; int count; main() { /* Input data from keyboard into array */ for (count = 1; count < 13; count++) { printf("Enter expenses for month %d: ", count); scanf("%f", &expenses[count]); } /* Print array contents */ for (count = 1; count < 13; count++) { printf("Month %d = $%.2f\n", count, expenses[count]); } return 0; }

  7. Multidimensional Arrays • A multidimensional array has more than one subscript. A two-dimensional array has two subscripts, a • A three-dimensional array has three subscripts, and so on. There is no limit to the number of dimensions a C array can have. • There is a limit on total array size

  8. Naming and Declaring Arrays Multidimensional Arrays

  9. M.D. Arrays • A two-dimensional array has a row-and-column • A three-dimensional array could be thought of as a cube structure • Four dimension and higher?

  10. When you declare an array, you can specify the number of elements with a • literal constant • or with a symbolic constant created with the #define directive. (Don’t use const command) • Example: • #define MONTHS 12 • int array[MONTHS]; is equivalent to this statement: • int array[12];

  11. Initializing Single dimensional Arrays • int array[4] = { 100, 200, 300, 400 } • Initializing Multidimensional Arrays • int array[4][3] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }; • results in the following assignments: • array[0][0] is equal to 1 • array[0][1] is equal to 2 • array[0][2] is equal to 3 • array[1][0] is equal to 4 • array[1][1] is equal to 5 • array[1][2] is equal to 6 • ... • array[3][1] is equal to 11 • array[3][2] is equal to 12

  12. We will restrict to 2 Dimensional for now. This program is a 3-d array • /* RANDOM.C - Demonstrates using a multidimensional array */ • #include <stdio.h> • #include <stdlib.h> • /* Declare a three-dimensional array with 1000 elements */ • intrandom_array[10][10][10]; • int a, b, c; • main() • { • /* Fill the array with random numbers. The C library */ • /* function rand() returns a random number. Use one */ • /* for loop for each array subscript. */ • for (a = 0; a < 10; a++) • { • for (b = 0; b < 10; b++) • { • for (c = 0; c < 10; c++) • { • random_array[a][b][c] = rand(); • } • } • } • /* Now display the array elements 10 at a time */ • for (a = 0; a < 10; a++) • { • for (b = 0; b < 10; b++) • { • for (c = 0; c < 10; c++) • { • printf("\nrandom_array[%d][%d][%d] = ", a, b, c); • printf("%d", random_array[a][b][c]); • } • printf("\nPress Enter to continue, CTRL-C to quit."); • getchar(); • } • } • return 0;

  13. Maximum Array Size • Storage space requirements for numeric data types for many PCs. • Element Data Type Element Size (Bytes) int 2 or 4 short 2 long 4 float 4 Double 8 • To calculate the storage space required for an array, multiply the number of elements in the array by the • element size. For example, a 500-element array of type float requires storage space of 500 * 4 = 2000

More Related