Array Function String
Array Function String
Array
1
2
Design Problem
Consider the task of
keeping track of data
about parts for
manufacture
» part number,
description, qty
needed, unit price
3
Parallel Array
Use “Parallel” arrays
One array each for part
num, descrip, qty, price partid desc qty price
nth item in any one of
A100 xxx 5 1.23 0
the arrays associated
with same nth item of all
B25 yyy 23 8.95 1
the arrays
» string partid[]
» string desc[] 2
» int qty[]
» double price[]
Parallel One-Dimensional Arrays
Program for a motorcycle club displays annual fee
associated with membership type entered by user
Program uses two parallel one-dimensional arrays
» char array named types: stores the five
membership types
» int array named fees: stores annual fee associated
with each type
Two arrays are referred to as parallel arrays if their
elements are related by their position in the arrays
4
Parallel One-Dimensional Arrays (cont’d.)
5
Parallel One-Dimensional Arrays (cont’d.)
6
Parallel One-Dimensional Arrays (cont’d.)
7
Parallel One-Dimensional Arrays (cont’d.)
8
Parallel One-Dimensional Arrays (cont’d.)
9
Parallel One-Dimensional Arrays (cont’d.)
10
Parallel One-Dimensional Arrays (cont’d.)
11
Passing Array to Function
Chapter 9
12
Passing Arrays to Functions
13
14
Arrays as Parameters
This is one task that CAN be done to the
WHOLE array
C++ always passes arrays by reference
Passing Size along with Array
15
Pass-by-Reference
16
Base Address of an Array and Array 17
in Computer Memory
#include <iostream>
using namespace std;
void printArray(int list[], int arraySize); // function prototype
int main()
{ 1 list[0]
int myList[5] = {1, 4, 3, 6, 8};
printArray(myList, 5);
return 0;
4 list[1]
}
3 list[2]
void printArray(int list[], int arraySize)
{ 6 list[3]
for (int i = 0; i < arraySize; i++)
{ 8 list[4]
cout << list[i] << " ";
}
}
20
Arrays as Parameters
The name of the array is a pointer
constant
The address of the array is passed to
the function
Size of the
array also
passed to
control loop
21
Arrays as Parameters
Note the empty brackets in parameter
list
» A number can be placed here but it
will be ignored
22
Sub-array Processing
Note we specified an array size of 100
» but we don’t anticipate that many scores
Array always declared larger than needed
Must keep track of how many have been
used
» this is our limit when doing other things
to the array
Example: PassByReferenceDemo
24
Example: ConstArrayDemo
#include <iostream>
using namespace std;
int main()
{
int numbers[5] = {1, 4, 3, 6, 8};
p(numbers, 5);
return 0;
}
26
X
int[] reverse(const int list[], int size)
27
Modifying Arrays in Functions, cont.
list
newList
28
Example: ReverseArray
list 1 2 3 4 5 6
newList 0 0 0 0 0 0
30
Trace the reverse Method, cont.
int list1[] = {1, 2, 3, 4, 5, 6};
reverse(list1, list2);
i = 0 and j = 5
void reverse(const int list[], int newList[], int size)
{
for (int i = 0, j = size - 1; i < size; i++, j--)
{
newList[j] = list[i];
}
}
list 1 2 3 4 5 6
newList 0 0 0 0 0 0
31
Trace the reverse Method, cont.
int list1[] = {1, 2, 3, 4, 5, 6};
reverse(list1, list2);
i (= 0) is less than 6
newList 0 0 0 0 0 0
32
Trace the reverse Method, cont.
int list1[] = {1, 2, 3, 4, 5, 6};
reverse(list1, list2);
list 1 2 3 4 5 6
newList 0 0 0 0 0 1
33
Trace the reverse Method, cont.
int list1[] = {1, 2, 3, 4, 5, 6};
reverse(list1, list2);
list 1 2 3 4 5 6
newList 0 0 0 0 0 1
34
Trace the reverse Method, cont.
int list1[] = {1, 2, 3, 4, 5, 6};
reverse(list1, list2);
i (=1) is less than 6
void reverse(const int list[], int newList[], int size)
{
for (int i = 0, j = size - 1; i < size; i++, j--)
{
newList[j] = list[i];
}
}
list 1 2 3 4 5 6
newList 0 0 0 0 0 1
35
Trace the reverse Method, cont.
newList 0 0 0 0 2 1
36
Trace the reverse Method, cont.
}
list 1 2 3 4 5 6
newList 0 0 0 0 2 1
37
Trace the reverse Method, cont.
int list1[] = {1, 2, 3, 4, 5, 6};
reverse(list1, list2);
list 1 2 3 4 5 6
newList 0 0 0 0 2 1
38
Trace the reverse Method, cont.
int list1[] = {1, 2, 3, 4, 5, 6};
reverse(list1, list2);
}
list 1 2 3 4 5 6
newList 0 0 0 3 2 1
39
Trace the reverse Method, cont.
int list1[] = {1, 2, 3, 4, 5, 6};
reverse(list1, list2);
list 1 2 3 4 5 6
newList 0 0 0 3 2 1
40
Trace the reverse Method, cont.
int list1[] = {1, 2, 3, 4, 5, 6};
reverse(list1, list2);
}
list 1 2 3 4 5 6
newList 0 0 0 3 2 1
41
Trace the reverse Method, cont.
int list1[] = {1, 2, 3, 4, 5, 6};
reverse(list1, list2);
list 1 2 3 4 5 6
newList 0 0 4 3 2 1
42
Trace the reverse Method, cont.
int list1[] = {1, 2, 3, 4, 5, 6};
reverse(list1, list2);
list 1 2 3 4 5 6
newList 0 0 4 3 2 1
43
Trace the reverse Method, cont.
int list1[] = {1, 2, 3, 4, 5, 6};
reverse(list1, list2);
}
list 1 2 3 4 5 6
newList 0 0 4 3 2 1
44
Trace the reverse Method, cont.
int list1[] = {1, 2, 3, 4, 5, 6};
reverse(list1, list2);
list 1 2 3 4 5 6
newList 0 5 4 3 2 1
45
Trace the reverse Method, cont.
int list1[] = {1, 2, 3, 4, 5, 6};
reverse(list1, list2);
list 1 2 3 4 5 6
newList 0 5 4 3 2 1
46
Trace the reverse Method, cont.
int list1[] = {1, 2, 3, 4, 5, 6};
reverse(list1, list2);
list 1 2 3 4 5 6
newList 0 5 4 3 2 1
47
Trace the reverse Method, cont.
int list1[] = {1, 2, 3, 4, 5, 6};
reverse(list1, list2);
list 1 2 3 4 5 6
newList 6 5 4 3 2 1
48
Trace the reverse Method, cont.
int list1[] = {1, 2, 3, 4, 5, 6};
reverse(list1, list2);
list 1 2 3 4 5 6
newList 6 5 4 3 2 1
49
Trace the reverse Method, cont.
int list1[] = {1, 2, 3, 4, 5, 6};
reverse(list1, list2);
list 1 2 3 4 5 6
newList 6 5 4 3 2 1
50
Case Study: Production Graph
Problem Definition:
» We are writing a program for the Apex Plastic
Spoon Company
» The program will display a bar graph showing the
production of each of four plants for a week
» Each plant has separate records for each
department
» Input is entered plant by plant
» Output shows one asterisk for each 1000 units,
and production is rounded to the nearest 1,000
units
Analysis of The Problem
Use an array named production to hold
total production of each plant
» Production for plant n is stored in production[n-1]
do the following
Read all the data for plant number plant_number
Sum the numbers
Set production[plant_number – 1] to the total
Coding input_data
The algorithm can be translated to C++ as:
Declaration of C-Strings
Similar to declaration of any array
char name[30];
// no initialization
char title [20] = "Le Grande Fromage";
// initialized at declaration
// with a string
char chList [10] = {'a', 'b', 'c', 'd'};
// initialized with list of char
// values
75
Initializing Strings
When a character array is declared, it is legal
to use the assignment operator to initialize
String Output
Strings (character arrays) are handled
differently than other types of arrays
This would NOT be allowed
int num_list [100];
. . .
cout << num_list;
This is legal:
char name [30] = “Snidly Q. Fizbane”;
. . .
cout << name;
78
String Input
Declare strings 1
element bigger than
planned size to
allow for ‘\0’
Using Strings
Instead of “hard coding” file name for the open (
… ) command,
» use a string variable,
» use keyboard entry with cin.getline(…)
» program more flexible, good for different files
ifstream inFile;
char fname[31];
cout << “Enter file name -> “;
cin.getline (fname, 30, ‘\n’);
inFile.open (fname);
83
Function prototypes
#include <iostream>
#include <ctime>
using namespace std;
const int NUMBER_OF_RANDOM_LETTERS = 100;
void createArray(char []);
void displayArray(const char []);
void countLetters(const char [], int []);
void displayCounts(const int []);
88
Main function
int main()
{
char chars[NUMBER_OF_RANDOM_LETTERS]; //declare array
// Initialize the array with random lowercase letters
createArray(chars);
// Display the array
cout << "The lowercase letters are: " << endl;
displayArray(chars);
// Count the occurrences of each letter
int counts[26];
countLetters(chars, counts);
// Display counts
cout << "\nThe occurrences of each letter are: " << endl;
displayCounts(counts);
return 0;
}
89
// Display counts
void displayCounts(const int counts[])
{
for (int i = 0; i < 26; i++)
{
if ((i + 1) % 10 == 0)
cout << counts[i] << " " << static_cast<char>(i + 'a')
<< endl;
else
cout << counts[i] << " " << static_cast<char>(i + 'a')
<< " ";
} }
93
Specification Requirements
Need to process a students’ result :
–Calculate average test scores
–Determines and prints the names of all the
students whose test scores are below the
average test score.
–find the highest test score.
–Prints the names of all the students whose
test scores are the same as the highest
test score.
95
Problem analysis
Input - a list of students’ names and their test scores
Output :–
» average test scores
» list of student names whose test scores < average test score
» highest test score
» list of student names whose test scores are the same as the
highest score
Process / calculation :
» average test score = (sum of test scores)/ number of students
» print student names whose test scores < average test score
» find highest test score
» print student names whose test scores == highest test score
96