0% found this document useful (0 votes)
75 views12 pages

C Program To Insert An Element in To An Array at A Given Position

The document contains C program code snippets for array operations (insertion, deletion), searching algorithms (linear, binary), sorting algorithms (bubble sort), matrix operations (transpose), string operations (substring insertion, deletion, length) and polynomial operations (addition). The code snippets demonstrate basic data structures and algorithms concepts in C programming language.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views12 pages

C Program To Insert An Element in To An Array at A Given Position

The document contains C program code snippets for array operations (insertion, deletion), searching algorithms (linear, binary), sorting algorithms (bubble sort), matrix operations (transpose), string operations (substring insertion, deletion, length) and polynomial operations (addition). The code snippets demonstrate basic data structures and algorithms concepts in C programming language.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

/* C Program to insert an element in to an array at a given

position*/

int main()
{   int arr[100] = { 0 };
    int i, x, pos, n = 10;
     for (i = 0; i < 10; i++)
      arr[i] = i + 1;
       for (i = 0; i < n; i++)
        printf("%d ", arr[i]);
        x = 50;
       pos = 5;
       n++;
     for (i = n; i >= pos; i--)
        arr[i] = arr[i - 1];
     arr[pos - 1] = x;
       for (i = 0; i < n; i++)
        printf("%d ", arr[i]);
    printf("\n");
    return 0;
}
/* C program to delete an element from an array*/

#include <stdio.h>
 void main() {
int A[] = {1,3,5,7,8};
int k = 3, n = 5;
int i, j;
printf("The original array elements are :\n");
for(i = 0; i<n; i++) {
printf("A[%d] = %d \n", i, A[i]); }
j = k;
while( j < n) {
A[j-1] = A[j];
j = j + 1;
}
n = n -1;
printf("The array elements after deletion :\n");
for(i = 0; i<n; i++) {
printf("A[%d] = %d \n", i, A[i]);
}
}
/* C program to search an element using linear search*/
#include<stdio.h>
int main()
{
  int a[100], search, i, n;
  printf("Enter number of elements in array\n");
  scanf("%d", &n);
  printf("Enter %d integer(s)\n", n);
  for (i = 0; i < n; i++)
  scanf("%d", &a[i]);
  printf("Enter a number to search\n");
  scanf("%d", &search);
  for (i = 0; i < n; i++)
  {
    if (array[i] == search)    
    {
      printf("%d is present at location %d.\n", search, i+1);
      break;
    }
  }
  if (i == n)
    printf("%d isn't present in the array.\n", search);
  return 0;
}
 /* C Program to implement binary search*/

#include <stdio.h>
int main()
{
  int i, first, last, middle, n, search, array[100];
  printf("Enter number of elements\n");
  scanf("%d", &n);
  printf("Enter %d integers\n", n);
  for (i = 0; i < n; i++)
    scanf("%d", &array[i]);
  printf("Enter value to find\n");
  scanf("%d", &search);
  first = 0;
  last = n - 1;
  middle = (first+last)/2;
  while (first <= last) 
{
    if (array[middle] < search)
      first = middle + 1;
    else if (array[middle] == search) 
{
      printf("%d found at location %d.\n", search, middle+1);
      break;
    }
    else
      last = middle - 1;
    middle = (first + last)/2;
  }
  if (first > last)
    printf("Not found! %d isn't present in the list.\n", search);
  return 0;
}
 
/* C program to sort an array of elements using bubble sort*/
#include <stdio.h>
int main()
{
  int array[100], n, i, j, temp;
  printf("Enter number of elements\n");
  scanf("%d", &n);
  printf("Enter %d integers\n", n);
  for (i = 0; i < n; i++)
    scanf("%d", &array[i]);
  for (i = 0 ; i < n - 1; i++)// no of passes
  {
    for (j = 0 ; j < n - i - 1; j++)// no of comparison
    {
      if (array[j] > array[j+1]) 
      {
        temp       = array[j];
        array[j]   = array[j+1];
        array[j+1] = temp;
      }
    }
  }
  printf("Sorted list in ascending order:\n");
  for (i = 0; i < n; i++)
     printf("%d\n", array[i]);
  return 0;
}
 

/* C program to find the transpose of a given sparse matrix*/


