Read the slides and do the task, code.
Introduction to Computer Science II
CSE 112
Arrays and type limitations
1
CSE 112 – Introduction to Computer Science
• Recall the array type
– Here, a constant is first declared for the size of the array,
and the array (sales_values) is declared:
const int MAX_VALUES = 20;
double sales_values[MAX_VALUES];
This is just as good.
double sales_values[20];
2
CSE 112 – Introduction to Computer Science
• Recall the array type
– We can also initialize an array when declared:
double sales_values[5] = {2.33, 4.58, 1.95};
• One limitation is that all of the array elements
must be type double
3
CSE 112 – Introduction to Computer Science
• Also, recall populating an array
– Populating the array using a function:
Pass by array
void fill_array(double salesValues[ ], int arraySize, int&
numberOfElements);
int main()
{
Size declared
Count the indexes populated
–
fill_array(salesValues, MAX_VALUES, numberOfElements);
4
CSE 112 – Introduction to Computer Science
• Populating the array with the file data:
void fill_array(double salesValues[], int arraySize, int& numberOfElements)
{
int index = 0;
// declare an index
double nextValue;
// store the value when read
in_stream >> nextValue;
// read the value
while ((nextValue > 0) && (index < arraySize))
5
CSE 112 - Introduction to Computer Science
• Populating the array with the file data:
void fill_array(double salesValues[], int arraySize, int& numberOfElements)
{
in_stream >> nextValue;
// read the value
while ((nextValue > 0) && (index < arraySize))
{
salesValues[index] = nextValue;
index++;
in_stream >> nextValue;
// read the value
}
numberOfElements = index;
// store the number of indexes
}
6
CSE 112 – Introduction to Computer Science
• Accessing the individual elements in the array:
int index = 0;
{
salesValues[index] = nextValue;
index++;
in_stream >> nextValue;
}
salesValues[0]
salesValues[1] …
salesValues[n]
– One value per array slot
7
CSE 112 – Introduction to Computer Science
• To store multiple data types for associated data, we can
use a structure.
• Example:
– What if we wanted to include additional information with
each sales value…
– If there are multiple stores and we wanted to “associate”
each sales value with the store that made the sale and the
location
• A structure can be used
8
CSE 112 – Introduction to Computer Science
• Structures
– A structure is a data type that can contain multiple data types
• A structure called storeInfo could contain the store
number and the monthly sales amount.
struct StoreInfo
{
total_sales
store_number
store_location
};
double
integer
string
9
CSE 112 – Introduction to Computer Science
• A structure provides this type of data container, and allows
multiple data type
• We can view a structure as an object (with no member
functions) that contains multiple values without any limitation
on the data types in the structure
• Therefore, when we need an array with multiple data types,
we can declare a structure and then an array of those
structures
10
CSE 112 – Introduction to Computer Science
• An Array of Structures
struct StoreInfo
{
double total_sales;
int store_number;
string store_location;
};
StoreInfo storeData[5];
data type
name of the array
number of structures
11
CSE 112 – Introduction to Computer Science
• Structures
– Note that the structures in the array do not have names.
– They are accessed using the indexes of the array
storeData[0]
total_sales
store_number
store_location
storeData[1]
total_sales
store_number
store_location
… storeData[n]
… total_sales
… store_number
… storeLocation
12
CSE 112 – Introduction to Computer Science
• Initializing when declared
• // Array of StoreInfo structures
StoreInfo storeData[3] = {
{23780.55, 1, “Wilmington”},
{46365.88, 2, “Trenton”},
{37658.34, 3, “Philadelphia”}
};
13
CSE 112 – Introduction to Computer Science
• Accessing the elements
for(int i = 0; i < arraySize; i++)
{
cout