Documente Academic
Documente Profesional
Documente Cultură
Note:
Read and try to understand the document before attempting to write the
example code.
1
Array basics
Not much to it. The variable age is created at line (5) as a short. A value is assigned to
age. Finally, age is printed to the screen.
Now let's keep track of 4 ages instead of just one. We could create 4 separate variables,
but 4 separate variables have limited appeal. (If using 4 separate variables is appealing to
you, then consider keeping track of 93843 ages instead of just 4). Rather than using 4
separate variables, we'll use an array.
2
On line (5), an array of 4 short's is created. Values are assigned to each variable in the
array on line (6) through line (9).
Accessing any single short variable, or element, in the array is straightforward. Simply
provide a number in square braces next to the name of the array. The number identifies
which of the 4 elements in the array you want to access.
The program above shows that the first element of an array is accessed
with the number 0 rather than 1. Later in the tutorial, We'll discuss why
0 is used to indicate the first element in the array.
Printing arrays
Our program is a bit unrevealing in that we never print the array to screen. Here is the
same program with an attempt to print the array to the screen:
Line (11) is meant to print the 4 ages to the screen. But instead of printing out the four
short variables, what appears to be nonsense prints out instead.
What the "nonsense" output actually is and why the 4 array values were
not printed will be addressed later in the tutorial. For now, the
important point to come away with is that simply providing the name
3
of the array in an output statement will not print out the elements of the
array.
How about printing out each of the values separately? Try this:
Lines (10) through line (13) produce the output we are expecting.
There is no single statement in the language that says "print an entire array to the screen".
Each element in the array must be printed to the screen individually.
Copying arrays
Suppose that after filling our 4 element array with values, we need to copy that array to
another array of 4 short's? Try this:
4
Line (12) tries to copy the age array into the same_age array. What happened when you
tried to compile the program above?
The point here is that simply assigning one array to another will not
copy the elements of the array. The hard question to answer is why the
code doesn't compile. Later in the tutorial this example will be re-
examined to explain why line (12) doesn't work. This code should not
compile on either C or C++ compilers. However, some older C++
compilers may ignore the ISO C++ standard and allow line 12 to
compile. If it does compile on your C++ compiler, make a mental note
that it is incorrect behavior.
Let's try copying arrays using a technique similar to the technique used to print arrays
(that is, one element at a time):
This technique for copying arrays works fine. Two arrays are created: age and same_age.
Each element of the age array is assigned a value. Then, in order to copy of the four
elements in age into the same_age array, we must do it element by element.
5
Copy first element
Like printing arrays, there is no single statement in the language that says "copy an
entire array into another array". The array elements must be copied individually.
The technique used to copy one array into another is exactly the same as the technique
used to copy 4 separate variables into 4 other variables. So what is the advantage to using
arrays over separate variables?
One significant advantage of an array over separate variables is the name. In our
examples, using four separate variables requires 4 unique names. The 4 short variables
in our array have the same name, age. The 4 short's in the array are identical except for
an index number used to access them. This distinction allows us to shorten our code in a
way that would be impossible with 4 variables, each with unique names:
6
8: age[0]=23;
9: age[1]=34;
10: age[2]=65;
11: age[3]=74;
12:
13: for(i=0; i<4; i++)
14: same_age[i]=age[i];
15:
16: for(j=0; j<4; j++)
17: cout << same_age[j] << endl;
18: return 0;
19: }
Since the only difference between each of the short's in the arrays is their index, a loop
and a counter can be used to more easily copy all of the elements. The same technique is
used to shorten the code that prints the array to the screen.
Even though arrays give us some convenience when managing many variables of the
same type, there is little difference between an array and variables declared individually.
There is no single statement to copy an array, there is no single statement to print an
array.
#include <iostream>
using namespace std;
int main() {
int a[1000]; // Declare an array of 1000 ints
int n = 0; // Number of values in a.
return 0;
}
7
Why read numbers into memory
The previous example, which reads numbers into an array then sums the elements of the
array, is not a convincing use of arrays. It would have been just as easy to add them while
we were reading them.
But usually the computation can not be performed while reading, for example, sorting
them and printing them in order by their value. An even simpler example is given below -
printing them in reverse order of input.
int main() {
//--- Declare array and number of elements in it.
float a[100000];
int n; // Number of values currenlty in array.
return 0;
}//end main
8
Exchanging Values of Variables
It is sometimes necessary to shuffle the order of the elements of an array.
Sorting arrays calls for the values of some elements to be exchanged. It would
therefore be helpful to first learn the technique of swapping variables.
How would you swap the values of the variables, let say, num1 and num2 (that is
exchanging the value of num1 in num2)?
You must use a third variable as in the following example:
third_var = num1;
num1 = num2;
num2 = third_var;
Sorting Variables
9
// a simple sorting program that sort a list of n
#include <iostream>
int main()
cin>>n;
cin>>list[i];
// do the sorting...
10
if(list[i] > list[j])
temp = list[i];
list[i] = list[j];
list[j] = temp;
cout<<" "<<list[i];
cout<<endl;
return 0;
Output:
11
Exercise
• Write a program to find the smallest number in an array ?
• Write a program that calculates the grade for 20 students, grades shall be
calculated according to the following rule
12