30
ARRAY: An array is a “contiguous memory location *Contiguous memory location means “ memory blocks having consecutive addresses”. An array is a group of consecutive memory location with same name and type. Simple variable is a single memory location with unique name and a type. The memory locations in the array are known as elements of array. The total number of elements in the array is called its length. Each element in the array is accessed with reference to its position of the location in the array. This position is called index. Each element in the array has a unique index. The index of first element is 0 and the index of last element is length. The value of the index is written in brackets along with the name of the array. An array is a collective name given to a group of similar quantities. These similar quantities could be percentage marks of 100 students, number of chairs in home, or salaries of 300 employees or ages of 25 students. Thus an array is a collection of similar elements. These similar elements could be all integers or all floats or all characters etc. 1

Array assignment

Embed Size (px)

DESCRIPTION

Assignment

Citation preview

Page 1: Array assignment

ARRAY:

An array is a “contiguous memory location*”

Contiguous memory location means “ memory blocks having consecutive addresses”.

An array is a group of consecutive memory location with same name and type. Simple variable is a single memory location with unique name and a type.

The memory locations in the array are known as elements of array. The total number of elements in the array is called its length. Each element in the array is accessed with reference to its position of the location in the array. This position is called index. Each element in the array has a unique index. The index of first element is 0 and the index of last element is length. The value of the index is written in brackets along with the name of the array.

An array is a collective name given to a group of similar quantities. These similar quantities could be percentage marks of 100 students, number of chairs in home, or salaries of 300 employees or ages of 25 students. Thus an array is a collection of similar elements. These similar elements could be all integers or all floats or all characters etc.

ARRAY DECLARATION:

The process of specifying array name, length, and data type is called array declaration.

Syntax = Data type _name of array[length];

1

Page 2: Array assignment

ARRAY INITIALIZATION:

The process of assigning values to array elements at the time of array declaration is called array initialization. The initialization process provides a list of initial values for array elements. The values are separated with commas and enclosed within braces.

Syntax = Data type _name of array[length] = {list of values};

ACCESSING ARRAY ELEMENTS USING LOOPS:

An easier and faster way of accessing array elements in using loops. The following examples shows how array elements can be accessed using for loop.

int marks[5];

for(inti=0; i<5; i++)

marks[i]=i;

The above example uses for loop to store different values in the array .It uses the counter variable ’i’ as an index. In each iteration, the value of ’i’ is changed. The statement marks[i] refers to different array element in each iteration.

INPUT AND OUTPUT VALUES OF AN ARRAY:

The process of input and output with arrays is similar to the Input and output with simple variables. The cin object is used to input values in the arrays. The cout object is used to display values of arrays.

USES OF ARRAYS:

Some uses of arrays are as follows:

Arrays can store a large number of values with single name. Arrays are used to process many values easily and quickly. The values stored in arrays can be sorted easily. A search process can be applied on arrays easily. Array can perform matrix operations

ARRAY DATA TYPES:

Data type indicates the value to be stored in the array.

Int It stores integer data and its capacity is 4 bytes (32 bits).

Float It stores integeras well as fractional data its capcity is 4 bytes (32 bits).

Char It stores characters (alphabets) its capacity is 1 byte (8 bits).

Double It also stores fractional data like float its capacity is 4 bytes.

2

Page 3: Array assignment

TYPES OF ARRAY:

Arrays can of following types:

I. Single-Dimensional Array. (1D array )II. Multi-Dimensional Array. (2D array )

SINGLE-DIMENSIONAL ARRAY:

A type of array in which all elements are arranged in the form of a list is known as one dimensional array. It is also called one-dimensional array. It consists of one column or one row. The elements are stored in consecutive memory locations. E.g. A [1], A [2]... A [N].

SYNTAX:

The syntax of declaring one-dimensional array is as follows:

Data type _identifier[length];

Data _Type: It indicates the data types of the values to be stored in the array.

Identifier: It indicates the name of the array.

Length: It indicates total number of elements in the array. It must be a literal

constant or symbolic constant.

EXAMPLE:

int x[7];

It allocates seven consecutive locations in memory. The index of first element is 0 and index of last element is 6.

