Week 7-8
Week 7-8
C[0] 24
C[1] 59
C[2] 35
C[3] ...
C[4] ...
C[5] ...
C[6] ...
C[7] ...
C[8] ...
C[9] ...
Index
Declaration of Arrays
To declare an array, define the variable type, specify the
name of the array followed by square brackets and specify
the number of elements it should store:
arrayType arrayName [numberOfElements ];
For example ,
int age [ 10 ] ;
More than one array can be declared on a line
int age [10] , height [10] , names [20] ;
int age[ 10 ] = { 0 } ;
Initializing an Array
int age [ 10 ] ;
for ( i = 0 ; i < 10 ; i ++ )
{
age [ i ] = 0 ;
}
Initializing an Array
int age [ ] =
{ 1,2,3,4,5,6,7,8,9,10 } ;
for ( i = 0 ; i < 10 ; i ++ )
‘ i ‘ will have value from 0 to 9
Initializing array by taking input from user
for ( i = 0 ; i < 10 ; i ++ )
{
cin >> age [ i ] ;
}
Accessing array element
#include <iostream>
#include <string>
using namespace std;
int main() {
string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
cout << cars[0];
return 0;
}
Note: Array indexes start with 0: [0] is the first element. [1] is the
second element,
Loop Through an Array
#include <iostream>
#include <string>
using namespace std;
int main() {
string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
for(int i = 0; i < 4; i++) {
cout << i << ": " << cars[i] << "\n";
}
return 0;
}
Example
#include <iostream>
using namespace std;
main ( )
{
int c [ 100 ] ;
int z , i = 0 ;
do
{
cout<<"Please enter numbers";
cin >> z ;
if ( z != -1 )
c[ i ] = z ;
i ++ ;
} while ( z != -1 && i < 100 ) ;
cout << "The total number of positive integers entered by user is " << i -1;
}
Example
#include <iostream>
using namespace std;
int main() {
int numbers[5];
cout << "Enter 5 numbers: " << endl;
return 0;
}
Manipulation of Array
Elements
Lecture Link:
https://www.youtube.com/watch?
v=zaQfj2bl3j4&list=PLVEVLI2v6thVDz7UxUPnURUKaqWFK7Z7v&index=25
Copying Arrays
– Data types should be
identical
b[0]=a[0];
b[1]=a[1];
b[2]=a[2];
b[3]=a[3];
………
………
b [ 10 ] = a [ 10 ] ;
Copying Arrays
for ( i =0 ; i < 10 ; i ++ )
b[i]=a[i];
Example
#include <iostream>
using namespace std;
int main() {
int a [ 10 ] ={3,4,5,6,7,8,9,10,11,12};
int b [ 10 ] ;
}
Example
Take the sum of squares of 10 different numbers which are stored in an
array
#include <iostream>
using namespace std;
int main() {
int a [ 5 ] ={1,2,3,4,5};
int arraySize =5 ;
int sumOfSquares = 0 ;
for ( int i = 0 ; i < arraySize ; i ++ )
{
sumOfSquares = sumOfSquares + a [ i ] * a [ i ] ;
}
cout<<"Sum of squares of array elements are: "<<sumOfSquares;
}
Example
#include <iostream>
using namespace std;
int main()
{
int i, n;
float arr[100];
cout << "Enter total number of elements(1 to 100): ";
cin >> n;
cout << endl;
// Store number entered by the user
for(i = 0; i < n; ++i)
{
cout << "Enter Number " << i + 1 << " : ";
cin >> arr[i];
}
// Loop to store largest number to arr[0]
for(i = 1;i < n; ++i)
{
// Change < to > if you want to find the smallest element
if(arr[0] < arr[i])
arr[0] = arr[i];
}
cout << "Largest element = " << arr[0];
return 0;
}
const
const int arraySize = 100 ;