Array

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Array definition

are random access data structures consisting of related data items of the same type - All elements are of the same data type - The elements themselves are stored contiguously in memory - Arrays in C remain the same size throughout program execution (i.e., they cannot be resized after they are created)

Example: Survey 40 students asked to rate quality of food cafeteria from 1 to 10. Place 40 responses in an integer array and summarize the results of the poll. (Print) ____________________ 1 2 2 2 3 4 4 5 5 11 6 7.... ___________________

int answer int rating int frequency[11]=[0]; int responses[40]={1,2,3,4,5,3,6,5,3,3...} for (answer =0; answer<40, answer++) { ++frequency[responses[answer]]; } for(rating=1; rating<11; rating++){ printf(%d,%d, rating, frequency[rating]); }

Passing Array to Function: Details

-In general, it's necessary to pass the size of the array as a parameter into function along w/ the array name - allows function to process the proper number of elements name of array evaluates to the address of the 1st element of the array. Thus, when we pass array to function as argument, we're essentially passing in the address of the first element in the array • Because the starting address of the array is passed, the called function knows precisely where the array is stored • In addition, any changes made to the array inside the function will affect the values in the array outside of the function

Passing arrays to Function: const to prevent modification

-qualifier prevents modification of array values in a function -When an array parameter is preceded by the const qualifier, the array elements become constant in the function body, and any attempt to modify an element of the array in the function body results in a compile-time error

Multidimensional Array: Overview

A common use of multiple-subscripted arrays (also called multidimensional arrays) is to represent tables of values consisting of information arranged in rows and columns • To identify a particular table element, we must specify two subscripts: The first (by convention) identifies the element's row and the second (by convention) identifies the element's column • Tables or arrays that require two subscripts to identify a particular element are called double-subscripted arrays

Passing Array to Function: individual array elements

- entire arrays are passed by pointers, individual elements passed by value like simple variables- scalars void function1(int a); int intArr[10] = {5,4,3,2,1}; function1(intArr[2]);

#define

- preprocessor directive - symbolic constant - Use only upper case letters for symbolic constants -Do not end a #define preprocessor directive with a semicolon, or all occurrences of the symbolic constant SIZE in the program are replaced with the text "10;" by the preprocessor

Passing Array to function: calling a function that has an array as an argument

- specify the name of the array without any brackets int hourlyTemperatures[24]; modifyArray(hourlyTemperatures, 24)

Passing Array to function: Function parameter

- this is done by adding square brackets to input parameter void modifyArray( int b[], int size ); - [] : indicate modifyArray expects to receive array of ints in parameter b - in size (number of array elements)


Ensembles d'études connexes

Employment Law for Human Resource Practice Chapter 1

View Set

Neuroscience of Behavior - Week 7

View Set

Chapter 6 Bones and Skeletal Tissues

View Set

Political Parties-Shaw Midterm 2

View Set

Musician's Guide to Fundamentals: Chapter 1 - 4 Review

View Set

Med Surg- Chapter 48 : Diabetes Mellitus

View Set

life skills notes needed to pass.

View Set