For 2021 - online Python 3 training - see ((here)).
Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.
Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
Collections in C and C++ - arrays, vectors and heap memory blocks
If you want to hold multiple values within a table in a program, you'll want to use some form of collection
- and in many languages including C++ and C, the most straightforward type of collection is an array
. An array occupies sequential locations in the computer's memory when your program runs, and you can refer to individual values within it using either a positions number in square brackets, or by offsetting the address. I'll show you each of those, but let's start off by defining an array:
is to be an array that holds up to 31 values (position numbers 0 to 30), each of which is a floating point number. From the name and size chosen in the example, it's probably fair to assume that in this example we're going to be storing the rainfall (in mm perhaps) for each day of the month.
We can the store values into individual elements of the array by their position number:
for (dayno=0; dayno<31; dayno++)
rain[dayno] = temp;
And we can then access each of those values as may times as we like - here's an example of a loop that reads all through those values and comes up with a total monthly rainfall:
totalRainfall = 0.0;
for (dayno=0; dayno<31; dayno++)
totalRainfall += rain[dayno];
There's a complete example (from which those code snippets are taken) [here]
. Rather than type in 31 values every time I read the program, I redirected input from a data file and there's a sample data file [here]
Not all months have 31 days ... and indeed you'll very often have applications in which you don't even know a maximum array size when you compile. The standard library (stdlib.h) in C includes functions to let you allocate memory as you run the program - malloc
let you select an amount of memory of your choice, and realloc
even lets you change the amount of memory you need during a run. In C++, a vector
provides a neat and easier to use wrapper around functions such as calloc
, allowing true, easy(ish) dynamic programming.
There's an example that uses calloc
as a comparison to the example above - it's [here]
. And there's a first example with vectors / C++ [here]
. (written 2011-04-12)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articlesC238 - C and C based languages - Templates 
Some new C programming examples - files, structs, unions etc - (2007-12-19) 
C++ - unknown array size, unknown object type. Help! - (2011-04-17) 
Templates in C++ - defining a family pattern of methods / functions - (2011-08-12) 
Operator Overloading, Exceptions, Pointers, References and Templates in C++ - new examples from our courses - (2011-11-06) 
Reading files, and using factories to create vectors of objects from the data in C++ - (2012-07-21) 
Using a vector within an object - C++ - (2013-01-19)C205 - C and C based languages - Arrays 
When an array is not an array - (2008-04-17) 
New C Examples - pointers, realloc, structs and more - (2009-01-20) 
Just pass a pointer - do not duplicate the data - (2010-06-30) 
Arrays of arrays - or 2D arrays. How to program tables. - (2011-01-02) 
New year, new C Course - (2011-01-05) 
Setting up arrays in C - fixed size at compile time, or dynamic - (2011-01-24) 
Passing arrays into functions in C - (2014-12-02) 
C - why is slow to write and debug) but fast to run? - (2015-11-01)
Some other Articles
Events - Spring and Early Summer 2011 in MelkshamWhat have these pictures in common?Light and dark at Green ParkMelksham - the way forward. 26th April, Well House ManorCollections in C and C++ - arrays, vectors and heap memory blocksC and C++ - preprocess, compile, load, run - what each step is forBreaking the running sequence - an introduction to conditional statements and loopsHow to return 2 values from a function (C++ and C) - more uses of pointersSpring in the countryside near MelkshamMelksham Town Council - vacancy in the Spa Ward
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page
This is a page archived from The Horse's Mouth at
the diary and writings of Graham Ellis.
Every attempt was made to provide current information at the time the
page was written, but things do move forward in our business - new software
releases, price changes, new techniques. Please check back via
our main site for current courses,
prices, versions, etc - any mention of a price in "The Horse's Mouth"
cannot be taken as an offer to supply at that price.
Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).