Showing posts sorted by relevance for query Array. Sort by date Show all posts

C Array

Array in C Language
#An array is a collection of similar data type value in a single variable. It is a derived data type in C, which is constructed from fundamental data type of C language.

Advantage of array

  • Code Optimization: Less code is required, one variable can store numbers of value.
  • Easy to traverse data: By using array easily retrieve the data of array.
  • Easy to sort data: Easily short the data using swapping technique
  • Random Access: With the help of array index you can randomly access any elements from array.

Dis-Advantage of array

Fixed Size: Whatever size, we define at the time of declaration of array, we can not change their size, if you need more memory in that time you can not increase memory size, and if you need less memory in that case also wastage of memory.

Declaring Array

To declare an array in C you need to declare datatype and size of an array.

Syntax

 
datatype arrayName[SIZE];

Example

 
int roll_no[10];

Initializing Array

Initializing is a process to initialize the value in array variable. This is happen in two ways, initialize array one by one or all elements are initializing once.

Initialization of array one by one

 
int arr[5]; 
arr[0]=10;
arr[1]=20; 
arr[2]=30;
 arr[3]=40;
 arr[4]=50; 

Initialization of array at once

 
int arr[]={10,20,30,40,50};

Accessing Array Elements

We can access array elements with the help of index value of element.

Example

 
int arr[]={10,20,30,40,50};
arr[3]  // here 3 is index value and it return 40

Example of array

 
#include<stdio.h>
#include<conio.h>

void main()
{
int i, marks[]={80, 62, 70, 90, 98}; //declaration and initialization of array
clrscr();

//traversal of array 
for(i=0;i<5;i++)
{
printf("\n%d",marks[i]);
}
getch();
}

Example

 
80
62
70
90
98

2-dimentional array

  • In 2-dimentional elements are arranged in row and column format.
  • When we are working with 2-dimentional array we require to refer 2-subscript operator which indicates row and column sizes.
  • The main memory of 2-dimentional array is rows and sub-memory is columns.
  • On 2-dimentional array when we are referring one-subscript operator then if gives row address, 2-subscript operator will gives element.
  • On 2-dimentional array arrayName always gives main memory that is 1st row base address, arrayName will gives next row base address.

Syntax

datatype  arrayName[SIZE][SIZE];

Important points related to array

Always size of the array must be an unsigned integer value which is greater than '0' only. In declaration of the array size must be required to mention, if size is not mention then compiler will give an error.

Example

 
int arr[]; //error
In declaration of the array size must be assigned type which value is greater than 0. In initialization of the array if specific number of values are not initialized it then rest of all elements will be initialized with it '0'.

Example

 
int arr[5]={10,20}; // yes valid
arr[0]=10;
arr[1]=20; 
arr[2], arr[3], arr[4]; // initialized with zero 
In initialization of the array mentioning the size is optional, in this case how many elements are initialize it that many variable are created.

Example

 
int arr[]={10,20,30,40,50}; // valid
Size=5;
Sizeof(arr)=10 byte

Important points for Array

  • In implementation when we required 'n' number of variables of same data type then go for an array.
  • When we are working with arrays always memory will created in continues memory location, so randomly we can access the data.
  • In arrays all elements will share same name with unique identification value called index.
  • Always array index will start with '0' and end with 'size-1'.
  • When we are working with array compile time memory management will occur that is static memory allocation.

Merge an array

C++ Program to Merge two Arrays in third array

In this program we enter an elements in any two array and then these two array (elements of array) are store in third array.

Example

#include<iostream.h>
#include<conio.h>

void main()
 {
   int a[10],b[10],c[20],i;
   clrscr();
   cout<<"Enter Elements in 1st Array: ";
   for(i=0;i<10;i++)
   {
   cin>>a[i];
   }
   cout<<"Enter Elements in 2nd Array: ";
   for(i=0;i<10;i++)
   {
   cin>>b[i];
   }
   cout<<"\nElements of Array After Merge: ";
   for(i=0;i<10;i++)
   {
    c[i]=a[i];
    c[i+10]=b[i];
   }
   for(i=0;i<20;i++)
   {
   cout<<c[i];
   }
  getch();
 }
Output
merge tow array

C++ Program to Find Duplicate Elements in Array


Array is the collection of similar data type, In this program we find duplicate elements from an array, Suppose array have 3, 5, 6, 11, 5 and 7 elements, in this array 5 appear two times so this is our duplicate elements.

Find Duplicate Elements in Array in C++

#include<iostream.h>
#include<conio.h>

 void main()
 {
  int i,arr[20],j,no;
  clrscr();
  cout<<"Enter Size of array: ";
  cin>>no;
  cout<<"Enter any "<<no<<" num in array: ";
  for(i=0;i<no;i++)
  {
   cin>>arr[i];
  }
  cout<<"Dublicate Values are: ";
  for(i=0; i<no; i++)
   {
    for(j=i+1;j<no;j++)
    {
    if(arr[i]==arr[j])
    {
    cout<<"\n"<<arr[i];
    }
   }
   }
  getch();
 }

