1) Write a C Program to Count number of occurrences (or frequency) in a
given sorted array
Input: arr[] = {1, 1, 2, 2, 2, 2,
3,}, x = 2
Output: 4 // x (or 2) occurs 4 times
in arr[]
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int arr[] = {1, 1, 2, 2, 2, 2,
3};
int x = 2, count = 0;
int n = sizeof(arr) /
sizeof(arr[0]);
for (int i = 0; i < n; i++)
{
if (arr[i] == x)
{
count++;
}
}
printf("Occurs %d time in
arr[] \n", count);
getch();
return 0;
}
========================================================
2) Write a C program to accept n elements, store those elements in array
and store the square of these numbers in another array and display both the
array.
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
void displaySquare(int arr[], int squareArr[], int n);
int main()
{
int arr[20], b[20];
int i, n;
printf("Enter the size of
array \n");
scanf("%d", &n);
printf("Enter the array
Elements \n");
for (i = 0; i < n; i++)
{
scanf("%d",
&arr[i]);
}
displaySquare(arr, b, n); //
calling 1.main array 2.square array 3. size of array.
printf("Square of array
element :\n");
for (i = 0; i < n; i++)
{
printf("%d\t",
b[i]);
}
getch();
return 0;
}
void displaySquare(int arr[], int squareArr[], int n)
{
int i;
for (i = 0; i < n; i++)
{
squareArr[i] = arr[i] *
arr[i];
}
}
===========================================================
3) Write a C program to Copy one array into another array.
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
void copyArray(int arr[], int cpyArr[], int n);
int main()
{
int arr[20], b[20];
int i, n;
printf("Enter the size of
array \n");
scanf("%d", &n);
printf("Enter the array
Elements \n");
for (i = 0; i < n; i++)
{
scanf("%d",
&arr[i]);
}
copyArray(arr, b, n);
printf("Copied of array
element :\n");
for (i = 0; i < n; i++)
{
printf("%d\t",
b[i]);
}
getch();
return 0;
}
void copyArray(int arr[], int cpyArr[], int n)
{
for (int i = 0; i < n; i++)
{
cpyArr[i] = arr[i];
}
}
==================================================================
Set B :
1) Write a C program accept the polynomial and display it in format e.g.
6x^4+2x^2+5x^1+3
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int n;
printf("Enter the highest
power of the polynomial : ");
scanf("%d", &n);
int arr[n + 1];
for (int i = 0; i <= n; i++)
{
printf("Enter the
coefficient for x^%d : ", i);
scanf("%d",
&arr[i]);
}
printf("Polynomial :
");
for (int i = n; i >= 0; i--)
{
if (arr[i] != 0)
{
if (i == n)
printf("%dx^%d", arr[i], i);
else if (i == 1)
printf("+%dx", arr[i]);
else if (i == 0)
printf("+%d", arr[i]);
else
printf("+%dx^%d", arr[i], i);
}
}
getch();
return 0;
}
==========================================================
2) Write a ‘C’ program to accept n elements store those elements in array
and find and replace a given number.
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
int find_value(int arr[], int size, int num);
void displayArray(int arr[], int size);
int main()
{
int arr[20];
int n, f_value, rep_value,
result;
printf("Enter the size of
array : \n");
scanf("%d", &n);
printf("Enter the array
elements : \n");
for (int i = 0; i < n; i++)
{
printf("index[%d] :
", i);
scanf("%d",
&arr[i]);
}
displayArray(arr, n);
printf("\nEnter the find
value in the array : \n");
scanf("%d",
&f_value);
result = find_value(arr, n,
f_value);
if (result < 0)
{
printf("The element is
not found !!");
}
else
{
printf("The element in
found at %d index !!\n", result);
// replace value
printf("Enter the replace value :
\n");
scanf("%d",
&rep_value);
arr[result] = rep_value;
displayArray(arr, n);
}
getch();
return 0;
}
int find_value(int arr[], int size, int num)
{
int i;
for (i = 0; i < size; i++)
{
if (arr[i] == num)
{
return i;
}
}
return -1;
}
void displayArray(int arr[], int size)
{
printf("Array is :
\n");
for (int i = 0; i < size; i++)
{
printf("%d\t",
arr[i]);
}
}
===========================================================
3) Write a ‘C’ program to accept two polynomials and find the addition of
accepted polynomials.
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
int n;
int main()
{
printf("Enter the highest
power of the polynomial : ");
scanf("%d", &n);
int poly1[n], poly2[n],
result[n];
printf("Enter the First
Polynomial : \n");
for (int i = 0; i <= n; i++)
{
printf("Enter the
coefficient for x^%d : ", i);
scanf("%d",
&poly1[i]);
}
printf("Enter the Second
Polynomial : \n");
for (int i = 0; i <= n; i++)
{
printf("Enter the
coefficient for x^%d : ", i);
scanf("%d",
&poly2[i]);
}
printf("First Polynomial :
");
for (int i = n; i >= 0; i--)
{
if (poly1[i] != 0)
{
if (i == n)
printf("%dx^%d", poly1[i], i);
else if (i == 1)
printf("+%dx", poly1[i]);
else if (i == 0)
printf("+%d", poly1[i]);
else
printf("+%dx^%d", poly1[i], i);
}
}
printf("\n Second Polynomial
: ");
for (int i = n; i >= 0; i--)
{
if (poly2[i] != 0)
{
if (i == n)
printf("%dx^%d", poly2[i], i);
else if (i == 1)
printf("+%dx", poly2[i]);
else if (i == 0)
printf("+%d", poly2[i]);
else
printf("+%dx^%d", poly2[i], i);
}
}
printf("\n Addition of
Polynomial :");
for (int i = n; i >= 0; i--)
{
result[i] = poly1[i] + poly2[i];
if (result[i] != 0)
{
if (i == n)
printf("%dx^%d", result[i], i);
else if (i == 1)
printf("+%dx", result[i]);
else if (i == 0)
printf("+%d", result[i]);
else
printf("+%dx^%d", result[i], i);
}
}
getch();
return 0;
}
=======================================================================
Set C
1) Write a ‘C’ program to accept two polynomials and find the
Multiplication of accepted polynomials.
0 Comments