Introduction To Arrays
Introduction To Arrays
Introduction to Arrays
int numbers[3]; ? ? ?
Note: When looping through an array, the array subscript should never go
below 0 and should always be less than the total number of elements in
the array (one less than the size of the array).
Array of characters
• In C, a string is a char array terminated by a NULL character '\
0‘.
• Arrays may be of any type, including chars – We can store
character strings in char arrays
• Example:
char string1[] = "Hello";
char string1[] = {'H', 'e', 'l', 'l', 'o', '\0'};
char string1[256] = "Hello";
Example: Summing the elements of an array
#include <iostream>
using namespace std;
Declare constant variable arraySize
int main() { using the const keyword
const int arraySize = 10; // specifies size of array
int a[arraySize] = {87, 68, 94, 100, 83, 78, 85, 91, 76, 87};
int total = 0;
cout << "Total of array elements: " << total << endl;
}
Example: Display array data graphically (1/2)
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
const int arraySize = 11;
int n[arraySize] = {0, 0, 0, 0, 0, 0, 1, 2, 4, 2, 1};
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
// define array sizes
const int responseSize = 20; // size of array responses
const int frequencySize = 6; // size of array frequency
// for each answer, select responses element and use that value
// as frequency subscript to determine element to increment
for (int answer = 0; answer < responseSize; answer++)
frequency[responses[answer]]++; For each response,
increment frequency
cout << "Rating" << setw(17) << "Frequency" << endl; value at the index
associated with that
// output each array element's value response
for (int rating = 1; rating < frequencySize; rating++)
cout << setw(6) << rating << setw(17) << frequency[rating]
<< endl;
Output:
} Rating Frequency
1 3
2 5
3 7
4 2
5 3
Using arrays to summarize survey results
responses[20] = {1, 2, 5, 4, 3, 5, 2, 1, 3, 1, 4, 3, 3, 3, 2, 3, 3, 2,
2, 5};
array frequency[6]:
Scale rating = 1 rating = 2 rating = 3 rating = 4 rating = 5
int main() {
const int arraySize = 5; // size of array a
int a[arraySize] = {0, 1, 2, 3, 4}; // initialize array a
#include <iostream>
using namespace std;
int main() {
int a[] = {10, 20, 30};
tryToModifyArray(a);
cout << a[0] << ' ' << a[1] << ' ' << a[2] << '\n';
}
static const int students = 10; // number of students who took the test
int grades[students] = {87, 68, 94, 100, 83, 78, 85, 91, 76, 87};
int getMaximum()
{
int highGrade = 0; // assume highest grade is 0
Using an array to store grades (3/6)
// loop through grades array
for (int grade = 0; grade < students; grade++)
{
// if current grade higher than highGrade, assign it to highGrade
if (grades[grade] > highGrade)
highGrade = grades[grade]; // new highest grade
}
outputGrades();
cout << "\nClass average is " << setprecision(2) << fixed <<
getAverage() << "\nLowest grade is " << getMinimum() << "\nHighest
grade is " << getMaximum() << endl;
return 0;
}