Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
Python and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for 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 or table - 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:

  float rain[31];

rain 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:

  float temp;
  int dayno;
  for (dayno=0; dayno<31; dayno++)
    {
    scanf("%f",&temp);
    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:

  float totalRainfall;
  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 and calloc 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 articles
C205 - C and C based languages - Arrays
  [4566] C - why is slow to write and debug) but fast to run? - (2015-11-01)
  [4338] Passing arrays into functions in C - (2014-12-02)
  [3144] Setting up arrays in C - fixed size at compile time, or dynamic - (2011-01-24)
  [3121] New year, new C Course - (2011-01-05)
  [3118] Arrays of arrays - or 2D arrays. How to program tables. - (2011-01-02)
  [2840] Just pass a pointer - do not duplicate the data - (2010-06-30)
  [2002] New C Examples - pointers, realloc, structs and more - (2009-01-20)
  [1614] When an array is not an array - (2008-04-17)

C238 - C and C based languages - Templates
  [3982] Using a vector within an object - C++ - (2013-01-19)
  [3810] Reading files, and using factories to create vectors of objects from the data in C++ - (2012-07-21)
  [3509] Operator Overloading, Exceptions, Pointers, References and Templates in C++ - new examples from our courses - (2011-11-06)
  [3388] Templates in C++ - defining a family pattern of methods / functions - (2011-08-12)
  [3252] C++ - unknown array size, unknown object type. Help! - (2011-04-17)
  [1478] Some new C programming examples - files, structs, unions etc - (2007-12-19)


Back to
C and C++ - preprocess, compile, load, run - what each step is for
Previous and next
or
Horse's mouth home
Forward to
Melksham - the way forward. 26th April, Well House Manor
Some other Articles
Events - Spring and Early Summer 2011 in Melksham
What have these pictures in common?
Light and dark at Green Park
Melksham - the way forward. 26th April, Well House Manor
Collections in C and C++ - arrays, vectors and heap memory blocks
C and C++ - preprocess, compile, load, run - what each step is for
Breaking the running sequence - an introduction to conditional statements and loops
How to return 2 values from a function (C++ and C) - more uses of pointers
Spring in the countryside near Melksham
Melksham 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 http://www.wellho.net/horse/ - 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).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2019: 404 The Spa • Melksham, Wiltshire • United Kingdom • SN12 6QL
PH: 01225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/3245_Col ... locks.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat