Array in C Programming

We shall learn Array using C Programming. Here is the list what we are going to learn:
1. Declare Array and Print in C Programming.

#include 

int main()
{
    printf("==========Part 1=========\n");
    int x[]={2,8,6,4,3};
    for(int i=0;i<5;i++){
           printf("Data: %d \n",x[i]);
    }
    printf("=======Part 2====\n");
  printf("Size Of Array: %d \n",sizeof(x)/sizeof(int));

    return 0;
}</pre>
<strong>2.Take input from console and Display array elements in C Programming. </strong>
<!--?prettify linenums=true?-->
<pre class="prettyprint">#include 

int main()
{

printf("===Part 1===\n");
int arr[5];
int j;
for(j=0;j&lt;5;j++){
    scanf("%d",&amp;arr[j]);
  
}
int p;
 for(p=0;p&lt;5;p++){
           printf("Data arr: %d \n",arr[p]);
    }
    
    return 0;
}

3. Display array elements with Memory addresses in C Programming.

#include 
int main() {
   int arr[5] = { 5, 7, 9,11,13 };
 printf("====== Normal Print =====\n");
   printf("\n arr[0] ,value=%d : address=%u", arr[0], &amp;arr[0]);
   printf("\n arr[1] ,value=%d : address=%u", arr[1], &amp;arr[1]);
   printf("\n arr[2] ,value=%d : address=%u", arr[2], &amp;arr[2]);
   printf("\n arr[3] ,value=%d : address=%u", arr[3], &amp;arr[3]);
   printf("\n arr[4] ,value=%d : address=%u", arr[4], &amp;arr[4]);
   printf("\n=== For Loop Print ===\n");
  for(int i=0;i&lt;sizeof(arr)/sizeof(int);i++){
      printf("\n arr[%d]: value=%d : address=%u",i, arr[i], &amp;arr[i]);
  }
   return (0);
}

4. Insert an element in an Array according to the position in C Programming.

To solve this problem, we shall take 5 variables like
arr[100],numOfElements, insertedElement, i, position;
arr[100] = we shall be able to keep 100 elements in this variable.
numOfElements = we have to decide how many elements do we want to use in this program.
insertedElement = suppose we want to put 10
i = it will be used for For Loop.
position = though array index starts from 0(zero),but we shall assume position from 1

 

#include  

int main() {
   int arr[100], insertedElement, numOfElements, i, position;

   printf("\nEnter number of elements :");
   scanf("%d", &amp;numOfElements);

   for (i = 0; i &lt; numOfElements; i++) { 
scanf("%d", &amp;arr[i]); } 
printf("\nEnter the insertedElement to be inserted :"); 
scanf("%d", &amp;insertedElement);
 printf("\nEnter the position"); 
scanf("%d", &amp;position); 
//Create space at the specified position
 for (i = numOfElements; i &gt;= position; i--) {
      arr[i] = arr[i - 1];
   }

   numOfElements++;
   arr[position - 1] = insertedElement;

   //Print out the result of insertion
   for (i = 0; i &lt; numOfElements; i++)
      printf("\n %d", arr[i]);

   return (0);
}

Explanation:
Step-1: Enter number of elements Like 5
Step-2: Now it is time to insert 5 elements like 3 5 7 9 11
Step-3: Enter the inserted Element to be inserted Like 10
Step-4: Now decide where do we want to insert, here I have decided to insert at position no. 2, that means after 3
Step-5: Now it is time to observe output, so the output is 3 10 5 7 9 11

5. There is a sorted array, now we have to insert an element at the specified position.


#include 

int main() {
   int arr[100], insertedElement, numOfElements, i, position;

   printf("\nEnter number of elements :");
   scanf("%d", &amp;numOfElements);

   for (i = 0; i &lt; numOfElements; i++) {
      scanf("%d", &amp;arr[i]);
   }

   printf("\nEnter the insertedElement to be inserted :");
   scanf("%d", &amp;insertedElement);
for (i = 0; i &lt; numOfElements; i++) {
 printf("%d", arr[i]); 
if(arr[i]&gt;=insertedElement){
          position=i+1;
          break;
      }
   }
  

   //Create space at the specified position
   for (i = numOfElements; i &gt;= position; i--) {
      arr[i] = arr[i - 1];
   }

   numOfElements++;
   arr[position - 1] = insertedElement;

   //Print out the result of insertion
   for (i = 0; i &lt; numOfElements; i++)
      printf("\n %d", arr[i]);

   return (0);
}

6. Delete an array element

#include 

