Articles by "Arrays"
Showing posts with label Arrays. Show all posts
no image
Write an algorithm for array implementation of List ADT.

A linked list is a sequence of data structures, which are connected together via links. Linked List is a sequence of links which contains items. Each link contains a connection to another link. The linked list is the second most-used data structure after the array.
A linked list is a sequence of data structures, which are connected together via links.
Linked List is a sequence of links which contains items. Each link contains a connection to another link. The linked list is the second most-used data structure after the array.




Following are the important terms to understand the concept of Linked List.
• Link − each link of a linked list can store a data called an element.
• Next − each link of a linked list contains a link to the next link called Next.
• Linked List − A Linked List contains the connection link to the first link called First.

Algorithm Steps:

Step1: Create nodes first, last; next, prev and cur then set the value as NULL.

Step 2: Read the list operation type.

Step 3: If the operation type is created then process the following steps.
1. Allocate memory for node cur.
2. Read data in cur's data area.
3. Assign cur node as NULL.
4. Assign first=last=cur.

Step 4: If the operation type is Insert then process the following steps.
1. Allocate memory for node cur.
2. Read data in cur's data area.
3. Read the position the Data to be inserted.
4. Availability of the position is true then assign cur's node as first and first=cur.
5. If availability of position is false then do following steps.
 1. Assign next as cur and count as zero.
 2. Repeat the following steps until count less than position.
   1.Assign prev as next
   2. Next as prev of the node.
   3. Add count by one.
   4. If prev as NULL then display the message INVALID POSITION.
   5. If prev not equal to NULL then do the following steps.
      1. Assign cur's node as prev's node.
      2. Assign prev's node as cur.

Step5: If the operation type is deleted then do the following steps.
  1. Read the position.
  2. Check list is Empty.If it is true display the message List empty.
  3. If the position is first.
     1. Assign cur as first.
     2. Assign First as first of node.
     3. Reallocate the cur from memory.
 4. If the position is last.
    1. Move the current node to prev.
    2. cur's node as Null.
    3. Reallocate the Last from memory.
    4. Assign last as cur.
    5. If the position is to enter Mediate.
        1. Move the cur to the required position.
        2. Move the Previous to cur's previous position
        3. Move the Next to cur's Next position.
        4. Now Assign previous of node as next.
5. Reallocate the cur from memory.

Step 6: If the operation is traverse.
1. Assign current as first.
2. Repeat the following steps until cur becomes NULL.
no image
Define a function template for finding the minimum value contained in an array. Write main( ) function to find the minimum value of integer array and the minimum value of floating point numbers in an array.




#include<iostream.h>
#include<conio.h>
template<class T>
T minimum(T a[],int size)
{
T min=a[0];
for(int i=0;i<size;i++)
{
if(a[i]<min)
min=a[i];
}
return min;
}
int main()
{
int a[10],size,i,min1;
float b[10],min2;
clrscr();
cout<<"Enter the size value:\n";
cin>>size;
cout<<"Enter the integer array elements\n";
for(i=0;i<size;i++)
cin>>a[i];
cout<<"Enter the floating array elements\n";
for(i=0;i<size;i++)
cin>>b[i];
min1=minimum(a,size);
min2=minimum(b,size);
cout<<"The minimum integer element is:\n";
cout<<min1;
cout<<"\nThe minimum floating element is :\n";
cout<<min2;
getch();
return 0;
}

The output of the above program is:
Enter the size value:
5
Enter the integer array elements
20
30
10
38
28
Enter the floating array elements
20.4
19.7
14.8
1.7
2.6

The minimum integer element is:
10
The minimum floating element is :
1.7





Thanks
Mukesh Rajput
no image
Create a 'MATRIX' class of size m X n. Overload the ‘+’ operator to add two MATRIX objects. Write a main function to implement it.

#include<iostream.h>
#include<conio.h>
class mat
{
int m,n,a[20][20];
public:
mat(int x,int y);
void readmat();
mat operator +(mat);
void display();
};



