| |||||||||||
| |||||||||||
declaring an array and defining its length
Arrays example from a Well House Consultants training course
More on Arrays [link]
Source code: lp.c Module: C205
#include <stdio.h>
/* Arrays, and their length, are declared at compile time in C; you can use the preprocessor to set up the size as that runs before compile time, and that also avoids the need for the same constant to be hardcoded right through If you need to be yet more dynamic, functions such as malloc allow you to grab additional memory at run time and write a program that's adaptive to the requirements of a particular run with no hard limits set */ #define YEARS 17 main () { int years = YEARS; int i; float gift; float annual[YEARS]; gift = 200.00; /* Compound Interest on 200 pound over 17 years, storing results in an array */ for (i=0; i < years; i++) { printf("%d years - %.2f value\n",i,gift); annual[i] = gift; gift *= 1.05; } printf("After 7 years - %.2f\n", annual[7]); /* reusing array to look back and see how much would have had to be invested to make a certain sum by the end of the period - reusing the stored data */ for (i=1;i<years;i++) { float invest = annual[YEARS-1-i] / annual[YEARS-1] * 100.0; printf("Invest %.2f %d years ago\n",invest,i); } } /* Sample Output munchkin:c2 grahamellis$ ./lp 0 years - 200.00 value 1 years - 210.00 value 2 years - 220.50 value 3 years - 231.52 value 4 years - 243.10 value 5 years - 255.26 value 6 years - 268.02 value 7 years - 281.42 value 8 years - 295.49 value 9 years - 310.27 value 10 years - 325.78 value 11 years - 342.07 value 12 years - 359.17 value 13 years - 377.13 value 14 years - 395.99 value 15 years - 415.79 value 16 years - 436.57 value After 7 years - 281.42 Invest 95.24 1 years ago Invest 90.70 2 years ago Invest 86.38 3 years ago Invest 82.27 4 years ago Invest 78.35 5 years ago Invest 74.62 6 years ago Invest 71.07 7 years ago Invest 67.68 8 years ago Invest 64.46 9 years ago Invest 61.39 10 years ago Invest 58.47 11 years ago Invest 55.68 12 years ago Invest 53.03 13 years ago Invest 50.51 14 years ago Invest 48.10 15 years ago Invest 45.81 16 years ago munchkin:c2 grahamellis$ */ Learn about this subject
This module and example are covered on the following public courses:
* Learning to Program in C * Learning to program in C and C++ * Programming in C * C and C++ Programming * Learning to program in C and C++ * C and C++ Programming Also available on on site courses for larger groups Books covering this topic
Yes. We have over 700 books in our library. Books
covering C and C++ are listed here and when you've selected a
relevant book we'll link you on to Amazon to order.
Other Examples
This example comes from our "Arrays" training module. You'll find a description of the topic and some
other closely related examples on the "Arrays" module index page.
Full description of the source code
You can learn more about this example on the training courses listed on this page,
on which you'll be given a full set of training notes.
Many other training modules are available for download (for limited use) from our download centre under an Open Training Notes License. Other resources
• Our Solutions centre provides a number of longer technical articles.
• Our Opentalk forum archive provides a question and answer centre. • The Horse's mouth provides a daily tip or thought. • Further resources are available via the resources centre. • All of these resources can be searched through through our search engine • And there's a global index here. Purpose of this website
This is a sample program, class demonstration or answer from a
training course. It's main purpose
is to provide an after-course service to customers who have attended our
public private or
on site courses, but the examples are made
generally available under conditions described below.
Web site author
Conditions of use
Past attendees on our training courses are welcome to use individual
examples in the course of their programming, but must check
the examples they use to ensure that they are suitable for their
job. Remember that some of our examples show you how not to do
things - check in your notes. Well House Consultants take no responsibility
for the suitability of these example programs to customer's needs.
This program is copyright Well House Consultants Ltd. You are forbidden from using it for running your own training courses without our prior written permission. See our page on courseware provision for more details. Any of our images within this code may NOT be reused on a public URL without our prior permission. For Bona Fide personal use, we will often grant you permission provided that you provide a link back. Commercial use on a website will incur a license fee for each image used - details on request. |
| ||||||||||
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho PAGE: http://www.wellho.net/resources/ex.php • PAGE BUILT: Sun Oct 11 14:50:09 2020 • BUILD SYSTEM: JelliaJamb |