Thursday, 31 August 2017

Prepared By: Niraj Bhagchandani
Email: niraj.bhagchandani@live.com
Set Theory: Counting Element from a set
IN C LANGUAGE
Introduction:
  1. Take the input from the user.
  2. Calling count_element function and counting the elements one by one
  3. After the counting process, we print it.
imageedit_1_9127618562.png
Image Courtesy: ntu.edu.sg
---Program: ---
#include <stdio.h>
#define SIZE 10
void count_elements(int a[]){
   int i;
   /*Processing error for counting all the elements in an array */
   for(i=0;a[i];i++);
   /* Printing the total elements in an array */
   printf("The Total Elements are: %d", i);
}
int main(){
   int i, element;
   int a[SIZE]={'\0'}; /*={4,6,8,10,11,13,15,'\0'};*/
   /*Enter The total size for input of array a[] */
   printf("\nEnter element size <%d: ", SIZE);
   scanf("%d", &element);
   /* Program for input from user and storing to array begins */
   printf("\nEnter the value for array: \n");
   for(i=0;i<element;i++){
       printf("a[%d]: ", i);
       scanf("%d", &a[i]);
   }
   /* Program for input from user and storing to array ends */
   /* Calling the count_element function to perform all the operations */
   count_elements(a);

   return 0;
}
---Output:---


Enter element size <10: 5

Enter the value for array:
a[0]: 2
a[1]: 3
a[2]: 4
a[3]: 5
a[4]: 6
The Total Elements are: 5
Process returned 0 (0x0)   execution time : 9.077 s
Press any key to continue.


About This Article:
To cite this article add this reference and give credits to author.
  1. Bhagchandani, Niraj. “Set Theory: P6 Counting Element from a Set in C Language.”Edatastructure.blogspot.in, Blogger, 31 Aug. 2017, edatastructure.blogspot.in/2017/08/set-theory-p6-counting-element-from-set.html.
Prepared By: Niraj Bhagchandani
Email: niraj.bhagchandani@live.com
Set Theory: Searching Element from a set
IN C LANGUAGE
Introduction:
In computer science, linear search or sequential search is a method for finding a target value within a list. It sequentially checks each element of the list for the target value until a match is found or until all the elements have been searched.[1]
Linear search runs in at worst linear time and makes at most n comparisons, where n is the length of the list. If each element is equally likely to be searched, then linear search has an average case of n/2 comparisons, but the average case can be affected if the search probabilities for each element vary. Linear search is rarely practical because other search algorithms and schemes, such as the binary search algorithm and hash tables, allow significantly faster searching for all but short lists[1]
  1. Take the input from the user and trace it one by one in the array given below.
  2. If found then print the message accordingly along with index and position.
  3. If not found then print the message “Element not found”.


cpp-array.png
Image Courtesy: Tutorial4us.com
---Program: ---
#include <stdio.h>
#define SIZE 20
int search_elements(int a[], int search){
   int i,flag=0;
/* Searching algorithm begins from here*/
   for(i=0;a[i];i++){
       if(search == a[i]){
           /* Printing the message if the element is found and turning on the flag bit */
           printf("\nThe value is %d position is %d index is %d",a[i],i+1,i );
           flag=1;
       }
   }
   /* Printing the message if the element is not found. */
   if(flag==0){
       printf("\nElement Not Found");
   }
   /* Searching algorithm ends from here*/
}
int main(){
   int i, search, element;
   int a[SIZE]={'\0'}; /*={4,6,8,10,11,13,15,'\0'};*/
   /*Enter The total size for input of array a[] */
   printf("\nEnter element size <%d: ", SIZE);
   scanf("%d", &element);
   /* Program for input from user and storing to array begins */
   printf("\n Enter the value for array: ");
   for(i=0;i<element;i++){
       printf("a[%d]: ", i);
       scanf("%d", &a[i]);
   }
   /* Program for input from user and storing to array ends */
   /* Taking input from user to search an element from array a[] */
   printf("\nEnter the value to search: ");
   scanf("%d", &search);
   /* Calling The search_element function to perform all the search operations */
   search_elements(a,search);
   return 0;
}
---Output:---


Enter element size <20: 7

Enter the value for array: a[0]: 4
a[1]: 6
a[2]: 8
a[3]: 10
a[4]: 11
a[5]: 13
a[6]: 15

Enter the value to search: 10

The value is 10 position is 4 index is 3
Process returned 0 (0x0)   execution time : 25.891 s
Press any key to continue.
References.
  1. “Linear Search.” Wikipedia, Wikimedia Foundation, 18 Aug. 2017, en.wikipedia.org/wiki/Linear_search#CITEREFKnuth1998.
About This Article:
To cite this article add this reference and give credits to author.

  1. Bhagchandani, Niraj. “Set Theory: P5 Searching Element from a Set in C Language.”Edatastructure.blogspot.in, Blogger, 31 Aug. 2017, edatastructure.blogspot.in/2017/08/set-theory-p5-searching-element-from.html.