

















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
What are arrays and how to use it in Cprogramming.
Typology: Slides
1 / 25
This page cannot be seen from the preview
Don't miss anything!


















Array : is a collection of fixed number of elements, wherein all of elements
have same data type.
Array Basics:
◦ Consecutive group of memory locations that all have the same type.
◦ The collection of data is indexed, or numbered, and at starts at 0 and The
highest element index is one less than the total number of elements in
the array.
One-dimensional array :
◦ elements are arranged in list form.
Multi-dimensional array:
Arrayname [element index].
Example: int list [10];
list [5] = 34;
Cout << list [5];
Consider the declaration
int list[10]; //array of size 10
After declaring the array you can use the For Loop to initialize it
with values submitted by the user.
Using for loops to access array elements:
for (int i = 0; i < 10; i++)
//process list[i]
Example:
for (int i = 0; i < 10; i++)
cin >> list[i]; 5
Example : Write a program that ask the user to enter 10
Employee salaries and store them , then add a bonus of 10 % for
each employee and print out the average salary value.
7
int main()
{
float sum = 0.0;
float salaries [10];
for(int i = 0; i < 10; i++)
{
cout<< "Enter Salary for employee no " << i+1<<"\n";
cin >> salaries [i];
}
for (int i = 0; i < 10 ; i++)
sum +=salaries[i];
cout << " The Average salary is " << sum/10 <<"\n";
}
for (int indx =0 ; indx <10; indx++)
{
salaries[indx]+= salaries[indx] * 0.10;
cout << " The salary for employee no " <
int Arr1[5];
int Arr2 [ 5 ] ;
Arr1 = Arr2 ;
10
using namespace std;
int main()
{
int list1[5]={2,4,6,8,10};
int list2[5];
for ( int i =0 ; i < 5 ; i++ )
list2[i]= list1[i];
cout << list2[3] <<"\n";
}
array as
a formal parameter
parameter
to the function.
11
float calc_Average ( float marks [ ] , int size
);
int main()
{
float marks [5] = {10,20,30,40,50};
cout << calc_Average ( marks, 5)<<"\n";
}
float calc_Average ( float marks [ ] , int size
)
{
float sum =0 ;
for (int i =0 ; i
Two dimensional Array declaration
Datatype ArrayName [ Rows] [Columns] ;
Example :
Float marks [6] [4] ;
13
1
2
3
4
5
0 1 2 3
20 marks [4][2]= 20 ;
0
Consider the declaration
float marks[6][4];
After declaring the array you can use the For Loop to
initialize it with values submitted by the user.
Using 2 nested for loops to access array elements:
for (int row = 0; row < 6; row++)
for (int col = 0; col < 4; col++)
cin >> marks[ row ][col];
14
Write a program that build a matrix of 5 rows and 3 columns. Ask
the user to enter the values for all the matrix items and print out
the all matrix items.
int main()
{
int matrix[5][3];
for (int row = 0; row < 5; row++)
for (int col = 0; col < 3; col++)
{
cout << "Enter the value for item " << row << "," << col
<<"\n";
cin >> matrix [ row ][col];
}
for (int row = 0; row < 5; row++)
{
for (int col = 0; col < 3; col++)
{
cout < 17
Example : Write a program that build a matrix of 5 rows and 3
columns. Ask the user to enter the values for all the matrix items ,
print out the sum of all matrix items and print out the sum of the
diagonal items.
known as members, can have different types
◦ For Example : to store a student record which includes different data items like
(student_no , Fname, Lname , Total_Marks , GPA , …….).
◦ Syntax for Defining Struct is :
19
Struct struct_name
{
Datatype identifier
1;
Datatype identifier
2;
.
.
.
Datatype identifier
n;
For Example : To store an Employee data like ( emp_no ,
fname , lname , salary , bonus , net_salary );
20
Struct Employee
int emp_no ;
string fname ;
string lname;
float salary;
float bonus ;
float net_salary;