mat :: mat(int x,int y)
{
m=x;n=y;
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
a[i][j]=0;
}
}
void mat :: readmat()
{
cout<<"\nEnter matrix elements\n";
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
cin>>a[i][j];
}
mat mat:: operator +(mat obj)
{
mat temp(m,n);
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
{
temp.a[i][j]=a[i][j]+obj.a[i][j];
}
return temp;
}
void mat:: display()
{
int i,j;
for(i=0;i<m;i++)
{
cout<<"\n\n";
for(j=0;j<n;j++)
cout<<"\t"<<a[i][j];
}
}
int main()
{
int m1,n1;
clrscr();
cout<<"\nEnter the size(m,n) of matrix: ";
cin>>m1>>n1;
mat a(m1,n1),b(m1,n1),c(m1,n1);
cout<<"\nEnter martix 1: ";
a.readmat();
cout<<"\nEnter matrix 2: ";
b.readmat();
c=a.operator +(b);
cout<<"\nFirst Matrix :\n";
a.display();
cout<<"\nSecond Matrix :\n";
b.display();
cout<<"\nmatrix 1+matrix 2: ";
c.display();
getch();
return 0;
}




Output of the above program is:
Enter the size(m,n) of matrix: 2 2
Enter martix 1: Enter matrix elements
3 3
3 3
Enter matrix 2: Enter matrix elements
4 4
4 4
First Matrix :
3 3
3 3
Second Matrix :
4 4
4 4
matrix 1 + matrix 2:
7 7
7 7





Thanks
Mukesh Rajput
no image
How two dimensional arrays are initialized?

1. Initialization at design time: In this design techniques data element are stored in to the array at the time of declaration of the array. The elements of each row and each column are represented within the pair of flower brackets. 



Example : 
int array[3][3]={51,23,75,43,97,32,57,18};

2. Initialization of the array at the time of execution: In this design techniques input data in to two dimensional array, two for loops are used foe every value of the outer loop index. The inner loop is executed a specified number of times. 
Example : 
int array[5][4];
for(i=0; i<5; i++)
for(j=0; j<4; j++)
scanf(“%d”,&array[i][j]);

How do you print a matrix? 
A two dimensional array can be printed as follows:
int a[3][4];
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
printf(“%d\t”,a[i][j]);
}
printf(“\n”);
}





Thanks
Mukesh Rajput
no image
How one dimensional array is initialized?

The initialization can be done in two ways:
1. Initialization at design level : In this type of initialization of array the data values are stored in to the array at the time of array declaration. 
Example : 
int array[]={1,5,2,30,50};

2. Initialization at run time :  In this type of initialization of array, means to input data values in to the array at the time of execution of the program. 

Example using for loop: 
int array[10];
for(i=0;i<10;i++)
scanf(“%d”,&array[i]);

Example using do-while loop: 
int array[10];
i=0;
do
{
scanf(“%d”,&array[i]);
i++;
}
while(i<10);

Example using while loop: 
int array[10];
i=0;
while(i<10)
{
scanf(“%d”,&array[i]);
i++;
}

How to output the elements of one dimensional array?
To print elements of an array a for loop, do-while or while loop are used.

Example using for loop: 
int array[10];
for(i=0;i<10;i++)
printf(“%d\n”,array[i]);

Example using do-while loop: 
int array[10];
i=0;
do
{
printf(“%d\n”,array[i]);
i++;
}
while(i<10);

Example using while loop: 
int array[10];
i=0;
while(i<10)
{
printf(“%d\n”,array[i]);
i++;
}



Thanks
Mukesh Rajput


no image
Write a C++ program to Insert an element in an array after a specific position.

Program Code:
#include<iostream>
using namespace std;
int main()
{
int i, n, no,pos;
int array[100];
cout<<"Enter elements into the Array : ";
cin>>n;
for(i=0;i<n;i++)
{
cin>>array[i];
}
cout<<endl;
cout<<"Entered element into the Array are : ";
for(i=0;i<n;i++)
{
cout<<array[i];
cout<<" ";
}
cout<<endl;
cout<<"Enter position to insert new element into the Array: ";



cin>>pos;
cout<<endl;
if(pos>10)
{
cout<<"This is out of range";
}
else
{
cout<<"Enter new element to be inserted: ";
cin>>no;
cout<<endl;
for(i=n;i>=pos;i--)
{
array[i+1]=array[i];
}
array[pos]=no;
cout<<"After inserting new elements into the Array are : ";
for(i=0;i<n+1;i++)
{
cout<<array[i];
}
}
return 0;
}

