This lesson you will learn:
- The use of one dimensional arrays
- The difference between strings and characters
Q. What's the difference between a blonde and an ironing
board ?
A. Occasionally you have trouble getting the legs apart
on an ironing board.
Arrays are a data structure which
hold multiple variables of the same data types. Consider
the case where a programmer needs to keep track of a number
of people within an organization. So far, our initial attempt
will be to create a specific variable for each use. This
might look like:
int name1 = 101;
int name2 = 232;
int name3 = 231;
It becomes increasingly more difficult to keep track of
this as the number of variables increase. Arrays offer a
solution to this problem. An array is a multi-element box,
a bit like a filing cabinet, and uses an indexing system
to find each variable stored within it. In C, indexing starts
at zero.
Arrays, like other variables in C, must be declared before
they can be used. The replacement of the above example using
arrays looks like this:
int names[3];
names[0] = 101;
names[1] = 232;
names[2] = 231;
names[3] = 0;
We created an array called names, which has space for four
integer variables. You may also see that we stored 0 in
the last space of the array. This is a common technique
to signify the end of an array. Arrays have the following
syntax, using square brackets to access each indexed value
(called an element).
x[i];
So that x[5] refers to the sixth element of an array called
x. In C, an array elements start with 0. Assigning values
to array elements is done by:
x[10] = g;
And assigning array elements to a variable is done by:
g = x[10];
In the following example, a character based array named
work is declared, and each element is assigned a character.
The last element is filled with a zero value, to signify
the end of the character string (in C, there is no string
type, so character based arrays are used to hold strings).
A printf statement is then used to print out all the elements
of the array.
Example 1:
/* Introducing arrays, 2 */
#include <stdio.h>
int main(void)
{
char work[20];
word[0] = ‘H’;
word[1] = ‘e’;
word[2] = ‘l’;
word[3] = ‘l’;
word[4] = ‘o’;
word[5] = 0;
printf(“The contents of word[] is -? %s\n”,
word);
return 0;
}
Sample program output:
The contents of word[] is Hello
Accepting single characters from the keyboard
getchar()
The following program illustrates this:
#include <stdio.h>
int main(void)
{
int I;
int ch;
for (I = 1; i<=5; ++i)
{
ch = getchar();
putchar();
}
return 0;
}
Sample program output
AACCddEEtt
The program reads five character (one for each iteration
of the for loop) from the keyboard. Note that getchar()
gets a single character from the keyboard, and putchar()
writes a single character (in this case, ch) to the console
screen.
Reading Strings
#include <stdio.h>
int main(void)
{
char name[25];
printf("Input a character string, up to 25 characters.
\n");
gets(name):
printf("The string is %s\n", name);
printf("End of program.\n");
return 0;
}
gets collects a string of characters terminated by a new
line from the standard input stream and puts it into name.
It replaces the new line by a null character (\0) in name;
it also allows input strings to contain certain characters
(spaces, tabs).
A sample program showing the advantage of using
an array (float)
#include <stdio.h>
int main(void)
{
float wageWk1, wageWk2, wageW3....... 47 more variables
..... wageWk51, wageWk52;
printf("\nEnter the wage for week 1")
scanf("%f", &wageWk1);
printf("\nEnter the wage for week 2");
scanf("%f", &wageWk2);
........... NINTEY-FOUR MORE LINES OF CODE
printf("\nEnter the wage for week 51");
scanf("%f", &wageWk51);
printf("\nEnter the wage for week 52");
scanf("%f", &wageWk52);
return 0;
}
Use of an Array
#include <stdio.h
int main(void)
{
float wageWk[52];
int cntr, numofwks;
printf("\nHow many weeks of wages do you want to
enter?");
scanf("%d", &numofwks);
cntr=0;
while (cntr <numofwks && cntr <52)
{
printf("\nEnter the wages
for week %d>), cntr+1);
scanf("%f", &wageWk[cntr]);
cntr = cntr+1;
}
return 0;
}
back to top exercise lesson
6 lesson
8