C Program To Insert An Element in To An Array at A Given Position
C Program To Insert An Element in To An Array at A Given Position
C Program To Insert An Element in To An Array at A Given Position
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;
}
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);
}
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]);
}
}
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++;
}
}
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;
}
#include <stdio.h>
#include <stdlib.h>
struct poly
{
int coeff;
int expo;
};
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 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;
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);
}
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);
}
#include<stdio.h>
#include<string.h>
int main()
{
int i, j = 0, k = 0,n = 0;
int flag = 0;
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';
#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;
}
#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;
}
}