Write a C only not C Write a C program that creates an integ
(Write a C only, not C++)
Write a C program that creates an integer array with M elements (M is read from the user). The array elements should be generated from the following equation: a[i] = 4*i+1, in that a[i] is the element with the subscript i in the array. The program will print the original array with 3 decimal places per value. Then, the program should read a value X from the user. Using linear search to find if the value X does exist in the array or not. If yes, print the position where we find X, otherwise, print \"X is not found in the given array\". Using functions. Array size: 6 Original array: 1 5 9 13 17 21 (first run) Value X: 17 X is found at position 5. (another run) Value X: 20 X is not found in the given array.Solution
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
void print(int a[],int n)
{
int i;
printf(\"Printing Array \ \");
for( i=0;i<n;i++)
{
printf(\"%d\\t\",a[i]);
}
printf(\"\ \");
}
int search(int a[],int n,int x)
{
int i;
for( i=0;i<n;i++)
{
if(a[i] ==x)
return i;
}
return -1;
}
int main(void)
{
int M,i=0,X;
printf(\"Enter the num of elements in array\ \");
scanf(\"%d\",&M);
int a[M];
for(i=0;i<M;i++)
a[i]=(4*i)+1;
print(a,M);
printf(\"Enter the element to search in array\ \");
scanf(\"%d\",&X);
printf(\"Searching for %d in the Array (first run)\ \",X);
if(search(a,M,X) == -1)
printf(\"%d is not found in the given array\ \",X);
else
printf(\"%d is found at position %d\ \",X,search(a,M,X));
printf(\"Enter the element to search in array\ \");
scanf(\"%d\",&X);
printf(\"Searching for %d in the Array (second run)\ \",X);
if(search(a,M,X) == -1)
printf(\"%d is not found in the given array\ \",X);
else
printf(\"%d is found at position %d\ \",X,search(a,M,X));
return 0;
}

