sequential or linear search using C

s

Search an element from the input array in a linear or sequential fashion.

If you are a computer science student or wants to start learning Data structure using practical hands-on then you are landing in the right place. This practice is part of the Data structure subject taught in GTU (Gujarat Technological University) Diploma course, but it is not limited to this. You can use this tutorial to have a better understanding of detail explanations. if you like to get more out of this practice then I highly recommend you to do code follow along is a best possible way. To know more about Author please visit here


Unit 1
Practical 3: Sequential or linear search example (for numbers only)

Problem statement:
User will input a list of numbers followed by the element they want to check within the entered list.

Solution :
step 1: Define Structure / logical collection to hold records about each student.
step 2: Take marks entry, name and roll number from the user.
step 3: Calculate Total Marks for each student.
step 4: Sort students records by total marks obtained (descending order)
or ranked students.


Step 1: Take a list of numbers from the user.

/*
* @Author: abdulkaiyum
* @Date:   2019-07-18 08:14:58
* @Last Modified by:   abdulkaiyum
* @Last Modified time: 2019-07-18 08:55:57
*/

// Sequencial search
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char const *argv[])
{

	int *inputArray,i;
	int count,key;


	printf("%s\n", "Please Enter number of Elements");
	scanf("%d",&count);

	inputArray = (int *) malloc(count * sizeof(int *));

	printf("%s\n", "Enter your numbers followed by enter key");

	for (i = 0; i < count; ++i)
	{
		scanf("%d",inputArray);
		inputArray++;
	}
	inputArray = inputArray - count;
		
	return 0;
}

Step 2: Take a number/element you want to search.

/*
* @Author: abdulkaiyum
* @Date:   2019-07-18 08:14:58
* @Last Modified by:   abdulkaiyum
* @Last Modified time: 2019-07-18 08:55:57
*/

// Sequencial search
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char const *argv[])
{

	int *inputArray,i;
	int count,key;


	printf("%s\n", "Please Enter number of Elements");
	scanf("%d",&count);

	inputArray = (int *) malloc(count * sizeof(int *));

	printf("%s\n", "Enter your numbers followed by enter key");

	for (i = 0; i < count; ++i)
	{
		scanf("%d",inputArray);
		inputArray++;
	}
	inputArray = inputArray - count;
	

	printf("%s\n", "Enter element you want to search in the array");
	scanf("%d",&key);
		
	return 0;
}

Step 3: Search number within the given list.

/*
* @Author: abdulkaiyum
* @Date:   2019-07-18 08:14:58
* @Last Modified by:   abdulkaiyum
* @Last Modified time: 2019-07-18 08:55:57
*/

// Sequencial search
#include <stdio.h>
#include <stdlib.h>


int seq_src(int *array,int arrayLength,int num)
{
	int i,isFound = 0;

	for (i = 0; i < arrayLength; ++i)
	{
		if(*array == num)
		{
			isFound = 1;
			break;
		}
		array++;
	}

	return isFound;
}

int main(int argc, char const *argv[])
{

	int *inputArray,i;
	int count,key;


	printf("%s\n", "Please Enter number of Elements");
	scanf("%d",&count);

	inputArray = (int *) malloc(count * sizeof(int *));

	printf("%s\n", "Enter your numbers followed by enter key");

	for (i = 0; i < count; ++i)
	{
		scanf("%d",inputArray);
		inputArray++;
	}
	inputArray = inputArray - count;
	

	printf("%s\n", "Enter element you want to search in the array");
	scanf("%d",&key);

	return 0;
}

Step 4: print the result.

/*
* @Author: abdulkaiyum
* @Date:   2019-07-18 08:14:58
* @Last Modified by:   abdulkaiyum
* @Last Modified time: 2019-07-18 08:55:57
*/

// Sequencial search
#include <stdio.h>
#include <stdlib.h>


int seq_src(int *array,int arrayLength,int num)
{
	int i,isFound = 0;

	for (i = 0; i < arrayLength; ++i)
	{
		if(*array == num)
		{
			isFound = 1;
			break;
		}
		array++;
	}

	return isFound;
}

int main(int argc, char const *argv[])
{

	int *inputArray,i;
	int count,key;


	printf("%s\n", "Please Enter number of Elements");
	scanf("%d",&count);

	inputArray = (int *) malloc(count * sizeof(int *));

	printf("%s\n", "Enter your numbers followed by enter key");

	for (i = 0; i < count; ++i)
	{
		scanf("%d",inputArray);
		inputArray++;
	}
	inputArray = inputArray - count;
	

	printf("%s\n", "Enter element you want to search in the array");
	scanf("%d",&key);

	if(seq_src(inputArray,count,key) == 1)
	{
		printf("%s\n", "element found");
	}
	else
	{
		printf("%s\n", "element not found");
	}
		
	return 0;
}
/*
* @Author: abdulkaiyum
* @Date:   2019-07-18 08:14:58
* @Last Modified by:   abdulkaiyum
* @Last Modified time: 2019-07-18 08:45:06
*/

// Sequencial search
#include <stdio.h>
#include <stdlib.h>


int seq_src(int *array,int arrayLength,int num)
{
	int i,isFound = 0;

	for (i = 0; i < arrayLength; ++i)
	{
		if(*array == num)
		{
			isFound = 1;
			break;
		}
		array++;
	}

	return isFound;
}

int main(int argc, char const *argv[])
{

	int *inputArray,i;
	int count,key;

	printf("%s\n", "Please Enter number of Elements");
	scanf("%d",&count);

	inputArray = (int *) malloc(count * sizeof(int *));

	printf("%s\n", "Enter your numbers followed by enter key");

	for (i = 0; i < count; ++i)
	{
		scanf("%d",inputArray);
		inputArray++;
	}
	inputArray = inputArray - count;
	

	printf("%s\n", "Enter element you want to search in the array");
	scanf("%d",&key);

	if(seq_src(inputArray,count,key) == 1)
	{
		printf("%s\n", "element found");
	}
	else
	{
		printf("%s\n", "element not found");
	}

	return 0;
}


Add comment

Recent Posts

Recent Comments

Archives

Categories

Email Subscriber