-->





Translate

Showing posts with label binary. Show all posts
Showing posts with label binary. Show all posts

Stream types and file makers bof eof and newline c programming tutorial

Leave a Comment
This C programming tutorial covers two topics

  1. What is stream and its types in c programming
  2. How to use file makers EOF, BOF and newline 


What is a stream in  c programming? Also discuss its different types.
File:
The data is stored in the form of file
A file is a set of related records.
Stream:

  • It is a logical interface to a file.
  • It is associated to a file using an open operation.
  • It is not associated to a file using a close operation.
  • Stream refers to flow of data from source is know as reading ,fetching, getting or extracting the data.
  • The process of producing output data to the destination is know as writing, storing, inserting or putting the data.

There are two types of streams.

  1. Text stream
  2. Binary stream

Text stream:

  • A text stream is a sequence of characters.
  • Character translation may occur in a text stream e.g. new line is represented as carriage return.
  • There may not be one to relation between the characters written and those on the external device.

Binary Stream:

  • It is a sequence of bytes.
  • The number of bytes written or read is always the same as those on the external device.it means there is one to one relation between the bytes written or read and those on the external device.
  • No character translations occur in binary stream.
  • Some additional bytes such as the file sector on the disk are added to to the binary stream.


What are file markers? Explain the use of BOF, EOF and New Line in c programming.

  • A file contains a numbers of characters or bytes each file stored on a disk has a start and an end.
  • The start of first byte or character is called beginning of file(BOF).
  • The end of last byte or character is called End of file (EOF).
  • The position where data read or data write operations are performed is called current position.
  • EOF character are placed after the last character i C.
  • Enter key is used in text editor for new line but in C new line character is \n to be placed at the of each line.
Read More...

Simple Binary search code example in cpp

3 comments
Write a cpp program which takes some elements in an array and a Key in variable then program use Binary Search c++ Algorithm to find the key.
Concept used:
Functions, loop, and if-else statements in c++

Program Explanation:
Program has two functions


  • One to sort array using bubble sort
  • Second to apply Binary Search on array
  • As it is necessary condition to sort the array before applying binary search in main after taking some elements as input in array. 
  • Array passes to bubble sort function to sort the array. 
  • After sorting array passes to Binary Search function which is of 'bool' type if element found in array it returns true else false to main function.

    C++ code for Binary Search

#include<iostream>
 using namespace std;

  // Prototypes of Functions
  void bubbleSort(int array[], int size);
  bool binarySearch(int array[], int size,int key);

  int main(){
      cout<<"Enter 5 numbers randomly : "<<endl;

      // Size can be change by replacing 5
      int array[5]; //Declaring array
     for(int i=0; i<5; i++)
      {
       cout<<"\t";  cin>>array[i]; // Initializing array
      }

      //Passing Arrary for Sorting
       bubbleSort(array,5);

    // Array has Sorted At This Point
    cout<<"\n\t\t\tEnter Key To Search: ";
    int key;
    cin>>key;

//Passing Array, size and key To Search Key
int result=binarySearch(array,5,key);

if(result==1)
cout<<"\n\t\t\tKey Found in Array "<<endl;
else
cout<<"\n\t\t\tKey NOT Found in Array "<<endl;


return 0;
}

void bubbleSort(int array[], int size){
      cout<<"  Input array is: "<<endl;
      for(int j=0; j<size; j++)
      {
       //Displaying Array
       cout<<"\t\t\tValue at "<<j<<" Index: "<<array[j]<<endl;
      }
      cout<<endl;
    // Bubble Sort Starts Here
     int temp;
     for(int i2=0; i2<size; i2++)
   {
     for(int j=0; j<size-1; j++)
     {
        //Swapping element in if statement
           if(array[j]>array[j+1])
       {
        temp=array[j];
        array[j]=array[j+1];
        array[j+1]=temp;
       }
     }
   }
   // Displaying Sorted array
      cout<<"  Sorted Array is: "<<endl;
     for(int i3=0; i3<size; i3++)
   {
    cout<<"\t\t\tValue at "<<i3<<" Index: "<<array[i3]<<endl;
   }
}// Sort Function Ends Here

bool binarySearch(int array[],int size, int key){
         int start=1, end=size;
         int mid=(start+end)/2;

  while(start<=end&&array[mid]!=key){
        if(array[mid]<key){
          start=mid+1;
      }
     else{
          end=mid-1;
          }
       mid=(start+end)/2;
     }// While Loop End

   if(array[mid]==key)
    return true; //Returnig to main
    else
   return false;//Returnig to main

   cout<<"\n\n\n";
}// binarySearch Function Ends Here




Sample Input Output:
Binary search code example in c++ programming tutorial

Recommended: For beginners to keep the program simple array size is small. Change the array size dry run the code on paper it will be very helpful to understand quick.
Another method to of searching






  • Linear search in C++ programming code Example

  • Recursive function in c++ linear search 
  • Read More...