t11AArraysDeclarations

t11AArraysDeclarations - Arrays Declarations Dale Roberts...

Info icon This preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Arrays Declarations Dale Roberts Arrays Array Group of consecutive memory locations Group Same name and type, ex: an array of integers Same Name of array (Note that all elements of this array have the same name, my_array) -45 6 0 72 1543 -89 0 62 -3 1 6453 78 To refer to an element, specify Array name Position number of particular element in the array my_array[0] my_array[1] my_array[2] my_array[3] my_array[4] my_array[5] my_array[6] my_array[7] my_array[8] my_array[9] Format: First element at position 0 First n element array named c: array_name[ position number ] position c[ 0 ], c[ 1 ]...c[ n – 1 ] c[ c[ Example: int my_array[12] int my_array[0]= -45 ⇐ value stored value Example: Position number must be an integer number or an my_array[10] integer expression my_array[11] my_array[1.5] ⇐ ERROR!! ERROR!! my_array[i+j] ⇐ valid if i and j are integers valid are Position number of the element within array my_array Dale Roberts Arrays (cont.) Arrays (cont.) Array elements are like normal variables my_array[8] = -3; -3; scanf("%d", &my_array[8]); printf("%d",my_array[8]); scanf("%d", Perform operations in subscript. If x equals 3: Perform my_array[ 5 - 2 ] == my_array[ 3 ] == my_array[ x ] my_array[ Declaring Arrays When declaring arrays, specify Name Type of array Number of elements: arrayType Number arrayName[numberOfElements]; arrayName[numberOfElements]; Examples: int c[ 100 ]; /* reserve memory sufficient enough to store 100 int elements of type integer */ elements float myArray[ 3284 ]; float Dale Roberts Arrays (cont.) Arrays (cont.) Declaring multiple arrays of same type: format similar to regular variables format Declaring Example: int b[ 100 ], x[ 27 ]; int Arrays may be declared to contain other data types Arrays Example: int a[ 100 ]; float b[ 100 ]; char c[ 100 ]; /* Strings are stored by using character arrays char Strings */ */ Example: #include <stdio.h> #include /* a simple program that uses arrays */ main( { int i, array_int[100]; for (i=0; i<100; i++) array_int[i]=0; for (i=0; i<100; i++) printf(“element %d: %d\n”, i, array_int[i]); } Dale Roberts Arrays (cont.) Arrays (cont.) Initializers int n[5] = {1, 2, 3, 4, 5}; int Example: main() Example { int i, a[10]={1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; for (i=0; i<10; i++) printf(“Element: %d\n”, a[i]); } If there are fewer initializations than elements in the array, then the If remaining elements are automatically initialized to 0. 0. int n[5] = {0} /* All elements 0 */ All int a[10] = {1, 2} /* a[2] to a[9] are initialized to zeros a[9] */ */ int b[5] = {1, 2, 3, 4, 5, 6} /* syntax error */ /* syntax C arrays have no bounds checking If size omitted, initializers determine it int n[ ] = { 1, 2, 3, 4, 5 }; int */ */ /* 5 initializers, therefore 5 element array Scalable Arrays: a better programming style #define SIZE 10 int c[SIZE]; /* defines a symbolic constant size with value 10 */ Dale Roberts Arrays (cont.) Arrays (cont.) Example: #include <stdio.h> #define SIZE 100 main() { int i, a[SIZE]; int sum = 0; … for (i=0; i < SIZE; i++) sum = sum + a[i]; printf(“sum: %d\n, sum); } Dale Roberts 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 /* Fig. 6.8: fig06_08.c Histogram printing program */ #include <stdio.h> #define SIZE 10 int main() { int n[ SIZE ] = { 19, 3, 15, 7, 11, 9, 13, 5, 17, 1 }; int i, j; printf( "%s%13s%17s\n", "Element", "Value", "Histogram" ); for ( i = 0; i <= SIZE - 1; i++ ) { printf( "%7d%13d ", i, n[ i ]) ; for ( j = 1; j <= n[ i ]; j++ ) printf( "%c", '*' ); printf( "\n" ); } return 0; } /* print one bar */ 1. Initialize array 2. Loop 3. Print Program output Element 0 1 2 3 4 5 6 7 8 9 Value 19 3 15 7 11 9 13 5 17 1 Histogram ******************* *** *************** ******* *********** ********* ************* ***** ***************** * Dale Roberts ...
View Full Document

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern