Using arrays in C Programming?, Slides of Programming for Engineers

What are arrays and how to use it in Cprogramming.

Typology: Slides

2018/2019

Uploaded on 04/16/2019

diwas-rathod
diwas-rathod 🇳🇵

3 documents

1 / 25

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Arrays
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19

Partial preview of the text

Download Using arrays in C Programming? and more Slides Programming for Engineers in PDF only on Docsity!

Arrays

Arrays

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:

Arrays

Accessing array Elements

Arrayname [element index].

Example: int list [10];

list [5] = 34;

Cout << list [5];

Array Initialization

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 " <

Array Initialization

int Arr1[5];

int Arr2 [ 5 ] ;

Arr1 = Arr2 ;

10

include

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 Parameter

to Function

  • Arrays are passed by reference only
  • The symbol & is not used when declaring an

array as

a formal parameter

  • The size of the array is usually passed as a

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

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

Two dimensional Array

Initialization

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.

Struct

  • Struct : collection of a fixed number of components (members), accessed by name.
  • is a group of data elements grouped together under one name. These data elements,

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;

Struct

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;