Arrays
Arrays
Single-Dimension Arrays
The general form for declaring a single-dimension array is
type var_name[size];
• type declares the base type of the array, which is the type of each element in
the array(ex: int, char, float, double)
• size defines how many elements the array will hold.
char a[7];
Initializing Arrays
Initializing of array is very simple in c programming. Look at the following C code
which demonstrate the declaration and initialization of an array.
int myArray[5] = {1, 2, 3, 4, 5}; //declare and initialize the array in one statement
int studentAge[4];
studentAge[0]=14;
studentAge[1]=13;
studentAge[2]=15;
studentAge[3]=16;
Lec. Sahar Khalid
Example: the program invites the user to enter a series of six values representing sales for
each day of the week (excluding friday), and then calculates the average of these values.
#include <iostream>
using namespace std;
int main()
{
const int SIZE = 6; //size of array
double sales[SIZE]; //array of 6 variables
cout << “Enter sales for 6 days\n”;
for(int j=0; j<SIZE; j++) //put figures in array
cin >> sales[j];
double total = 0;
for(int j=0; j<SIZE; j++) //read figures from array
total += sales[j]; //to find total
double average = total / SIZE; // find average
cout << “Average = “ << average << endl;
return 0;
}
• A new detail in this program is the use of a const variable for the array size and loop limits.
This variable is defined at the start of the listing:
const int SIZE = 6;
makes it easier to change the array size:Lec. Sahar Khalid
The example sets the elements of a 10-element array s to the even integers 2, 4, 6, …, 20
and prints the array in tabular format. These numbers are generated by multiplying each
successive value of the loop counter by 2 and adding 2.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{ const int arraySize = 10; // constant variable can be used to specify array size
int s[ arraySize ]; // array s has 10 elements
for ( int i = 0; i < arraySize; ++i ) // set the values
s[ i ] = 2 + 2 * i;
cout << "Element" << setw( 13 ) << "Value" << endl;
for ( int j = 0; j < arraySize; ++j ) // output contents of array s in tabular format
cout << setw( 7 ) << j << setw( 13 ) << s[ j ] << endl;
}
Here the first subscript stands for the row number and second one for column number.
Alternatively the above definition can be defined and initialized as
int values [3] [4] = { { 1, 2, 3, 4 } { 5, 6, 7, 8 } { 9, 10, 11, 12 } };
Here the values in first pair of braces are initialized to elements of first row, the values of
second pair of inner braces are assigned to second row and so on.
#include <iostream>
using namespace std;
int main(void)
{ int const r=4,c=5;
int i,j, num[r][c];
cout<<"enter 4*5 two dimensional array"<<endl;
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
cin>>num[i][j];
/* now print them out */
for(i=0; i<r; ++i)
{
cout<<endl;
for(j=0; j<c; ++j)
cout<<num[i][j]<<'\t';
}
return 0;
}
Lec. Sahar Khalid
Example:-write a program that reads a two dimensional array 3*4 then find the sum of
each row in the array store the sum in a one dimensional array.
#include <iostream>
using namespace std;
#define r 3
# define c 4
int main(void)
{
int i,j, num[r][c],s[r],sum;
cout<<r<<'*'<<c<<"array";
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
cin>>num[i][j];
for(i=0; i<r; ++i)
{sum=0;
for(j=0; j<c; ++j)
sum+=num[i][j];
s[i]=sum;
}
for(i=0;i<r;i++)
cout<<"sum row "<<i<<'='<<s[i];
return 0; Lec. Sahar Khalid
}
Write a C program that reads a two dimensional float array 3*4. compute the average
value of the array elements. Count how many elements are greater than the average.
#include <iostream>
using namespace std;
int main()
{ const int row=3, col=4;
float arr[row][col],cont=0,i,j;
float sum=0,avr;
cout<<"enter"<<row<<'*'<<col<<"array";
for(i=0;i<row;i++)
for(j=0;j<col;j++)
cin>>arr[i][j];
for(i=0;i<row;i++)
for(j=0;j<col;j++)
sum+=arr[i][j];
avr=sum/(row*col);
cout << "average=" << avr<<endl;
for(i=0;i<row;i++)
for(j=0;j<col;j++)
if(arr[i][j]>avr)
cont++;
cout<<"no. of elements greater than the average are"<<cont;
return 0;
} Lec. Sahar Khalid