CNIT 15501 Lecture Notes - Lecture 11: Array Data Structure

57 views2 pages
Arrays in C#
Array: A data structure for storing several values of the same data type, under one
name in the computer’s RAM
Declaration Syntax
dataType[] arrayName = new dataType[size];
Size, is the number of locations in the array.
It must be a whole number > 0
Size can be a numeric constant, ex: 100
It can be a named constant.
Size cannot be a variable.
Ex: Declare an array to store 50 real numbers:
double [] price = new double [50];
What happens at declaration?
int [] data = new int[7];
indices: 0 1 2 3 4 5 6
Data:
7 consecutive memory locations get allocated.
Memory locations are indexed 0, 1, 2, …6.
Index of the 1st element is always 0.
Accessing array elements
Int [] data = new int[7];
Store 40 at index 3: data[3] = 40;
Store 2 in the first element: data[0] = 2;
To display data[3] in a Textbox:
txtOutput.Text = data[3].ToString();
To store the user’s input in data[2],
data[2] = int.Parse(txtInput.Text);
find more resources at oneclass.com
find more resources at oneclass.com
Unlock document

This preview shows half of the first page of the document.
Unlock all 2 pages and 3 million more documents.

Already have an account? Log in

Document Summary

Array: a data structure for storing several values of the same data type, under one name in the computer"s ram. Size, is the number of locations in the array. It must be a whole number > 0. Size can be a numeric constant, ex: 100. Ex: declare an array to store 50 real numbers: Double [] price = new double [50]; What happens at declaration? int [] data = new int[7]; indices: 0 1 2 3 4 5 6. Memory locations are indexed 0, 1, 2, 6. Index of the 1st element is always 0. Store 40 at index 3: data[3] = 40; Store 2 in the first element: data[0] = 2; To display data[3] in a textbox: txtoutput. text = data[3]. tostring(); To store the user"s input in data[2], data[2] = int. parse(txtinput. text); Let"s write a for loop to go through indices 0 9 and store 1 in every array element. int ctr; for (ctr = 0; ctr < 10; ctr++)

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents

Related Questions