Transcript Arrays

An Array
• A sequence of elements of a particular type
• Each element in the array has an index which gives its
position in the sequence
• An array is declared
• int a[10] which declares an array of 10 integers whose
indices go from 0-9
Length of Arrays
– The length of the array is set when it is created. It cannot change.
•
Individual array elements are accessed via an index.
– Array index numbering starts at 0.
Arrays
a0
An array: an object that can hold a fixed number of values
of the same type. Array to the right contains 4 int values.
The type of the array to the right is
int
Here is a variable that contains the name of
the array.
x a0
0
5
1
7
2
4
3
-2
int[]
A declaration has the basic form
<type> <variable-name>[arraylength] ;
int x[10]
;
Elements of the array are numbered 0, 1, 2, …, x.length–1;
length is a variable, not a function, so don’t put () after it.
Make everything as simple as possible, but no simpler. Einstein
Arrays
Assign 5 to array element 2 and
-4 to array element 0
x[2]= 5;
x[0]= -4;
x[2] is a reference to element
number 2 of array x
a0
a0
int k= 3;
x[k]= 2* x[0];
x[k-1]= 6;
-4
0
5
0
0
1
2
3
Assign 2*x[0], i.e. -8, to x[3]
Assign 6 to x[2]
0
1
2
3
-4
0
6
-8
Array initializers
a0
Instead of
5
int c[5];
c[0]= 5; c[1]= 4; c[2]= 7; c[3]= 6; c[4]= 5;
4
7
6
Use an array initializer:
int c[5]= {5, 4, 7, 6, 5};
5
array initializer: gives the values to be in the
array initially. The values must all have the
same type, in this case, int. The length of the
array is the number of values in the list
Computer science has its field called computational complexity;
mine is called computational simplicity. Gries
Processing Array Elements
• Access individual elements of an array
using:
– the name (handle) of the array
– a number (index or subscript) that tells which
of the element of the array
count
[0]
[1]
[2]
[3]
5
-7
9
27
• What value is stored in count[2]?
Processing Array Elements
• Often a for( ) loop is used to process
each of the elements of the array in turn.
for (int i = 0; i < NUM_STUDENTS; i++)
{);
}
• The loop control variable, i, is used as the
index to access array components
Some exercises
• Write a program which finds the maximum
of an array
• Write a program which finds the average
of a list of integers
Largest of 10 Integers
#Include <stdio.h>
main()
{ int i, a,max;
printf(“enter number \n”);
scanf(“%d”,&a);
max = a;
for (i=1;I < 10; i++)
{ printf(“enter number \n”);
scanf(“%d”,&a);
If (a > max) max = a;
};
printf(“largest is %d”,max);
}
Largest of 10 Integers
#Include <stdio.h>
main()
{ int i, max;
int a[10];
printf(“enter number \n”);
scanf(“%d”,&a[0]);
max = a[0];
for (i=1;I < 10; i++)
{ printf(“enter number \n”);
scanf(“%d”,&a[i]);
If (a[i] > max) max = a[i];
};
printf(“largest is %d”,max);
}
Largest of 10 Integers
#Include <stdio.h>
main()
{ int i, max;
int a[10]= {7,3,4,28,4,56,12,3,44,7}
max = a[0];
for (i=1;I < 10; i++)
If (a[i] > max) max = a[i];
};
printf(“largest is %d”,max);
}
Multi-dimensional Arrays
•
Arrays with multiple dimensions can also be created.
declaration syntax: type
arrayName[rowlength]
[collength];
•
They are created and initialized in the same way as single
dimensioned arrays.
intgrades[20][5];
for(int i = 0; i< 20; i++)
for(int j = 0; j<5; j++)
grades[i][j] = 100;
Exercises
• Write Programs to
• 1: given an array of 20
Integers print out all those
> 15 and print out their
positions.
• 2: Given an array of 20
integers, delete the 5th
element so that:
• Before
• 17-15-14-23-22-19-etc
• After
• 17-15-14-23-19-etc
Exercise
• Write a program which takes an array and
reverses it.
We could use scanf and a for loop
•
•
•
•
•
•
•
•
•
•
•
•
•
•
#include <stdio.h>
main()
{
int a[10]
int i
for ( i = 0;i < 10 ; i++)
{ printf(“enter a number”);
scanf(“%d”,&a[i]);
}
for ( i = 0;i < 10 ; i++)
printf(“the %d th number in the arrray is %d \n”,i,a[i]);
for ( i = 9;i >= 0 ; i--)
printf(“the %d th number in the arrray is %d \n”,i,a[i]);
}
Exercise
• Write a program to enter 10 numbers into
an array and to print out the maximum
element of the array.
We could use scanf and a for loop
•
•
•
•
•
•
•
•
•
#include <stdio.h>
main()
{
int a[10]
int I;
Int max;
Printf(“enter first number”);
scanf(“%d”,&a[0]);
max = a[0];
•
•
•
•
•
•
•
•
for ( i = 1;i < 10 ; i++)
{ printf(“enter a number”);
scanf(“%d”,&a[i]);
If (a[i] > max)
max = a[i];
}
printf(“the max number in the arrray is %d \n”,max);
}