Please send questions to
st10@humboldt.edu .
* C++ arrays...
* like Python lists, a C++ array gives a single name
to a collection of values
...hopefully logically connected in some way
* in a C++ array, ALL of the contents are of the SAME type
(unlike a Python list)
* and a C++ array has a GIVEN size, which cannot change;
* SO: a C++ array declaration has to tell the C++ compiler
THREE things:
...the name of the array,
...the type of its elements,
...how many of those elements there are
type name[size];
* style point: if the size is a set, known quantity,
(that doesn't change), it should be declared as a
named constant
* examples:
const int NUM_STUDENTS = 10;
double grades[NUM_STUDENTS];
* you can initialize an array at the time of declaration
by setting the array to a comma-separated list of
values surrounded by curly braces { }
double grades[NUM_STUDENTS] = {0, 0, 0, 0, 0,
0, 0, 0, 0, 0};
const int NUM_COINS = 5;
const double COIN_TYPES[NUM_COINS] = {.01, .05, .10, .25, 1.0};
* a C++ array's indices are a lot like Python's ---
for an array of NUM_ELEMENTS, the indices are 0 to (NUM_ELEMENTS-1),
and you indicate the element in an array with square brackets
[ ] with the index inside ---
that is, COIN_TYPES[0] contains the value .01,
grades[3] contains the value 0
* and if an array isn't constant, you can change a particular
element of an array with an index, too:
grades[3] = 99.5; // have set the 4th element in array grades
// to the value 99.5
int index = 0;
while (index < NUM_GRADES)
{
cout << "enter next grade: ";
cin >> grades[index];
index++;
}
* like Python, note that
grades <-- the whole array
grades[5] <-- the single element with index 5 inside array grades
* and, as you can guess from the example above,
when you do something to everything in an array,
you often use a count-controlled loop
(of course, you don't HAVE to do stuff to everything
in an array --- you get to choose!)
* few more examples:
what if you want to print to the screen everything in
an array?
int index = 0;
while (index < ARR_SIZE)
{
cout << arr[index] << endl;
index = index + 1;
}
what if you want to raise everyone's grade by 10%?
double percent_incr;
int index = 0;
cout << "what is the percentage increase? (.1, .33, etc) ";
cin >> percent_incr;
while (index < NUM_STUDENTS)
{
grades[index] = grades[index] * (1 + percent_incr);
// grades[index] + (grades[index] * percent_incr)
index += 1;
}
* note: in C++, a function cannot look at an array argument
and tell its size...
SO: whenever you pass an array to a function, you SHOULD
pass its size, also.
* example: what if I'd like a function sum_contents that simply
takes any double array and its size, and returns the sum
of its contents?
see: sum_contents.cpp, test_sum_contents.cpp
* a more CONVENIENT count-controlled loop: the for loop
int ct = 0;
while (ct < limit)
{
// do what you want
ct = ct + 1;
}
for (int ct = 0; ct < limit; ct = ct + 1)
{
// do what you want
}
* the two above do the SAME thing, EXCEPT the scope
of ct is the whole function for the while version,
and the scope of ct is ONLY the for-loop in the for version.
in general, then:
for (initialize_steps; condition; updating_steps)
{
// do what you want
}
examples...
for (int index = 0; index < NUM_GRADES; index++)
{
cout << "enter next grade: ";
cin >> grades[index];
}
* sum_contents2.cpp, test_sum_contents2.cpp: version with
a for-loop
* NOTE: C++ has TWO kinds of strings:
* the more-modern string class, available when you #include <string>
(or <iostream>!)
* the older C-string, a special array of char
* I tend to avoid the older C-string, but sometimes you can't;
...a string literal, "Hi", is an old-style C-string;
...the file i/o described below expects an old-style C-string
as one of its arguments;
* if you are writing a function that NEED an on-style C-string
as its parameter, that parameter can be declared using
char *old_style_c_string
* ...because, as you'll find out further in CIS 230 and
CIS 291, an array is represented as a POINTER to
its first element --- that's what the * means above;
* (for the argument, declare as an array of char...
char c_string_arg[MAX_STRING_LENGTH];
... or use a string literal as the argument! 8-) )
* one modern-string gotcha: if you have a string parameter,
will need to #include <string> or <iostream> in the .h file
as well as the .cpp file...
#ifndef DO_SOMETHING_H
#define DO_SOMETHING_H
#include <string>
using namespace std;
void do_something(string word);
#endif
* file input/output in C++
* FIRST: this is in the <fstream> C++ library
... #include <fstream>
when you use this.
* SECOND: the idea of a physical file in Python and C++
are pretty much the same...
* in C++, you create an input file stream or an output file
stream, and then you open the stream, do something with it,
and close it;
* how do you declare it? either as an input file stream (ifstream)
or as an output file stream (ofstream)
ifstream instream;
ofstream outstream;
* how do I open the stream?
... I call the open method for the stream with the
name of the physical file expressed as an old-style
C-string
instream.open("scores.txt");
outstream.open("looky.txt");
* how do I read from an ifstream?
...like reading from the keyboard using cin!
instream >> variable;
* how to I write to an ofstream?
...like writing to the screen using cout!
outstream << "whatever" << whatever << endl;
* when done... close 'em
instream.close();
outstream.close();
* examples: get_file_contents.cpp, test_get_file_contents.cpp
put_contents.cpp, test_put_contents.cpp