Welcome !!

NICETECHVIDYA

Tutorials

Web Designs

You are welcome to Aptech Designers, designed by Pratik from Nepal.

Creativity is my passion. Logo describes your details about company.

Graphics Design needs vision to create yours unique digital concept.

Responsive and All Browser Compatible Dynamic Web Layouts our target.

Home Python C Language J A V A Our Courses
This WebSite is under Construction....

Thursday, 10 March 2022

39.arrays:Specification13-16


Specification13:

Accept "n" elements into an array, delete an element at a required position and print the resultant array.

 

 

 

 

Logic:

We will overwrite all the elements one place left from the element next to the position.

We are overwriting the element to be deleted by the next element.

 

 

 

 

 

 

 

 

 

 

 


 

 

 

 

 

 

 

Program:

#include<stdio.h>

void main()

{

int n,i,a[50],pos;

 

printf("How many elements?");

scanf("%d",&n);

printf("Enter %d elements:\n",n);

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

   scanf("%d",&a[i]);

printf("Enter the position:");

scanf("%d",&pos);

if(pos<1 || pos>n)                

    printf("Invalid position");

else

{

      pos--;

      for(i=pos;i<n-1;i++)

           a[i]=a[i+1];

      n--;

      printf("The resultant array:\n");

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

        printf("%5d",a[i]); 

}  

 

}

 

Execution:

How many elements: 7

Enter 6 elements:

12   67   54   34   98   27   15

Enter the position:  4

The resultant array:

45   66   45   98   27   15

 

Specification14:

Accept "n" elements into an array and find whether an element is existed or not. (Searching)

 

Logic:

We will store 0 into "flag"

We will traverse element by element in searching for the required element (x).

If the required element is existed then 1 is stored into "flag".

At the end of loop, if the value of "flag" is 1 then the required element is existed otherwise not existed.

flag=0;

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

    if(a[i]==x)

    {

         flag=1;

         break;

    }

 

 

 

 

 

 

 

 

 

 

Program:

#include<stdio.h>

void main()

{

 int n,i,x,flag,a[50];

 

printf("How many elements?");

scanf("%d",&n);

printf("Enter %d elements:\n",n);

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

   scanf("%d",&a[i]);

printf("Find what?");

scanf("%d",&x);

flag=0;

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

    if(a[i]==x)

   {

         flag=1;

         break;

   }

if(flag)

    printf("Existed");

else

    printf("Not existed");

 

}

 

Execution1:

How many elements? 6

Enter 6 elements:

45    56   78   45   34   23

Find what? 78

Existed       

 

Execution2:

How many elements? 6

Enter 6 elements:

45    56   78   45   34   23

Find what? 17

Not existed

 

Specification15:

Accept "n" elements into an array, find an element and replace it with another element and print the resultant array.

 

Logic:

We will traverse element by element in searching for the required element (x).

If the required element is existed then we will overwrite it with new element (a[i]=y;).

Finally we will print the total array as output.

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

   if(a[i]==x)

        a[i]=y;

 

 

 

 

 

 


Program:

#include<stdio.h>

void main()

{

int n,i,x,y,a[50];

 

printf("How many elements?");

scanf("%d",&n);

printf("Enter %d elements:\n",n);

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

    scanf("%d",&a[i]);

printf("Find what?");

scanf("%d",&x);

printf("Replace with what?");

scanf("%d",&y);

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

{

   if(a[i]==x)

      a[i]=y;

}

printf("The resultant array:\n");

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

   printf("%5d",a[i]);

 

}

 

Execution:

How many elements? 6

Enter 6 elements:

21   56   34   23   56   32

Find what? 56

Replace with what? 66

The resultant array:

21   66   34   23   66   32

 

Specification16:

Accept "n" elements into one array, "m" elements into another array, concatenate the elements of second array to the first array and print the resultant array (Concatenating two vectors).

 


Logic:

 

 

 

 

 

 

 

                                                                                

 

 

 

 

 

 

Program:

#include<stdio.h>

void main()

{

int i,j,n,m,a[50],b[50];

 

printf("How many elements into 1st vector:");

scanf("%d",&n);

printf("How many elements into 2nd vetor:");

scanf("%d",&m);

printf("Enter %d elements:\n",n);

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

   scanf("%d",&a[i]);

printf("Enter %d elements:\n",m);

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

   scanf("%d",&b[i]);

for(i=n,j=0;j<m;i++,j++)

    a[i]=b[j];

printf("The resultant array:\n");

for(i=0;i<n+m-1;i++)

   printf("%5d",a[i]);

 

}

 

Execution:

How many elements into 1st vector: 5

How many elements into 2nd vector: 5

Enter 5 elements:

5   9   6   4   7

Enter 5 elements:

8   3   2   1   5

The resultant array:

5   9   6   4   7   8   3   2   1   5


0 comments:

Post a Comment