Professional Documents
Culture Documents
An array is like a large data variable that can store multiple data items of the same type. Arrays are a series of elements (variables) of the same type placed consecutively in memory that can be individually referenced by adding an index to a unique name. X
X[0]
Each individual element is referred to by the array name and the subscript (or index)
Arrays
We can store 5 values of type int without having to declare 5 different variables each one with a different identifier. Instead of that, using an array we can store 5 different values of the same type, int for example, with an unique identifier. Example: Use an array called billy to contain 5 integer values of type int.
0 1 2 3 4
billy
int The cells are numbered from 0 to 4 since in arrays the first index is always 0, independently of its length .
We can have arrays of any type and dimension: Example: char X[20] float Y[50] double Z[10] NOTE: The elements field within brackets [] when declaring an array must be a constant value, since arrays are blocks of static memory of a given size and the compiler must be able to determine exactly how much memory must assign to the array before any instruction is considered.
3
billy
But additionally, when we declare an Array, we have the possibility to assign initial values to each one of its elements using curly brackets { }. Example: int billy [5] = { 16, 2, 77, 40, 12071 }; 4
Exercise: Write a program to input an integer n (n<=10) value followed by n real-numbers into an array A of size 10 and display the maximum value stored as well as the position where it is stored in the array .
#include <iostream.h> void main() {int i,n; float A[10]; int maxposn; float max; cout<<Input value for n; cin>>n; cout<<Input<<n<<integer numbers; maxposn=0;
for (i=0;i<=n;i++) { cin>>A[i]; if (i= =0) max=A[i]; else if (A[i]>max) {max=A[i]; maxposn=i;} } cout<<largest is <<max<< at posn << maxposn; }
Exercise - Write a program to input the name of a person and display the number of vowels in the name.
void main() { char name[20]; char ch; int i,count; count = 0; cout<<Input name; cin>>name; i=0; while ((name[i]!=\0) && (i<20)) { ch=name[i]; if ((ch==A) || (ch==E)|| (ch==I) ||(ch==O)|| (ch==U)) count++; i++; } 10 cout<<No. of vowels is << count<<endl; }
11
12
It is not allowed to specify a size for the buffer using cin. What makes your program unstable in case that the user input is longer than the array that will host it.
15
Exercise: Write a program to store your name first name and surname and your address #include<iostream.h> #include<conio.h> void main() { char x[100], y[50]; clrscr(); cout<<"Please enter your name:"; cin.getline(x,50,'\n'); cout<<"Please enter your address:"; cin.getline(y,50,'\n'); cout<<"You entered the following values:"<<endl; cout<<"Name:"<<x<<" Address:"<<y; getch(); }
Strings (ctnd.)
16
Exercise: Write a program which makes use of two functions enter - to store your name first name and surname and your address and display to display back the values entered.
#include<iostream.h> #include<conio.h> //Function Prototypes void enter(char a[], char b[]); void display(char a[], char b[]); void main() { char x[100], y[50]; enter(x,y); display(x,y); } } void display(char a[], char b[]) { cout<<"You entered the following values:"<<endl; void enter(char a[], char b[]) { cout<<"Please enter your name:"; cin.getline(a,50,'\n'); cout<<"Please enter your address:"; cin.getline(b,50,'\n');
Strings (ctnd.)
cout<<"Name:"<<a<<" Address:"<<b;
}
17
jimmy represents a bidimensional array of 3 per 5 values of type int. (3x5 means 3 rows by 5 columns) The way to declare this array would be: int jimmy [3][5];
18
jimmy[1][3]
Note: Multidimensional arrays are not limited to two indices (two dimensions)
19
20
. 9 18
99 108
10 10 20
110 120
Storing Matrices
A 2-D Array can be viewed as a table consisting of rows and columns. Hence it can be used to store matrices. Exercise: Write a program to input two 3x3 matrices A and B, of integers, and output their sums.
#include <iostream.h> void main() { int A[3][3],B[3][3],C[3][3]; int i,j; cout<<Input the nine elements of A; for (i=0;i<3;i++) for (j=0;j<3;j++) cin>>A[i][j]; cout<<Input the nine elements of B; for (i=0;i<3;i++) for (j=0;j<3;j++) cin>>B[i][j];
for (i=0;i<3;i++) for (j=0;j<3;j++) C[i][j] =A[i][j]+B[i][j]; for (i=0;i<3;i++) { for (j=0;j<3;j++) cout<<C[i][j]; cout<<endl;} }
22
sum=sum+marks[i][j];
percentage[i] = sum/4;} for (i=0; i<10;i++)
{cout<<names[i];
for (j=0;j<4;j++) cout<<marks[i][j]<< percentage[i]<<endl;} }
24
base_type[][depth] For Example, a function with a multidimensional array as argument could be: void procedure (int myarray[][3]) Notice that the first bracket [] is void and the following one not. This must always be thus because the compiler must be able to determine within the function which is the depth of each additional dimension.
25
display(jimmy);