Beruflich Dokumente
Kultur Dokumente
2010/2011
In this chapter …
o Concept of arrays
o Arrays and strings
o Character input and output
o Reading strings using scanf()
o Strings with gets() and puts()
o Multi-dimensional arrays
o Common pitfalls with arrays in C
2
Concept of arrays
X 53
Variable name
Memory
Location
Contents
3
Concept of arrays
o Whilst standard variables are of course very useful,
there are applications where they are not practical.
o Arrays are most useful when the data are naturally
expressed as a contiguous sequence of values.
• Example: Write a program which inputs a number between 0-10
and displays the equivalent page number of that section in a book.
Index
0 1 2 ….. 8 9 10
X 0 1 16 ….. 66 81 93
Variable name
Memory locations
Contents
4
Declaration and assignment
5
Declaration and assignment
o Very important: An array index starts at 0.
The first element of the array corresponds to index 0,
the second one to index 1, more generally the nth
element corresponds to index n-1.
o To assign a value to a particular element of the array:
Alphabet[2]=‘C’;
o Note: the array is not initiliazed by default.
o The brackets are similar to function call operator and
has precedence over most operators.
o It is possible to have an expression inside the
brackets.
6
Initialization
int Odd[5]=
Odd[5];
Odd[ /* Initialize
]= {1,3,5,7,9}
{1,3,5,7,9}; /* array array
; Initialize */ */
Odd[0]=1;
int
// Ifindex=0,value; // Option 1
the array is Initialized at Declaration
Odd[1]=3;
// The number
Option of //
3 Initialize Simple
elements
array atassignment
need not be specified.
Declaration
Odd[2]=5;
for(value=1;value<11;value+=2,index++)
//Have
// Compiler will assign
to initialize correct
every array size.
element
Odd[3]=7;
Odd[index]=value; // Option 2: Use for loop to
Odd[4]=9;
// The number of element //simplify assignment.
in the list should be equal
// to the size of the array
// If you specify less element in the list, the
// remaining cell of the array will be initialized
to 0
// If you specify more, a syntax error would appear
7
Initialization
8
Examples using arrays
#include <stdio.h>
void main (void) {
float amounts[10];
int i;
printf (“US Dollar to UK Pound converter: \n\n”);
printf (“%10s %10s\n”, “Dollars”, “Pounds”);
for (i = 0; i < 10; i++) {
amounts[i] = (i+1)*5.00;
printf (“%10.2f %10.2f\n”, amounts[i], (amounts[i] /
1.66));}
}
9
Example using array
10
Strings and character arrays
11
Strings and character arrays
o C gives an easier way to initialize an array
without counting the elements by using an
unsized array by automatically calculating the
dimensions of array big enough to hold all the
characters including the terminating ‘\0’
o Bad practice example
char word [10];
word[0] = ‘H’;
word[1] = ‘e’;
word[2] = ‘l’;
word[3] = ‘l’;
word[4] = ‘o’;
word[5] = ‘\0’;
printf(“%s”, word);
12
Strings and character arrays
o Better practice?
13
Strings and character arrays
#include<stdio.h>
void main(void)
{
char Name1[ ] = {‘D’,’a’,’v’,’i’,’d’};
char Name2[ ] = {“David”};
int x;
for(x=0;x<5;x++) printf(“%c”,Name1[x]);
printf(“%s”,Name2);
}
14
Strings and character arrays
15
Strings and character arrays
o Example 2: Create an array which can store a
string of five characters inputted from the
keyboard.
#include<stdio.h>
void main()
{
char Text[6];
int x;
for(x=0;x<6;x++) scanf(“%c”,&Text[x]);
for(x=0;x<6;x++) printf(“%c”,Text[x]);
17
Strings and character arrays
#include<stdio.h>
void main(void)
{
char Text[6];
scanf(“%s”, &Text);
printf(“%s”,Text);
}
Note: When using an array of char as a string
we do not use index.
18
Reminder: Character I/O
Example
{
char Key;
do
Key=getchar();
while(Key!=‘Q’);
}
19
Reminder: Character I/O
Example
{
int i = 0;
char line [] ={“Hello\n”};
while(line [i] ! = ‘\0’) {
putchar (line [i]);
i++;
}
}
20
Reading strings using scanf( )
21
More problems with scanf
22
gets() and puts()
23
gets() and puts()
o Example
#include<stdio.h>
void main(void)
{
char Input_String[40];
gets(Input_String); // Read string
puts(Input_String); // Display string
24
gets() and puts()
25
Multidimensional array
0 1 2 ….. 23 24 25
char X[26]; X A B C ….. X Y Z
char X[2][26]; X a b c ….. x y z
X α β χ ….. ξ ψ ζ
char X[n][26];
26
Multidimensional array
o Example
void main ()
{
Char text[][13]={“first text\n”,
“second text\n”,
“third text\n”,
“last text\n”};
int counter1;
/*print complete strings*/
for (counter1=0; counter1<4; counter1++)
printf(“%s”, text[counter1]);
}
27
Example
# include <stdio.h>
void main ()
{
double amounts[2][10];
char titles[2][7] = {"Pounds", "Euros"};
double rates[] = {0.6078, 0.6654};
int i, j;
28
Example
29
Exercise 1
30
Exercise 1
31
Exercise 1
#include<stdio.h>
void main()
{
char Place[5][5][15]={“Britain”, “London”, “Coventry”, “Birmingham”,
“Manchester”, “France”, “ Paris”, “Marseille”, “Toulouse”, “Lyon”,
“Sweden”,”Malmo”, “Stockholm”, “” ,“”,“Germany”, ”Berlin”,
“Frankfurt”, “Dresden”, “”, “Spain”, “ Barcelona”, “Vigo”, “Madrid”,
“Seville”};
int C;
for(C=1;C<5;C++)
printf(“%s ”,Place[C][0]);
}
What is display on the screen?
32
Exercise 1
33
Exercise 1
#include<stdio.h>
void main()
int C;
34
Exercise 1
35
Common pitfalls with arrays in C
36