DOC PREVIEW
IUPUI CSCI 23000 - Arrays

This preview shows page 1-2-24-25 out of 25 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Slide 1ArraysArrays (cont.)Slide 4Slide 5Slide 6Slide 7Character ArraysPassing Arrays to FunctionsPassing Arrays to Functions (cont.)Slide 11Slide 12Multiple-Dimensional ArraysMultiple-Subscripted ArraysSlide 15Slide 16Slide 17Sorting ArraysCase Study: Computing Mean, Median and Mode Using ArraysSlide 20Slide 21Slide 22Slide 23Slide 24Searching Arrays: Linear Search vs Binary SearchDale RobertsDepartment of Computer and Information Science,Department of Computer and Information Science,School of Science, IUPUISchool of Science, IUPUICSCI 230ArraysDale Roberts, LecturerDale Roberts, [email protected]@cs.iupui.eduDale RobertsArraysArraysArrayArrayGroup of consecutive memory locations Group of consecutive memory locations Same name and type, ex: an array of integersSame name and type, ex: an array of integersTo refer to an element, specifyTo refer to an element, specifyArray nameArray namePosition number of particular element in the arrayPosition number of particular element in the arrayFormat:Format:array_namearray_name[[ position numberposition number ]]First element at position First element at position 00 nn element array named element array named cc::c[ 0 ]c[ 0 ], , c[ 1 ]c[ 1 ]......c[ c[ n – 1n – 1 ] ]ExampleExample: : int my_array[12]int my_array[12] my_array[0]= -45 my_array[0]= -45  value storedvalue storedPosition number must be an integer number or anPosition number must be an integer number or aninteger expressioninteger expressionExampleExample: : my_array[1.5] my_array[1.5]  ERROR!!ERROR!! my_array[i+j] my_array[i+j]  valid ifvalid if i i and and j j are integersare integersName of array (Note that all elements of this array have the same name, my_array)Position number of the element within array my_array-4560721543-89062-31645378My_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]My_array[10]My_array[11]Dale RobertsArrays Arrays (cont.)(cont.)Array elements are like normal variablesArray elements are like normal variablesmy_array[8]my_array[8] = -3;= -3; scanf("%d", &my_array[8]); printf("%d",my_array[8]);scanf("%d", &my_array[8]); printf("%d",my_array[8]);Perform operations in subscript. If Perform operations in subscript. If xx equals equals 3: 3: my_array[ 5 - 2 ] == my_array[ 3 ] == my_array[ x ]my_array[ 5 - 2 ] == my_array[ 3 ] == my_array[ x ]Declaring ArraysDeclaring ArraysWhen declaring arrays, specifyWhen declaring arrays, specifyNameNameType of arrayType of arrayNumber of elements: Number of elements: arrayType arrayType arrayName[numberOfElements];arrayName[numberOfElements];ExamplesExamples: : int c[ 100 ];int c[ 100 ];/* reserve memory sufficient enough to store 100/* reserve memory sufficient enough to store 100 elements of type integer */elements of type integer */ float myArray[ 3284 ];float myArray[ 3284 ];Dale RobertsArrays Arrays (cont.)(cont.)Declaring multiple arrays of same type: Declaring multiple arrays of same type: format similar to regular variablesformat similar to regular variablesExampleExample::int b[ 100 ], x[ 27 ]; int b[ 100 ], x[ 27 ]; Arrays may be declared to contain other data typesArrays may be declared to contain other data typesExampleExample::int a[ 100 ];int a[ 100 ];float b[ 100 ];float b[ 100 ];char c[ 100 ]; /* char c[ 100 ]; /* Strings are stored by using character arraysStrings are stored by using character arrays */ */ExampleExample: : #include <stdio.h>#include <stdio.h>/* a simple program that uses arrays *//* a simple program that uses arrays */main(main({{int i, array_int[100];int i, array_int[100];for (i=0; i<100; i++)for (i=0; i<100; i++)array_int[i]=0;array_int[i]=0;for (i=0; i<100; i++)for (i=0; i<100; i++)printf(“element %d: %d\n”, i, array_int[i]);printf(“element %d: %d\n”, i, array_int[i]);}}Dale RobertsArrays Arrays (cont.)(cont.)InitializersInitializersint n[5] = {1, 2, 3, 4, 5}; int n[5] = {1, 2, 3, 4, 5}; ExampleExample::main()main(){{int i, a[10]={1, 2, 3, 4, 5, 6, 7, 8, 9, 10};int i, a[10]={1, 2, 3, 4, 5, 6, 7, 8, 9, 10};for (i=0; i<10; i++)for (i=0; i<10; i++)printf(“Element: %d\n”, a[i]);printf(“Element: %d\n”, a[i]);}}If there are fewer initializations than elements in the array, then the If there are fewer initializations than elements in the array, then the remaining elements are automatically initialized to remaining elements are automatically initialized to 0.0.int n[5] = {0}int n[5] = {0} /* /* All elements 0All elements 0 */ */int a[10] = {1, 2}int a[10] = {1, 2}/* a[2]/* a[2] to to a[9]a[9] are initialized to zeros are initialized to zeros */*/int b[5] = {1, 2, 3, 4, 5, 6}int b[5] = {1, 2, 3, 4, 5, 6}/* /* syntax errorsyntax error */ */C arrays have no bounds checkingC arrays have no bounds checkingIf size omitted, initializers determine itIf size omitted, initializers determine itint n[ ] = { 1, 2, 3, 4, 5 }; /* int n[ ] = { 1, 2, 3, 4, 5 }; /* 5 initializers, therefore 5 element array 5 initializers, therefore 5 element array */*/Scalable Arrays: a better programming styleScalable Arrays: a better programming style#define SIZE 10#define SIZE 10int c[SIZE];int c[SIZE];/* defines a symbolic constant size with value 10 *//* defines a symbolic constant size with value 10 */Dale Roberts1 /* Fig. 6.8: fig06_08.c2 Histogram printing program */3 #include <stdio.h>4 #define SIZE 1056 int main()7 {8 int n[ SIZE ] = { 19, 3, 15, 7, 11, 9, 13, 5, 17, 1 };9 int i, j;1011 printf( "%s%13s%17s\n", "Element", "Value", "Histogram" );1213 for ( i = 0; i <= SIZE - 1; i++ ) {14 printf( "%7d%13d ", i, n[ i ]) ;1516 for ( j = 1; j <= n[ i ]; j++ ) /* print one bar */17 printf( "%c", '*' );1819 printf( "\n" );20 }2122 return 0;23 }Element Value Histogram 0 19 ******************* 1 3 *** 2 15 *************** 3 7 ******* 4 11 *********** 5 9 ********* 6 13 ************* 7 5 ***** 8 17 ***************** 9 1 *1. Initialize array2. Loop3. PrintProgram outputDale RobertsArrays Arrays (cont.)(cont.)Example:#include <stdio.h>#define SIZE 100main(){int i, a[SIZE];int i, a[SIZE];int sum = 0;int sum = 0;……for (i=0; i< size; i++)for (i=0; i< size; i++)sum =


View Full Document

IUPUI CSCI 23000 - Arrays

Download Arrays
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Arrays and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Arrays 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?