Saturday, 28 October 2017

Binary Search 


Let's understand without going into the details.
Let's say we have a sorted array, say in ascending order like:
1, 4, 10, 13, 40, 65, 100.

Now let's say I searched for 10. The program will execute and it will look at the middle term and will compare it with our number. If the match is found then it will show the position of array. And if it is smaller than the middle term then it will look for the numbers before the middle term, and this will go on until the number is found. If not found, then we will display: "Not found".

Program:


void main()
{
clrscr();
int n, i, arr[20], search, first, last, middle;
cout<<"Enter total number of elements :";
cin>>n;
cout<<"Enter "<<n<<" number :";
for (i=0; i<n; i++)
{
cin>>arr[i];
}
cout<<"Enter a number to find :";
cin>>search;
first = 0;
last = n-1;
middle = (first+last)/2;
while (first <= last)
{
if(arr[middle] < search)
{
first = middle + 1;

}
else if(arr[middle] == search)
{
cout<<search<<" found at location "<<middle+1<<"\n";
break;
}
else
{
last = middle - 1;
}
middle = (first + last)/2;
}
if(first > last)
{
cout<<"Not found! "<<search<<" is not present in the list.";
}
getch();
}

Monday, 11 September 2017

Linear Search. 



One of the easiest method for searching the element in an array.

Structure:

int Array[10], x; //x is variable for search 
for (int i =0 ; i<10; i++) //Initalize, condition, increment 
{
cin>>Array[i];

cout<<"Enter the element to Search: ";
cin>>x;
for (int i =0 ; i<10; i++) //Initalize, condition, increment 
{
if(Array[i] == x){ cout<<"Number found at position" <<i+1;  break;}
}

Tuesday, 25 July 2017

Handling the Patterns in C++


___________________________________________________________
These are often asked and the funny thing is we still don't pay much attention to this.
___________________________________________________________
Consider the Example:
*
**
***
****

Now , for a beginner it's a little bit challenging.

There are two ways to do this:
1. Just print everything with "COUT"
2. Using Loops(Your Logic)
________________________________________________________

The first can be done by anybody so lets start with second One.

for(int i=  0; i<=5 ;i++)
{
for(int j=0; j<i; j++)
{
cout<<"*";
}
cout<<"\n";
}
________________________________________________________

Explanation: 

This works from outer to inner. When i was 0, condition was true, loop goes inside, now j=0 and second condition is false so the loop comes outside and prints "\n"(A new Line).
Now the value of i=1, condition is true, loop goes inside, j=0, and the condition is true, so inner loop runs and prints "*".
Now the inner loop runs one more time and the j becomes 1 and now the condition is false, so inner loop terminates. back to the outer loop.
Like this the loop will go on until the i becomes greater than 5 and you will obtain an output. 

Tuesday, 20 June 2017

Easiest way to use onClick method in Android. 

It can be done by using the XML. Just define a Button with 'id' and and 'onClick'.

Like: 
<Button 
android: height = "wrap_content"
android: height = "wrap_content"
android: text = "Click it"
android: id = "@id/click_button_1"
android: onClick = "Do_the_Click"
/>

So when you will define the onClick, there will be a suggestion on the left side of the statement. Click on that, it would suggest to make a method in java file with name "Do_the_Click", Click on it and it will create it.

And then you can write your code in that method. 

Sunday, 11 June 2017

How to import or use any defined thing in the MainActivity.java File?

The simple Answer is from Resources.
Now, What are resources and how to use Resources?

The Most simple and Standard way is to create a Variable and use that to get the other resources from other Files like Strings.xml.
_______________________________________________________________________________
Consider that had defined a String-Array in Strings.xml file and want to import that array into you ListView.

Step 1. Create a Listview in MainActivty.xml(Your main Layout file) and define ID.

Step 2. Define you string -array in Strings.xml file(It's located in Values directory) and give a name say "Languages".

Step 3. Go to MainActivity.java file and type following code

ListView list = (ListView) findViewById(R.id.list_items);

Resources resources = getResources();
String[] languages = resources.getStringArray(R.array.Languages);

ArrayAdapter<String> adapter =  new ArrayAdapter<String>(this, android.R.layout.Simple_list_1, languages); //I don't exactly know the ListView drop-down options
listview.setAdapter(adapter);

Step 4.  Run the Emulator.

Saturday, 27 May 2017

BOXING and UNBOXING (Java)

---------------------------------------------------------------------------------------------------------------------
Well it's not what you see on the YouTube rather than its a Topic when you read about the Wrapper Classes.

Now What are Wrapper Classes?
So the Answer is simple, you have heard about the Primitive data types. So during development it was difficult to use the Primitive data types because if they had used Primitive data types then there was a possibility that their files would have altered the data. This created a need for Objects to use instead of the directly using them.
Now, the Objects accept the properties of the primitive data types. this is why we need Wrapper classes. 
Wrapper classes are the Subclass of the Number class. 
Diagram of Number Class and its subclasses
_______________________________________________________________________________

And now comes the Boxing and Unboxing.

So, Converting primitive data types into object is called boxing and the vice versa is called Unboxing. They are done by Compiler so the User doesn't have to do much.

Tuesday, 16 May 2017

What will be the next Update in Android after Nougat?


Nougat was released on 9 March 2016(The alpha version) and then officially on August 22, 2016. We know some of the important features that we got from this Update.
 Whether, it's Split Screen or Notifications redesigning(You can also set the priorities of the Notifications).

So the Next 'Android O' will be coming soon. And there are many guessing about the name going on with "Oreo, Orange, etc." It's pretty much possible that the name will be Android Oreo.

But the name doesn't matter as the user will judge it on the basis of its performance and appearance. We already know some of the features like the picture-in-picture multitasking and some new emoji s introduced.

Updates available for the Device:

  • Nexus 5P, Nexus 6P, Nexus Player
  • Pixel C, Pixel, Pixel XL

It's Beta version is out there and you can download it from Google but it's going through a developing stage so it will contain Bugs for sure. So download at your own risk. It will be officially released between August to September 2017.