Scrigroup - Documente si articole


HomeDocumenteUploadResurseAlte limbi doc
AccessAdobe photoshopAlgoritmiAutocadBaze de dateCC sharp
CalculatoareCorel drawDot netExcelFox proFrontpageHardware
HtmlInternetJavaLinuxMatlabMs dosPascal
PhpPower pointRetele calculatoareSqlTutorialsWebdesignWindows

AspAutocadCDot netExcelFox proHtmlJava
LinuxMathcadPhotoshopPhpSqlVisual studioWindowsXml

Simple arrays


+ Font mai mare | - Font mai mic

Simple arrays

The use of arrays in C is often a problem for the beginner. The declaration of arrays isn't too difficult, especially the one-dimensional ones, but a constant source of confusion is the fact that their indices always count from 0. To declare an array of 5 ints, the declaration would look like this:

int something[5];

In array declarations C uses square brackets, as you can see. There is no support for arrays with indices whose ranges do not start at 0 and go up; in the example, the valid array elements are something[ to something[4]. Notice very carefully that something[ is not a valid array element.

This program reads some characters from its input, sorts them into the order suggested by their representation, then writes them back out. Work out what it does for yourself; the algorithm won't be given much attention in the explanation which follows.

#include <stdio>
#include <stdlib.h>
#define ARSIZE 10
lastchar = lastchar-1;

* Now the traditional bubble sort.
count1 = 0;
while(count1 < lastchar)
count2 = count2 + 1;
count1 = count1 + 1;

count1 = 0;
while(count1 <= lastchar)

Example 1.4

You might note that the defined constant ARSIZE is used everywhere instead of the actual array size. Because of that, to change the maximum number of characters that can be sorted by this program simply involves a change to one line and then re-compiling. Not so obvious but critical to the safety of the program is the detection of the array becoming full. Look carefully; you'll find that the program stops when element ARSIZE-1 has been filled. That is because in an N element array, only elements through to N-1 are available (giving N in total).

Unlike some other languages it is unlikely that you will be told if you 'run off' the end of an array in C. It results in what is known as undefined behaviour on the part of your program, this generally being to produce obscure errors in the future. Most skilled programmers avoid this happening by rigorous testing to make sure either that it can't happen given the particular algorithm in use, or by putting in an explicit test before accessing a particular member of an array. This is a common source of run-time errors in C; you have been warned.


Arrays always number from ; you have no choice.

A n-element array has members which number from to n-1 only. Element n does not exist and to access it is a big mistake.

Politica de confidentialitate | Termeni si conditii de utilizare



Vizualizari: 597
Importanta: rank

Comenteaza documentul:

Te rugam sa te autentifici sau sa iti faci cont pentru a putea comenta

Creaza cont nou

Termeni si conditii de utilizare | Contact
© SCRIGROUP 2024 . All rights reserved