Professional Documents
Culture Documents
(CS-102)
Madiha Kiran
Arrays
marks 80 75 65 85 90 78
grade a b c d f
Uses of Arrays
One-Dimensional Array
List of related values with the same data type that’s stored with a single group name.
It is also called single dimensional array.
Declaring One-Dimensional Array
The process of specifying array name, length and data type is called array declaration.
Syntax
dataType arrayName[numberOfItems];
Example
int marks[6];
0 1 2 3 4 5
marks
Example
double amount[10];
char grade[5];
float values[20];
Accessing Array Elements
Syntax
arrayName[index];
Example
marks[0] = 80;
Example
marks[0] marks[1] marks[2] marks[3] marks[4] marks[5]
marks
marks[0] = 80;
marks[1] = 90;
marks[2] = 78;
marks[3] = 77;
marks[4] = 87;
marks[5] = 88;
marks 80 90 78 77 87 88
Accessing Array Elements using loop
int arr[6], i;
for (i = 0; i <=5; i++)
arr[i] = i;
arr 0 1 2 3 4 5
Input & Output of Array Values
cout<<“Enter Marks = ”;
cin>>marks[0]>>marks[1]>>marks[2]>>marks[3]>>marks[4]>>marks[5];
cout<<“Marks = ”<<endl;
cout<<marks[0]<<endl;
cout<<marks[1]<<endl;
cout<<marks[2]<<endl;
cout<<marks[3]<<endl;
cout<<marks[4]<<endl;
cout<<marks[5];
Input & Output of Array Values using loop
Write a program to input five numbers into an array named marks. After all numbers are input, display the
numbers and their total.
#include <iostream>
using namespace std;
int main() {
i marks[i] sum
int marks[5], sum=0;
for(int i = 0; i<5; i++) 0 80 80
{ 1 87 167
cout<<"Enter marks = ";
2 95 262
cin>>marks[i];
}
3 77 339
cout<<"The total of marks"; 4 78 417
for(int i = 0; i<5; i++) 5 --- ---
{
cout<<" "<<marks[i];
sum = sum + marks[i];
}
cout<<" is "<<sum;
return 0; }
Output
Array Initialization
The process of assigning values to array elements at the time of array deceleration.
Syntax:
dataType arrayName[numberOfItems] = { List of values };
Example:
double width[7] ={10.96, 6.43, 2.58, 0.86, 5.89, 7.56, 8.22};
double width[7] ={10.96, 6.43, 2.58, 0.86};
Example
Write a program that inputs 10 numbers/ values from the user, store them in an array and
display these numbers , sum and average of these numbers.
Write a program in which you can declare an array for alphabets and stores first seven
alphabets in it and display these alphabets.
Write a program that input only grades of your five subjects (ask user for input his/her
grade) and display all the grades.
Example
Syntax
arrayName[row][col];
Example
val[1][3] = 6;
Example
cout<<“Enter Values = ”;
cin>>val[0][0]>>val[0][1]>>val[0][2]>>val[0][3];
cout<<“Values = ”<<endl;
cout<<val[0][0]<<endl;
cout<<val[0][1]<<endl;
cout<<val[0][2]<<endl;
cout<<val[0][3]<<endl;
Input & Output of 2-D Array Values using loop
Write a program to input and display elements of two dimensional array.
#include <iostream>
using namespace std; i j val[i][j]
int main() {
0 0 90
int val[2][3];
for(int i = 0; i<2; i++)
0 1 77
for(int j = 0; j<3; j++) 0 2 95
{ 1 0 87
cout<<"Enter values = ";
1 1 78
cin>>val[i][j];
}
1 2 65
for(int i = 0; i<2; i++) 2 --- ---
{
cout<<endl;
for(int j = 0; j<3; j++) 90 77 95
cout<<" "<<val[i][j]; 87 78 65
}
return 0; }
Output
2-D Array Initialization
The process of assigning values to two dimensional array elements at the time of array
deceleration.
Example:
int val[3][4] = { {8,16,9,52},
{3,15,27,6},
{14,25,2,10} };
Example
Write a program to calculate total of each row.
#include <iostream>
row col val[row][col] total
using namespace std;
int main() { 0 0 2 2
int tot_rows = 2; 0 1 3 5
int tot_cols = 3; 0 2 7 12
int val[tot_rows][tot_cols] = {{2,3,7},{6,12,4}};
1 0 6 6
int total = 0;
1 1 12 18
for(int row = 0; row < tot_rows; row++)
{
1 2 4 22
total = 0; 2 --- --- ---
for(int col = 0; col < tot_cols; col++)
total = total + val[row][col];
cout<<"Total of row " <<row<<" = "<<total<<endl;
}
return 0; }
Output
Example
Write a program to calculate average of all numbers stored in 2-D array.
#include <iostream>
using namespace std;
int main() {
float val[2][3] = {{2.2,3,7},{6.2,12,4}}; row col val[row][col] total
float total = 0; 0 0 2.2 2.2
float avg; 0 1 3 5.2
for(int row = 0; row < 2; row++) 0 2 7 12.2
for(int col = 0; col < 3; col++) 1 0 6.2 18.4
total = total + val[row][col]; 1 1 12 30.4
avg = total/6;
1 2 4 34.4
cout<<"Average = "<<avg;
2 --- --- ---
return 0;
}
Output
Practice Problems
Gary J. Bronson , Program Development and Design using C++, 2nd Edition. (Chapter 7)