The program output is tested on www.jdoodle.com
Output:
Enter elements into the Array : 1 2 3 4 5 6 7 8 9 10
Entered element into the Array are : 1 2 3 4 5 6 7 8 9 10
Enter position to insert new element into the Array: 9
Enter new element to be inserted: 30
After inserting new elements into the Array are : 1 2 3 4 5 6 7 8 9 30 10





Thanks
Mukesh Rajput
no image
Write a C++ program to find the transpose of a given matrix of size mxn, where m and n are interned by the user.




 Program Code:
#include<iostream>
using namespace std;
int main()
{
int a[15][15],b[15][15];
int i,j,m,n;
cout<<"Enter number of row in the given matrix : ";
cin>>m;
cout<<endl;
cout<<"Enter number of column in the given matrix : ";
cin>>n;
cout<<endl;
cout<<"Enter elements in an matrix : ";
cout<<endl;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cin>>a[i][j];
}
}
cout<<"Input for the given matrix is : ";



for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
b[i][j]=a[j][i];
}
}
cout<<"Transpose of the given matrix : ";
cout<<endl;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cout<<b[i][j]<<" ";
}
cout<<endl;
}
return 0;
}





The Program output is tested on www.jdoodle.com
Output:
Enter number of row in the given matrix : 3
Enter number of column in the given matrix : 3
Enter elements in an matrix : 1 2 3 4 5 6 7 8 9
Input for the given matrix is : 
1 2 3 
4 5 6 
7 8 9 
Transpose of the given matrix : 
1 4 7 
2 5 8 
3 6 9 





Thanks
Mukesh Rajput
no image
Write a program in C++ to Display Largest Element in the array.




Program Code:
#include <iostream> 
using namespace std;
int main() 
{    
int j, n;    
int array[20];    
cout << "Enter number of elements in the given array ";   
cin >> n;
cout<<endl;    
for( j = 0; j < n;  j++) // Loop to store number in the array  
{     
cout << "Enter Number is " << j + 1 << " : ";      
cin >> array[j];
cout<<endl;
}    
for(j = 1 ; j < n; j++) // Loop to store largest number at first position   
{         
if(array[0] < array[j])
{ 
array[0] = array[j]; 
}
}
cout << "Largest element in the array is = " << array[0];



return 0;
}



Output of the above program: 




Enter number of elements in the given array 

Enter Number is 1 : 2
Enter Number is 2 : 3
Enter Number is 3 : 5
Enter Number is 4 : 6
Enter Number is 5 : 4

Largest element in the array is = 6






Note: This program is tested on www.jdoodle.com under C++ language compiler link.




Thanks

Mukesh Rajput




no image
Declaration of 1-Dimensional Arrays:

In order to use the array in your programming, you must provide the following information to compiler of C++ you use in your computer.



1. The name of the array and it should not be an reserved keyword of C++ language.
2. Total number of element to be stored in the array at the time of its use.
3. Total number of memory location to be reserve.

Example:  Data_type_name  array_name[size];
where Data_type_name is a user defined name which they want to use like int, float, double etc.
array_name is the name of the array given by the user and it can't be a reserved key words of C++ language and size is the given element size of the variable.

Program: Write a program to initialize and print an array element.

no image
Introduction to Arrays in C++ language

An array is defined as a linear collection of similar data items, which are stored in continuous memory locations. In other words we can say that an array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier. The array of character is known as "string" whereas the array of integers and floats are known as simply array.



It should be important to note that all the elements of the arrays are of same type i.e we can't have an array of 20 elements, of which 10 are of integer type and 10 are of float type. All the elements of the array are accessed using the same variable name followed by different subscript index which is further written in square brackets. The subscript number of first element of the array in C++ is always 0 and the last index of the array element is 1 less than the total number of elements. 

Let's have a look on array and how elements are stored in it.