Output

Enter Size of Array : 5
Enter any 5 elements in Array: 
5 4 5 2 3 
Duplicate Elements are: 
5

Structure in C

Structure in C
#Structure is a user defined data type which hold or store heterogeneous data item or element in a singe variable. It is a Combination of primitive and derived data type.

Why Use Structure in C

In C language array is also a user defined data type but array hold or store only similar type of data, If we want to store different-different type of data in then we need to defined separate variable for each type of data.
Example: Suppose we want to store Student record, then we need to store....
  • Student Name
  • Roll number
  • Class
  • Address
For store Student name and Address we need character data type, for Roll number and class we need integer data type.
If we are using Array then we need to defined separate variable.

Example

char student_name[10], address[20];
int roll_no[5], class[5];
If we use Structure then we use single variable for all data.

Syntax

struct stu
{
char student_name[10];
char address[20];
int roll_no[5];
int class[5];
};
Note: Minimum size of Structure is one byte and Maximum size of Structure is sum of all members variable size.
Note: Empty Structure is not possible in C Language.

Defining a Structure

Syntax

struct tagname
{
Datatype1 member1;
Datatype2 member2;
Datatype3 member3;
...........
};
At end of the structure creation (;) must be required because it indicates that an entity is constructed.

Example

struct emp
{
int id;
char name[36];
int sal;
};
sizeof(struct emp) // --> 40 byte (2byte+36byte+2byte)

Syntax to create structure variable

struct tagname variable;

Difference Between Array and Structure

ArrayStructure
1Array is collection of homogeneous data.Structure is the collection of heterogeneous data.
2Array data are access using index.Structure elements are access using . operator.
3Array allocates static memory.Structures allocate dynamic memory.
4Array element access takes less time than structures.Structure elements takes more time than Array.

Example of Structure in C

#include<stdio.h>
#include<conio.h>

struct emp
{
int id;
char name[36];
float sal;
};

void main()
{
struct emp e;
clrscr();
printf("Enter employee Id, Name, Salary: ");
scanf("%d",&e.id);
scanf("%s",&e.name);
scanf("%f",&e.sal);

printf("Id: %d",e.id);
printf("\nName: %s",e.name);
printf("\nSalary: %f",e.sal);
getch();
}

Output

Output: Enter employee Id, Name, Salary: 5 Spidy 45000 Id : 05 Name: Spidy Salary: 45000.00

Syntax to access structure members

By using following operators we can access structure members.

Syntax

.     struct to member
-->   pointer to member
When the variable is normal type then go for struct to member operator.
When the variable is pointer type then go for pointer to member operator.

Difference Between Structure and Pointer in C

Structure in C refer to a collection of various data types for example you create a structure named "Student" which contains his name , roll no, DOB etc. Name is string, Roll no is int.
While pointer refer to address in C & symbol are used to point some particular place in C memory.

C++ Program to Reverse an Array


Array store all data in array on the basis of index. For reverse an array element you nedd to interchange elements of array on the basis of index value. .

C++ Program to Reverse an Array


#include<iostream.h>
#include<conio.h>

 void main()
  {
   int a[20],b[20],i,j,n;
   clrscr();
   cout<<"How many elements you want to enter: ";
   cin>>n;
   cout<<"Enter any "<<n<<" elements in Array: ";
   for(i=0; i<n ;i++)
   {
   cin>>a[i];
   }
   cout<<"Reverse of Array: ";

   for(i=n-1,j=0; i>=0;i--,j++)
   {
    b[i]=a[j];
   }
   for(i=0; i<n ;i++)
   {
   cout<<b[i];
   }
  getch();
  }

Output

How many elements you want to enter : 5
Enter any 5 elements in Array: 
1 4 2 7 5
Reverse of Array: 5 7 2 4 1

C++ Program to Delete an element from Array

C++ Program to Delete an element from Array

Delete an element in an array from specific position


#include<iostream.h>
#include<conio.h>

void main()
  {
   int i,a[5],no,pos;
   clrscr();
   cout<<"Enter data in array: ";
   for(i=0;i<5;i++)
   {
    cin>>a[i];
   }
   cout<<"\n\nStored Data in array:  ";
   for(i=0;i<5;i++)
   {
    cout<<a[i];
   }
   cout<<"\n\nEnter poss. of element to delete: ";
   cin>>pos;
   if(pos>5)
   {
   cout<<"\n\nThis value is out of range: ";
   }
   else
   {
   --pos;
   for(i=pos;i<=4;i++)
   {
    a[i]=a[i+1];
   }
   cout<<"\n\nNew data in array: ";
   for(i=0;i<4;i++)
   {
    cout<<a[i];
  }
  }
   getch();
 }
 

Output

Enter data in array: 10 20 30 40 50
Stored Data in array: 10 20 30 40 50
Enter poss. of element to delete: 2
New data in array: 10 20 40 50