CS110_04c_arrays

# You can assign its value use its value in an

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

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 element 2 element 3 element 4 381 4107 932 40 212 20 Summary: Arrays 3) Then a properly indexed array behaves like any other variable. You can: Assign its value, Use its value in an expression, and Set its index using a variable or an expression: elvis[2] = 0; elvis element 0 element 1 element 2 element 3 element 4 381 4107 0 40 212 21 Summary: Arrays 3) Then a properly indexed array behaves like any other variable. You can: Assign its value, Use its value in an expression, and Set its index using a variable or an expression: elvis[2] = 0; elvis[1] = (elvis[3] / 10)%6; elvis element 0 element 1 element 2 element 3 element 4 381 4 0 40 212 22 Summary: Arrays 3) Then a properly indexed array behaves like any other variable. You can: Assign its value, Use its value in an expression, and Set its index using a variable or an expression: elvis[2] = 0; elvis[1] = (elvis[3] / 10)%6; elvis[elvis[2]] = 5; elvis element 0 element 1 element 2 element 3 element 4 5 4 0 40 212 23 Summary: Arrays 3) A properly indexed array behaves like any other variable. You can: Assign its value, Use its value in an expression, and Set its index using a variable or an expression, Pass its value to a function, such as printf() printf("elvis[%d]=%d",elvis[2], elvis[elvis[2]]); elvis element 0 element 1 element 2 element 3 element 4 5 4 0 40 212 24 What is an `array', really? What happens in memory when we `declare a variable' and when we `declare an array' ? How is it different? 25 (Recall) Bits, Bytes and Memory BIT == BInary digiT == a 2-way choice (1/0, yes/no, BI true/false) == the fundamental unit of information Byte = 8 bits grouped together: 28 = 256 choices A byte is atomic for most* computers: the smallest `bite' of info they can retrieve and process. *exceptions: very small and/or special purpose computers Memory is a long list of stored bytes. Sequentially numbered; address. 26 (Recall) Bits, Bytes and Memory ... 8919 8920 8921 8922 8923 8924 8925 8926 8927 8928 8929 8930 8931 8932 ... stored bytes Address Address Memory Memory holds the instructions and variables of your program (& much much more...) 27 Declarations and Memory To `declare' a variable is to ==`reserve a named chunk of memory,' or ==(Jargon) `allocate memory for this variable' Address Address int my_int = 5; char ch = `a'; ... 8919 8920 8921 8922 8923 ... +0 00 00 05 a my_int ch 28 Declarations and Memory any variable How much memory gets used? Use sizeof() function to find out: or expression byte_count = sizeof(my_var); For Windows98, NT4, 2000, ME, XP,... memory usage in bytes char int float double == 1 byte, == 4 bytes, == 4 bytes, == 8 bytes. 29 Arrays and Memory usr ... 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 ... int usr[3]; usr[0] Sequential in memory usr[1] Address Address Amount of Memory used: array_size*sizeof(type) usr[2] Addressing is easy: (Address_of_element_0) + (i * sizeof(type)) 30...
View Full Document

{[ snackBarMessage ]}

### What students are saying

• 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.

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

• 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.

Dana University of Pennsylvania ‘17, Course Hero Intern

• 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.

Jill Tulane University ‘16, Course Hero Intern