MULTI-DIMENSIONAL ARRAY:

In multi-dimensional array there is two-dimensional array which can be considered as a table that consists of rows and column. Each element in 2-D array is referred with the help of two indexes. One index is used to indicate the row and the second index indicates the column of the element.. It is also called matrix array because in it the elements form a matrix. E.g. A [2] [3] has 2 rows and 3 columns and 2*3 = 6 elements.

3

Page 4: Array assignment

SYNTAX:

The syntax of declaring two-dimensional array is as follows:

Data type _identifier[Rows][column];

Data _Type: It indicates the data types of the values to be stored in the array.

Identifier: It indicates the name of the array.

Rows: It indicates number of rows in the array. It must be a literal constant or

symbolic constant.

column: It indicates number of column in the array. It must be a literal constant.

EXAMPLE:

intArr[3][4];

The statement declares a two-dimensional array. The first index indicates array contain three rows. The index of first row is 0 and the index of last row is 2. The second index indicates that each row in the array contains three columns. The index of first column is 0 and the index of last column is 3. The total number of elements can be determined by multiplying rows and columns. It means that array contains twelve element

4

Page 5: Array assignment

PROGRAMS OF ARRAY

1. Write a program to find the highest and second highest number? Using array.

DESCRIPTION:

In this program we find highest and second highest number. In the program we will the input from user and use FOR loop statement to completing the program.

INPUT:

Enter 0 Number

Enter 1 Number

Enter 2 Number

Enter 3 Number

Enter 4 Number

METHOD:

This program will take an integer array as input and by using FOR loop it will give highest and second highest as output

PROGRAM: #include<iostream.h>

#include<conio.h>

void main()

{

clrscr();

int a[5],highest=0,secondhighest=0;

for (int i=0; i<5; i++)

{

cout<<" Enter "<<i<<" Number =";

cin>>a[i];

}

for (int j=0; j<5; j++)

{

highest = a[j];

}

}

cout<<"Highest Number is ="<<highest<<"\n";

for (int k=0; k<5; k++)

{

if (a[k] > secondhighest && a[k] < highest)

{

secondhighest = a[k];

}

}

cout<<"Second Highest Number is ="

5

Page 6: Array assignment

if (a[j] > highest)

{

cout<<secondhighest<<"\n";

getch();

}

OUTPUT:

2. Write a program to find highest and lowest number? Using array.

DESCRIPTION: In this program we will find the highest and lowest numbers. In that program the input from user and use FOR loop statement to completing the program.

INPUT:

Enter 0 Number, Enter 1 Number, Enter 2 Number, Enter 3 Number, Enter 4 Number

PROGRAM: #include<iostream.h>

#include<conio.h>

void main()

{

int a[5];

for (int i=0; i<5; i++)

{

cout<<" Enter "<<i<<" Number = ";

cin>>a[i];

cout<<"\n";

}

int highest=a[0],lowest=a[0];

highest = a[j];

}

}

cout<<"Highest Number is =";

cout<<highest<<”\n\n”;

for (int k=0; k<5; k++)

{

if (a[k] < lowest)

{

lowest = a[k];

}

}

6

Enter 0 Number =90

Enter 1 Number =7

Enter 2 Number =60

Enter 3 Number =55

Enter 4 Number =40

Highest Number is =90

Second Highest Number is =70

Page 7: Array assignment

for (int j=0; j<5; j++)