#include<stdio.h>
#define MAX 20

void printsparse(int[][3]);
void readsparse(int[][3]);
void transpose(int[][3],int[][3]);

int main()
{
int b1[MAX][3], b2[MAX][3], m, n;
printf("Enter the size of matrix (rows,columns):");
scanf("%d%d",&m,&n);
b1[0][0]=m;
b1[0][1]=n;
readsparse(b1);
transpose(b1,b2);
printsparse(b2);
}

void readsparse(int b[MAX][3])


{
int i,t;
printf("\nEnter no. of non-zero elements:");
scanf("%d",&t);
b[0][2]=t;

for(i=1;i<=t;i++)
{
printf("\nEnter the next triple(row,column,value):");
scanf("%d%d%d",&b[i][0],&b[i][1],&b[i][2]);
}
}

void printsparse(int b[MAX][3])


{
int i,n;
n=b[0][2]; //no of 3-triples
printf("\nAfter Transpose:\n");
printf("\nrow\t\tcolumn\t\tvalue\n");
for(i=0;i<=n;i++)
printf("%d\t\t%d\t\t%d\n",b[i][0],b[i][1],b[i][2]);
}

void transpose(int b1[][3],int b2[][3])


{
int i,j,k,n;
b2[0][0]=b1[0][1];
b2[0][1]=b1[0][0];
b2[0][2]=b1[0][2];

k=1;
n=b1[0][2];
for(i=0;i<b1[0][1];i++)
for(j=1;j<=n;j++)
if(i==b1[j][1])
{
b2[k][0]=b1[j][1];
b2[k][1]=b1[j][0];
b2[k][2]=b1[j][2];
k++;
}
}

/* C program to insert a substring in a given string*/


#include <stdio.h>
#include<stdlib.h>
#include <string.h>

void insert(char *s, char *t, int i)


{
char result[80], *temp=result;
if(i<0 && i>strlen(s))
{
printf("invalid position");
exit(0);
}
if(!strlen(s))// if s is empty string
strcpy(s,t);
else if (strlen(t))
{
strncpy(temp,s,i);
strcat(temp,t);
strcat(temp,(s+i));
strcpy(s,temp);
}
puts(s);
}

int main()
{
char s[20], t[40];
int pos;
printf("enter the text\n");
gets(s);
printf("enter the substring\n");
gets(t);
printf("enter the position\n");
scanf(" %d", &pos);
insert(s,t,pos);
return 0;
}

/* C program to read, print and add two polynomials*/

#include <stdio.h>
#include <stdlib.h>
struct poly
{
int coeff;
int expo;
};

struct poly p1[10],p2[10],p3[20];

int readPoly(struct poly []);


int addPoly(struct poly [],struct poly [],int ,int ,struct poly []);
void displayPoly( struct poly [],int terms);

int main()
{
int t1,t2,t3;
t1=readPoly(p1);
printf(" \n First polynomial : ");
displayPoly(p1,t1);
t2=readPoly(p2);
printf(" \n Second polynomial : ");
displayPoly(p2,t2);

t3=addPoly(p1,p2,t1,t2,p3);
printf(" \n\n Resultant polynomial after addition : ");
displayPoly(p3,t3);
printf("\n");
return 0;
}

int readPoly(struct poly p[10])


{
int t1,i;

printf("\n\n Enter the total number of terms in the polynomial:");


scanf("%d",&t1);

printf("\n Enter the COEFFICIENT and EXPONENT in DESCENDING ORDER\n");


for(i=0;i<t1;i++)
{
printf(" Enter the Coefficient(%d): ",i+1);
scanf("%d",&p[i].coeff);
printf(" Enter the exponent(%d): ",i+1);
scanf("%d",&p[i].expo);
}
return(t1);
}

