0% found this document useful (0 votes)
38 views20 pages

Array Fun

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views20 pages

Array Fun

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Array

Array
An array is a collection of elements of the same type.

How to declare Array in C

int num[35]; /* An integer array of 35 elements */


char ch[10]; /* An array of characters for 10 elements */
double f[5]; /* array of 5 doubles: f[0], . . ., f[4]*/
Each element of the array is referenced by its index, that is, a number from 0 to N −1.
int mydata[20];
mydata[0] /* first element of array mydata*/
mydata[19] /* last (20th) element of array mydata*

f[4] = 2.5;
m[2] = 4;
cout << f[4]; // outputs f[4], which is 2.5
Various ways to initialize an array
In the above example, we have just declared the array and later we initialized it with the values input
by user. However you can also initialize the array during declaration like this:

int arr[5] = {1, 2, 3, 4 ,5}; OR (both are same) int arr[] = {1, 2, 3, 4, 5};

double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};

char c[ ] = {’c’, ’a’, ’t’}; // declares and initializes c[3]

int children[3] = {2, 12, 1};


The above declaration is equivalent to the following code:

int children[3];
children[0] = 2;
c[0]=‘c’;
children[1] = 12;
c[1]=‘a’
Exercises
 What is the output of the following code?
char symbol[3] = {’a’, ’b’, ’c’};
for (int i = 0; i < 3; i++)
cout << symbol[i];

 What is the output of the following code?


double a[3] = {1.1, 2.2, 3.3};
cout << a[0] << " " << a[1] << " " << a[2] << endl;
#include <iostream >
using namespace std;
#include <iomanip>
using std::setw;
int main ()
{
int n[ 10 ]; // n is an
array of 10 integers
Element
for ( int i = 0; i < 10; i++ ) Value
0
n[ i ] = i + 100; // set 100
element at location i to i + 100 1
101
cout << "Elemnet<<“\t’’<< "Value" << endl; 2
102
for ( int j = 0; j < 10; j++ ) 3
103
cout << “\t”<< n[ j ] << endl; 4
104
return 0; 5
} 105
6
Example array

#include<iostream>
using namespace std;
int main()
{
int ar1[3]={10,20,30}, ar2[3];
ar2[0]=ar1[2];
cout<<“element 0 in ar2= "<<ar2[0];
return 0;
}
Example elements array summation
#include<iostream>
using namespace std;
int main()
{
int sum=0, ar[3];
for (int i=0; i<3 ;i++)
{
cin>>ar[i];
sum =sum+ar[i];
cout<<"sum of array elements= "<<sum;
//Reads in 5 scores and shows the highest score.

#include <iostream>
int main( )
{
using namespace std;
int i, score[5], max=0;
cout << "Enter 5 scores:\n";
cin >> score[0];
max = score[0];
for (i = 1; i < 5; i++)
{
cin >> score[i];
if (score[i] > max)
max = score[i]; //max is the largest of the values score[0],..., score[i].
}
cout << "The highest score is " << max << endl;
Return 0;
}
Array of int elements find the sum of odd
and sum of even elements
int s=3, sume=0,sumo=0, ar[s]={10,25,30};
for (int i=0; i<s ;i++)
{
if (ar[i] %2 ==0)
sume=sume + ar[i];
else
sumo=sumo + ar[i];
}
cout<<"sum of even= "<<sume<<"sumod"<<sumo;
A two-dimensional array
 A two-dimensional array is implemented as an “array of arrays.”
 For example “int A[15][30]” declares A to be an array of 30 objects, each of which is an array of 15 integers.
 An element in such an array is indexed as A[i][j], where i is in the range 0 to 14 and j is in the range 0 to 29.
 When declaring an array, we can initialize its values by enclosing the elements in curly braces ({...}). When
doing so, we do not have to specify the size of the array, since the compiler can figure this out.
 array. Such a two-dimensional array is sometimes also called a matrix. In C++, we declare a two-
dimensional array as follows:
 int M[8][10]; // matrix with 8 rows and 10 columns
 This statement creates a two-dimensional “array of arrays,” M, which is 8×10, having 8 rows and
10 columns. That is, M is an array of length 8 such that each element of M is an array of length 10
of integers.

 Figure: A two-dimensional integer array that has 8 rows and 10 columns. The value of M[3][5] is
100 and the value of M[6][2] is 632.
 Arrays are nice and simple for storing things in a certain order, but they have drawbacks.
They are not very adaptable. For instance, we have to fix the size n of an array in
advance, which makes resizing an array difficult. Insertions and deletions are difficult
because elements need to be shifted around to make space for insertion or to fill empty
positions after deletion.
 int arr[4][2] = {
 {12, 56},
 {21, 33},
 {34, 80},
 {13, 78}
 };

 int arr[4][2] = {12, 56, 21, 33, 34, 80, 13, 78};
#include<iostream>

using namespace std;

main( )

int arr[4][2] = {

{ 10, 11 },

{ 20, 21 },

{ 30, 31 },

{ 40, 41 }

};

int i,j;

cout<<"Printing a 2D Array:\n";

for(i=0;i<4;i++)

for(j=0;j<2;j++)

cout<<"\t"<<arr[i][j];

cout<<endl;

}
#include<iostream>
using namespace std;
main( )
{
int s[2][2];
int i, j;
cout<<"\n2D Array Input:\n";
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
cin>>s[i][j];

cout<<"\nThe 2-D Array is:\n";


for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cout<<"\t"<<s[i][j];
}
cout<<endl;
}
}
Function
#include <iostream>
using namespace std;

void greet()
{
cout << "Hello there!";
}

int main() {

// calling the function


greet();
s
return 0;
}
Function Parameters
As mentioned above, a function can be declared with parameters (arguments). A
parameter is a value that is passed when declaring a function.

#include <iostream>
using namespace std;
void displayNum(int n1, float n2) {
cout << "The int number is " << n1;
cout << "The double number is " << n2;
}
int main() {
int num1 = 5;
double num2 = 5.5;
// calling the function
displayNum(num1, num2);
return 0;
}
#include <iostream>
using namespace std;
// declaring a function
int add(int a, int b) {
return (a + b);
}

int main() {
int sum;
sum = add(100, 78);
cout << "100 + 78 = " << sum << endl;
return 0;
}

You might also like