{

if (a[j] > highest)

{

cout<<"Lowest Number is ="<<lowest<<”\n\n”;

getch();

}

OUTPUT:

Enter 0 Number =1000

Enter 1 Number =899

Enter 2 Number =678

Enter 3 Number =45

Enter 4 Number =55

Highest Number is =1000

Lowest Number is =45

3. Write a program to find the average? Using array

DESCRIPTION:

In this program we will find average (means that sum of total numbers divided by total numbers) of the number with the help of FOR loop statement.

INPUT:

Enter Number (This message will be displayed 5 times)

METHOD:

This program will take integer array i-e.; m [5] as input and by using FOR loop it will give average by finding first the sum of the numbers given by user and then dividing by total number of values as output.

PROGRAM: #include<iostream.h>

#include<conio.h>

void main ()

{

clrscr();

int m[5];

cout<<"\n";

}

float z=0;

for (int i=0; i<5; i++)

{

z=z+m[i];

7

Page 8: Array assignment

float average;

for (int s=0; s<5; s++)

{

cout<<"Enter Number "<<s<<" = ";

cin>>m[s];

}

average=z/5;

cout<<"Average is ="<<average<<"\n";

getch();

}

OUTPUT:

Enter Number = 50

Enter Number= 50

Enter Number= 50

Enter Number=50

Enter Number=50

Average is = 50

4. Write a program to find the marks of math and declare that how many students pass and fail in math subject? Using array.

DESCRIPTION:

In this question we type the program in c++ for the marks of math student and show the result that how many student are passed in math and how many student are failed in math with the help of FOR loop statement.

INPUT:

Enter Marks of Student No 0 of MathEnter Marks of Student No 1 of MathEnter Marks of Student No 2 of Math Enter Marks of Student No 3 of Math Enter Marks of Student No 4 of Math

METHOD:

This program will take integer array as input. user enter the numbers of mathematics of five students in that array then by using FOR loop and IF ELSE statement it will give output of total number of PASSED and FAILED students.

8

Page 9: Array assignment

PROGRAM: #include<iostream.h>

#include<conio.h>

void main()

{

clrscr();

int s[5];

for (int i=0; i<5; i++)

{

cout<<"Enter Marks of Student No "

cout<<i<<" of math"<<" = ";

cin>>s[i];

cout<<"\n";

}

int pass=0,fail=0;

for (int j=0; j<5; j++)

{

if (s[j] >=50)

{

pass=pass+1;

}

else

{

fail=fail+1;

}

}

cout<<"passed student are ="<<pass<<”\n\n”;

cout<<"failed student are ="<<fail<<”\n\n”;

getch();

}

OUTPUT:

Enter Marks of Student No 0 of Math = 78

Enter Marks of Student No 1 of Math = 35

Enter Marks of Student No 2 of Math = 88

Enter Marks of Student No 3 of Math = 67

Enter Marks of Student No 4 of Math = 23

passed student are = 3

failed student are = 2

5. Write a program to find the multiple of matrix? Using Array.

DESCRIPTION:

In this program we multiply the matrix (2*2) in the array by using FOR loop statement.

INPUT:

9

Page 10: Array assignment

Multiplication of Matrix is (This message will be displayed only one time)

PROGRAM: #include<iostream.h>

#include<conio.h>

void main()

{

clrscr();

int a[3][3],b[3][3],i,j,k,sum;

cout<<"First Matrix"<<“\n”;

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

{

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

{

cout<<"Enter number :";

cin>>a[i][j];

}

}

cout<<"Second Matrix"<<“\n”;

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

{

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

{

cout<<"Enter number :";

cin>>b[i][j];

}

}

cout<<"Multiplication is"<<“\n”;

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

{

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

{

sum=0;

for(k=0;k<3;k++)

{

sum=sum,+a[i][k]*b[k][j];

}

cout<<sum<<"\t";

}

cout<<“\n”;

}

getch();

}

METHOD:

10

Page 11: Array assignment

In this program we first enter the elements 9 times for each matrix. There variable “i” is

used for the rows and variable “j” is used for the columns and the sum variable is used to

find the individual product of each element as the multiplication formula is “ 1st rows

elements of 1st matrix * 1st column elements of 2nd matric and add them together. After

calculation product is displayed at the end ./t is used to give 8 spaces.

OUTPUT:

11

First Matrix Enter the number1 Enter the number1 Enter the number1 Enter the number1 Enter the number1 Enter the number1 Enter the number1 Enter the number1 Enter the number1

Second Matrix

Enter the number1 Enter the number1 Enter the number1 Enter the number1 Enter the number1 Enter the number1 Enter the number1 Enter the number1 Enter the number1

Multiplication is3 3 33 3 33 3 3

Page 12: Array assignment

6.Write a program for the addition of matrix? Using Array.

DESCRIPTION: In this program we will add the matric (2x2) in the array by using FOR loop statement.

INPUT: First Matrix Enter Element (This message will be displayed 4 times)Second MatrixEnter Element (This message will also be displayed 4 times)

PROGRAM: #include<iostream.h>

#include<conio.h>

int main()

{

clrscr();

int a[2][2],b[2][2],c[2][2], i, j;

cout<<" First Matrix "<<"\n";

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

{

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

{

cout<<"Enter Element = ";

cin>>a[i][j];

}

}

cout<<" Second Matrix "<<"\n";

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

{

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

{

cout<<"Enter Element = ";

cin>>b[i][j];

}

}

cout<<" Addition of Matrix "<<"\n";

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

{

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

{

c[i][j] = a[i][j] + b[i][j];

cout<<c[i][j]<<" ";

}

cout<<"\n\n";

}

getch();

}

METHOD:

12

Page 13: Array assignment

In this program we first enter the elements 4 times for each matrix in two arrays i-e.; a[2]

[2] and b[2][2].By using Nested FOR loop 1st element of 1st matrix is added with 1st

element of 2nd matrix, 2nd of 1st matrix with 2nd of 2nd matrix and so on . Program will

give one matrix after addition of two as output. OUTPUT:

First MatrixEnter Element = 1Enter Element = 2Enter Element = 1Enter Element = 1 Second MatrixEnter Element = 1Enter Element = 1Enter Element = 1Enter Element = 1Addition of matrix2 3

2 2

7. Write a program for the subtraction of matrix? Using Array.

DESCRIPTION:

In this program we subtract the matrix in the array by using FOR loop statement.

INPUT:

First Matrix Enter Element (This message will be displayed 4 times)Second MatrixEnter Element (This message will also be displayed 4 times)

METHOD:

In this program we first enter the elements 4 times for each matrix in two arrays i-e.; a[2]

[2] and b[2][2].By using Nested FOR loop 1st element of 1st matrix is subtracted with

1st element of 2nd matrix, 2nd of 1st matrix with 2nd of 2nd matrix and so on . Program will

give one matrix c[2][2] after subtraction of two as output.

PROGRAM:

#include<iostream.h>

#include<conio.h>

cout<<"Enter Element = ";

cin>>b[i][j];13

Page 14: Array assignment

int main()

{

clrscr();

int a[2][2],b[2][2],c[2][2], i, j;

cout<<" First Matrix "<<"\n";

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

{

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

{

cout<<"Enter Element = ";

cin>>a[i][j];

}

}

cout<<" Second Matrix "<<"\n";

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

{

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

{

}

}

cout<<" Subtraction of Matrix "<<"\n";

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

{

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

{

c[i][j] = a[i][j] + b[i][j];

cout<<c[i][j]<<" ";

}

cout<<"\n\n";

}

getch();

}

OUTPUT:

First MatrixEnter Element = 1Enter Element = 2Enter Element = 1Enter Element = 1Second MatrixEnter Element = 1Enter Element = 1Enter Element = 1Enter Element = 1Subtraction of matrix0 1

0 0

8. Write a program that inputs values from the user, stores them in an array and displays the sum.

14

Page 15: Array assignment

DESCRIPTION:

This program will take the input of 5 numbers from the user and by using for loop in array it will calculate the sum of these numbers and then find the average by dividing the sum of values by the number of values.

INPUT :

Enter value: (This message will be displayed 5 times)

METHOD:

The above example initializes the variable sum to 0. The first loop gets five inputs. The second loop again visits all elements of the array. In first iteration, the value of counter variable is 0. So it adds the value of first elements and the value of Sum and stores the result in sum. In second iteration, it adds the value of second element and sum and then stores the result back to sum and so on.

PROGRAM:

#include<iostream.h>

#include<conio.h>

void main()

{

int arr[5], i, sum=0;

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

{

cout<<”Enter value=”;

cin>>arr[i];

}

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

{

sum=sum+arr[i];

}

cout<<”sum is=”<<sum<<“\n”;

getch();

}

OUTPUT:

15

Enter value=3

Enter value=7

Enter value=5

Enter value=9

Enter value=6

Sum is=30

Page 16: Array assignment

9. Write a program that inputs the age of different users and counts the number of persons in the age group of 50 to 60.

DESCRIPTION:

This program will take the input from the user and the number of users will also be taken at run time. After this it will asks for the ages of people. By using for loop and IF Statement in array it will display the number of persons in the age group 50 and 60 if the condition satisfies.

INPUT :

Enter the number of person required= (This message will be displayed as much time as user required)

Enter ages of persons = (This will take the ages of persons)

METHOD:

In this program the user first enters the number of person and their ages with the help of one-dimensional array in the loop and by applying if condition user easily determine the ages in between 50 and 60. Taking “sum” as a variable equals to zero as condition satisfied in the if statement the value of sum increment and at the end it stores a value which shows the ages of person which is in between 50 and 60.

PROGRAM:

#include<iostream.h>

#include<conio.h>

void main()

{

int age[10], i, n, sum=0;

cout<<”Enter the number of person required=”;

cin>>n;

cout<<”Enter ages of”<<n<<”persons=”<<“\n”;

cin>>age[i];

}

for(int z=0; z<n; z++)

{

if(age[z]>=50 && age[z]<=60)

{

Sum=sum+1;

}

cout<<sum<<”persons are between 50 and 60.”;

16

Page 17: Array assignment

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

{

getch();

}

OUTPUT:

10. Write a program that input values from the user and stores them in an array. It displays the location of those values.

DESCRIPTION:

This program will take the input from the user and it will displays the location of that element stored in that array by using For Loop.

INPUT:

Enter values to find their location= (This message will take the input from user 5 times)

METHOD:

In this program the user enters number in the array and it stores in array with their specific locations. The loop searches the array stores the value of their index. The program checks the value that is stored over it and by taking a new variable which is equal to the loop in which the value is stored we can easily determine their locations.

PROGRAM: 17

Enter the number of person required=3

Enter ages of 3 persons=

51

55

34

2 persons are between 50 and 60.

Page 18: Array assignment

#include<iostream.h>

#include<conio.h>

void main()

{

Int a[5], i, locat, h;

Cout<<”Enter values to find their location=”;

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

{

cin>>a[i];

}

for(int d=0; d<5; d++)

{

h=a[d];

locat=d;

cout<<”The value “<<h<<” is in“<<locat

cout<<” location”;

}

getch();

}

OUTPUT:

11. Write a program that inputs the marks of 3 students in three different subjects .If student is passed in two subjects then he will be considered pass and

18

Enter values to find their location=

23

45

12

56

78

The value 23 is in 0 location.

The value 45 is in 1 location.

The value 12 is in 2 location.

The value 56 is in 3 location.

The value 78 is in 4 location.

Page 19: Array assignment

displays the number of pass students and also displays the number of students who fail.

DESCRIPTION:

This program will take the input the marks of 3 students of their any 3 subjects .Then it will check that the student is passed or not by using IF Condition .If student is passed in two subjects then he will be considered pass .It will also use a counter to calculate the number of students who are passed or fail.by using Nested For Loop and IF Condition.

INPUT:

Enter 1 student marks in 1 subject =

Enter 1 student marks in 2 subject =

Enter 1 student marks in 3 subject =

(This message will displayed 3 times)

METHOD:

In the above program user inputs the marks of students in three subjects with the help of 2-D array and by using the loops. At start we first apply if condition in the subject loop that if marks of student is equal to 50 or greater than 50 then a variable “s” is increment similarly if less than 50 or equal to 50 than the variable ”f”is decrement same procedure is apply in the student loop that if the variable ”s” is greater than 2 or equals to 2 then a new variable “u” is increment which shows the number of pass students and remaining goes to the else condition in which the variable “f” is increment which shows the number of fail students.

19

Page 20: Array assignment

PROGRAM:

#include<iostream.h>

#include<conio.h>

void main()

{

int a[15][3];

for(int i=1; i<=3; i++)

{

for (int z=1; z<=3; z++)

{

cout<<”Enter”<<i<<” student marks in “<<z<<” subject =”;

cin>>a[i][z];

}

cout<<”\n”;

}

int u=0,h=0;

for(int y=1; y<=3; y++)

{

int s=0,f=0;

for(int r=1; r<=3; r++)

{

If( a[y][r]>=50)

{

s=s+1;

}

else

{

f=f+1;

}

}

if(s>=2)

{

u=u+1;

}

else

{

h=h+1;

}

}

cout<<”pass student =”<<u;

cout<<“\n”;

cout<<”fail student =”<<h;

getch();

}

20

Page 21: Array assignment

OUTPUT:

12. Write a program for enter the marks of math, physics, chemistry and show the result that how many student are passed and failed in these subject? Using Array.

DESCRIPTION:

In the program we use FOR loop and IF Else statement to enter the marks of student of these subject and declare that these student are passed and these student are failed.

INPUT:

Enter Math Numbers (This message will displayed 5 times)

Enter Physics Numbers (This message will also displayed 5 times)

Enter Chemistry Numbers (This message will also displayed 5 times)

21

Enter 1 student marks in 1 subject =98

Enter 1 student marks in 2 subject =56

Enter 1 student marks in 3 subject =23

Enter 2student marks in 1 subject =23

Enter 2 student marks in 2 subject =34

Enter 2 student marks in 3 subject =67

Enter 3 student marks in 1 subject =85

Enter 3 student marks in 2 subject =56

Enter 3 student marks in 3 subject =67

pass student =2

fail student =1

Page 22: Array assignment

METHOD: In this program user will enter the marks of three subjects of 4 students. By used FOR loop and IF ELSE statement program will give number of failed and passed student of each subject as output.

PROGRAM:

#include<iostream.h>

#include<conio.h>

void main()

{

clrscr();int math[4],physics[4],chemistry[4];for (int i=0; i<4; i++){ cout<<"Enter Math Numbers ="; cin>>math[i]; cout<<"Enter Physics Numbers ="; cin>>physics[i]; cout<<"Enter Chemistry Numers ="; cin>>chemistry[i];} cout<<"\n";int mp=0,mf=0,pp=0,pf=0,cp=0,cf=0;for (int j=0; j<4; j++){ if (math[j] >=50) { mp=mp+1; } else { mf=mf+1; } if (physics[j] >=50) {

else { pf=pf+1; }

if (chemistry[j] >=50){ cp=cp+1;}else{ cf=cf+1;}}

cout<<"passed student for math ="<<mp<<"\n";

cout<<"failed student for math="

cout <<mf<<"\n";cout<<"passed student for physics ="

cout<<pp<<"\n";cout<<"failed student for physics ="

cout<<pf<<"\n";cout<<"passed student for chemistry ="

cout<<cp<<"\n";cout<<"failed student for chemistry ="

cout<<cf<<"\n";getch();}

22

Page 23: Array assignment

pp=pp+1; }

OUTPUT:

Enter Math Numbers =78

Enter Physics Numbers =12

Enter Chemistry Numbers =34

Enter Math Numbers =90

Enter Physics Numbers =33

Enter Chemistry Numbers =23

Enter Math Numbers =99

Enter Physics Numbers =78

Enter Chemistry Numbers =77

Enter Math Numbers =45

Enter Physics Numbers =66

Enter Chemistry Numbers =20

Passed student for math =3

failed student for math =1

Passed student for physics =2

failed student for physics =2

Passed student for chemistry =1

failed student for chemistry =3

13. Write a program to swap the numbers? Using Array.

23

Page 24: Array assignment

DESCRIPTION:

In this program we will change the location of number with the help of FOR and IF Else statement.

INPUT:

Enter the Number A

Enter the Number B

METHOD:

This program will get an integer array as input of two values and BY using FOR loop and IF ELSE statement it will swap the numbers with each other and give output as shown below.

PROGRAM:

#include<iostream.h>

#include<conio.h>

void main()

{

clrscr();

int a[2],c;

for (int i=0; i<2; i++)

{

if (i==0)

{

cout<<"Enter the Number A = ";

}

else

{

cout<<"Enter the Number B =";

cin>>a[i];

}

c=a[0];

a[0]=a[1];

a[1]=c;

cout<<"\n"<<"After Swapping"<<"\n";

cout<<"A is = "<<a[0]<<"\n";

cout<<"B is = "<<a[1]<<"\n";

getch();

}

Output:

24

Enter the Number A = 20

Enter the Number B = 10

After the swapping

A is = 10