int addPoly(struct poly p1[10],struct poly p2[10],int t1,int t2,struct poly p3[10])
{
int i,j,k;
i=0;
j=0;
k=0;

while(i<t1 && j<t2)


{
if(p1[i].expo==p2[j].expo)
{
p3[k].coeff=p1[i].coeff + p2[j].coeff;
p3[k].expo=p1[i].expo;

i++;
j++;
k++;
}
else if(p1[i].expo>p2[j].expo)
{
p3[k].coeff=p1[i].coeff;
p3[k].expo=p1[i].expo;
i++;
k++;
}
else
{
p3[k].coeff=p2[j].coeff;
p3[k].expo=p2[j].expo;
j++;
k++;
}
}

while(i<t1)
{
p3[k].coeff=p1[i].coeff;
p3[k].expo=p1[i].expo;
i++;
k++;
}
while(j<t2)
{
p3[k].coeff=p2[j].coeff;
p3[k].expo=p2[j].expo;
j++;
k++;
}

return(k);
}

void displayPoly(struct poly p[10],int term)


{
int k;

for(k=0;k<term-1;k++)
printf("%d(x^%d)+",p[k].coeff,p[k].expo);
printf("%d(x^%d)",p[term-1].coeff,p[term-1].expo);
}

/* C Program to delete a substring from a given string*/

#include<stdio.h>
#include<string.h>

int main()
{
int i, j = 0, k = 0,n = 0;
int flag = 0;

char str[100], neww[100], word[100];

printf("Enter a String: ");


gets(str);

printf("\n\n Enter a Word You Want to be Removed: ");


gets(word);

for(i = 0 ; str[i] != '\0' ; i++)


{
k = i;

while(str[i] == word[j])
{
i++,j++;
if(j == strlen(word))
{
flag = 1;
break;
}
}
j = 0;

if(flag == 0)
i = k;
else
flag = 0;

neww[n++] = str[i];
}

neww[n] = '\0';

printf("\n\n After Removing Word From String: %s",neww);


}

/* C program to find the length of a String*/

#include <stdio.h>
#include <string.h>
main()
{
    char s1[20];
    int len;
    printf("\nEnter any string: ");
    gets(s1);
    len = strlen(s1);
    printf("\nLength of string: %d", len);
  }
 /* C program to find the length of a String*/
#include <stdio.h>
int main()
{
char s[] = "Programming is fun";
int i;
 
for (i = 0; s[i] != '\0'; ++i);
printf("Length of the string: %d", i);
return 0;
}

/* C program to delete a substring from a given text*/

#include <stdio.h>
int search(char[], char[]);
int delete_word(char[], char[], int);
int main()
{
char str[80], word[50];
int index;
printf("Enter string:\n");
gets(str);
printf("Enter word to delete:\n");
gets(word);
index = search(str, word);
if (index != - 1)
{
delete_word (str, word, index);
printf("String without word:\n%s", str);
}
else
{
printf("The word not present in the string.");
}
return 0;
}
/* Function returns the index of str where word is found */
int search(char str[], char word[])
{
int l, i, j; /* finding length of word */
for (l = 0; word[l] != '\0'; l++);
for (i = 0, j = 0; str[i] != '\0' && word[j] != '\0'; i++)
{
if (str[i] == word[j])
{
j++;
}
else
{
j = 0;
}
}
if (j == l)
{ /* substring found */

return (i - j);
}
else
{
return - 1;
}
}

int delete_word(char str[], char word[], int index)


{
int i, l; /* finding length of word */
for (l = 0; word[l] != '\0'; l++);
for (i = index; str[i] != '\0'; i++)
{
str[i] = str[i + l + 1];
}
}

/* C program to search a pattern in a given string */(using Naives


method)
#include <stdio.h>
#include <string.h>
int match(char [], char []);
int main() {
  char a[100], b[100];
  int position;
  printf("Enter some text\n");
  gets(a);
  printf("Enter a string to find\n");
  gets(b);
  position = match(a, b);
  if (position != -1) {
    printf("Found at location: %d\n", position + 1);
  }
  else {
    printf("Not found.\n");
  }
  return 0;
}
int match(char text[], char pattern[]) 
{
  int c, d, e, text_length, pattern_length, position = -1;
  text_length    = strlen(text);
  pattern_length = strlen(pattern);
  if (pattern_length > text_length) {
    return -1;
  }
  for (c = 0; c <= text_length - pattern_length; c++)
 {
    position = e = c;
    for (d = 0; d < pattern_length; d++) {
      if (pattern[d] == text[e]) {
        e++;
      }
      else {
        break;
      }
    }
    if (d == pattern_length) {
      return position;
    }
  }
  return -1;
}
 

You might also like