int main() {
   int arr[100], insertedElement, numOfElements, i, position;

   printf("\nEnter number of elements :");
   scanf("%d", &amp;numOfElements);

   for (i = 0; i &lt; numOfElements; i++) {
      scanf("%d", &amp;arr[i]);
   }

   printf("\nEnter the insertedElement to be inserted :");
   scanf("%d", &amp;insertedElement);
for (i = 0; i &lt; numOfElements; i++) {
 printf("%d", arr[i]); 
if(arr[i]&gt;=insertedElement){
          position=i+1;
          break;
      }
   }
  

/* loop for the deletion  */
   while (position &lt; numOfElements) {
      arr[position - 1] = arr[position];
      position++;
   }
   numOfElements--;  // No of elements reduced by 1

   //Print out the result of insertion
   for (i = 0; i &lt; numOfElements; i++)
      printf("\n %d", arr[i]);

   return (0);
}

7.Delete an array element according to position

#include 

int main() {
   int arr[100], numOfElements, i, position;

   printf("\nEnter number of elements :");
   scanf("%d", &amp;numOfElements);

   for (i = 0; i &lt; numOfElements; i++) {
      scanf("%d", &amp;arr[i]);
   }

   printf("\nEnter the position to be deleted :");
   scanf("%d", &amp;position);
for (i = 0; i &lt; numOfElements; i++) {
      printf("%d", arr[i]);
      
        }
   /* loop for the deletion  */
   while (position &lt; numOfElements) {
      arr[position - 1] = arr[position];
      position++;
   }
   numOfElements--;  // No of elements reduced by 1

   //Print out the result of insertion
   for (i = 0; i &lt; numOfElements; i++)
      printf("\n %d", arr[i]);

   return (0);
}

8. Copy all elements of an array into Another array Like from x to y in C Programming.

#include 

int main() {
   int x[5]={2,6,8,9,10};
   int y[5],i;
   
   for (i = 0; i &lt; sizeof(x)/sizeof(int); i++){
      y[i]=x[i];
      printf("\n %d", y[i]);
}
   return (0);
}

9. Search single array element in C Programming.

>#include 

int main() {
   int x[5]={2,6,8,9,10};
   int i,searchElement;
   
   searchElement=9;
   i=0;
  while(i&lt;5 &amp;&amp; x[i]!=searchElement){
      i++;
  }
 
      //If i &lt; 5 then Match found
   if (i &lt; 5) {
       printf("\nSearch Element Found at index=[%d], %d",i, i+1);
   } else {
      printf("Number not found");
   }
     

   return (0);
}

10. Search single array element,but multiple matches in C Programming.

#include 


int main() {
   int x[5]={2,6,8,9,9};
   int i,searchElement;
   int rs[1];
   
   searchElement=9;
   i=0;
  while(i&lt;5 &amp;&amp; x[i]!=searchElement){
      i++;
  }
 
      //If i &lt; 5 then Match found
      int j=0;
   if (i &lt; 5) {
       rs[j]=x[i];
        printf("\nSearch Element Found at index=[%d], %d",j, rs[j]);
     j++;
   } else {
      printf("Number not found");
   }
    // printf("\nSearch Element Found at index=[%d], %d",i, rs[0]);
     

   return (0);
}

11. Reversing an Array Elements in C Programming


int main () {

   /* local variable definition */
   int a[6]={2,6,8,3,7,5};
   int b[6],i,j;
i=0;
j=sizeof(a)/sizeof(int);
   /* while loop execution */
   while( j &gt; 0 ) {
     // printf("value of a: %d\n", a[j-1]);
      b[i]=a[j-1];
      i++;
      j--;
   }
   int p=0;
   while(p&lt;sizeof(b)/sizeof(int)){
        printf("value of a: %d\n", b[p]);
        p++;
   }
 
   return 0;
}

12.Find the Largest Element in Array in C Programming

#include 
 
int main () {

     int arr[6]={112,6,8,3,7,15};
int i,largestNumber;

largestNumber=arr[0];
for(i=0; i &lt; sizeof(arr)/sizeof(int);i++){ 
if(arr[i] &gt; largestNumber){
       largestNumber=arr[i];
       
   }
}
     printf("Largest Element of Array: %d\n", largestNumber);
 
   return 0;
}

13.Find the Smallest Element in Array in C Programming

 

#include 
 
int main () {

     int arr[6]={112,6,1,3,7,15};
int i,smallestNumber;

smallestNumber=arr[0];
for(i=0; i &lt; sizeof(arr)/sizeof(int);i++){
   if(arr[i] &lt; smallestNumber){
       smallestNumber=arr[i];
       
   }
}
     printf("Smallest Element of Array: %d\n", smallestNumber);
 
   return 0;
}

14.Find the Sum of Elements in Array in C Programming

#include 
 
int main () {

     int arr[6]={42,6,21,3,17,15};
int i,sum;

sum=0;
for(i=0; i &lt; sizeof(arr)/sizeof(int);i++){
  sum+=arr[i];
}
     printf("Sum of all Element of Array: %d\n", sum);
 
   return 0;
}

Leave a Reply