C++ gurus and learners, we program ourselves :P all work is CC licensed

Friday, November 8

Simple void function use by Usman Sherry

                Use of a Simple Void Function 
 
#include<iostream>
using namespace std;

void function(); ////void type function

int main()
{
function(); //// not necesserely use inside cout statment
cout<<endl; //// as it is a display. Align it properly if needed
return 0;
}

void function() //// returns nothing
{
cout<<"This line would be simpaly displayed if this function is called";
} //// no return used

//////////// you can also use loop for a display i.e. triangle or diamond and function will just print on screen ///////////////////////////

Best Explanation of Functions by Usman Sherry

Some basic understanding to use functions:


          First of all we have to look for output of functions as we want
  • void
  • int
  • double
    • and so for other types. The major difference is between void and non-void (integral, character and float) outputs.

Void: (with no arguments)

------------------------------------------------------------------------------------------------------------
  1. #include<iostream>
  2. using namespace std;
  3. void function();                                 //with or without arguments dose not effect it's output type
  4. void main()
  5. {
  6.           function();
  7. void function()
  8. {
  9.            cout<<"To display something "<<endl;
  10. }                                                         // void type returns no value with or without argument
------------------------------------------------------------------------------------------------------------
Output will be:
To display something                                                                               Press any key to continue . . .                                                                   
------------------------------------------------------------------------------------------------------------

Void: (with arguments)
------------------------------------------------------------------------------------------------------------
  1. #include<iostream>
  2. using namespace std;
  3. void function(int);                 //with or without arguments dose not effect it's output type
  4. void main()
  5. {
  6.           int y;
  7.           cin>>y;
  8.           function(y);
  9. void function(int y)
  10. {
  11.            cout<<"Square is :"<<y*y<<endl;
  12. }                                                // void type returns no value with or without argument
------------------------------------------------------------------------------------------------------------
Output will be: (let y=5)
Square is :25                                                                                           Press any key to continue . . .                                                                   
_________________________________________________________________
------------------------------------------------------------------------------------------------------------

Other types of outputs:

without argument

------------------------------------------------------------------------------------------------------------
  1. #include<iostream>
  2. using namespace std;
  3. int function();         //with or without arguments dose not matter it's output type
  4. void main()
  5. {
  6.        cout<<function()<<endl;
  7. int function()
  8. {
  9.  return 5*99;
  10. }                       // must return a value 
------------------------------------------------------------------------------------------------------------
Output will be:
495                                                                                                       Press any key to continue . . .                                                                   

------------------------------------------------------------------------------------------------------------

with argument

------------------------------------------------------------------------------------------------------------
  1. #include<iostream>
  2. using namespace std;
  3. int function(int,int);    //with or without arguments dose not matter it's output type
  4. void main()
  5. {
  6.        int x,y;
  7.        cin>>x>>y;
  8.        cout<<function(x,y)<<endl;
  9. int function(int x,int y)
  10. {
  11.   return y*x;
  12. }                         // must return a value 
------------------------------------------------------------------------------------------------------------
Output will be: (x=5 / y=3)
5                                                                                                           
3                                                                                                           
15                                                                                                         Press any key to continue . . .                                                                   

------------------------------------------------------------------------------------------------------------

Thursday, November 7

Pointer to array Declaration and Definition

20:29 By

Many of you has question in your mind that how to declare and define Pointer to Array in C++.

I 'll show how?



#include <iostream>

#include <conio.h>




using namespace std;




void main()

{

//int pointer to array declaration.

int *pointer_array[10];

//defining pointer to array.

for(int i=0; i<100; i++)

{

pointer_array[i] = new int[5];

for (int j=0; j<5; j++)

pointer_array[i][j] = -1;

}

getch();

}

Array Sorting with Selection Sort

20:22 By

In computer scienceselection sort is a sorting algorithm, specifically an in-place comparison sort. It has O(n2) time complexity.
The algorithm divides the input list into two parts: the sublist of items already sorted, which is built up from left to right at the front (left) of the list, and the sublist of items remaining to be sorted that occupy the rest of the list. Initially, the sorted sublist is empty and the unsorted sublist is the entire input list. The algorithm proceeds by finding the smallest (or largest, depending on sorting order) element in the unsorted sublist, exchanging it with the leftmost unsorted element (putting it in sorted order), and moving the sublist boundaries one element to the right.
For more detail use Wiki... Selection Sort

#include <iostream>
#incl
ude <conio.h>
using namespace std;//Selection Sort Function

void Ssort(int *a,int length)
{

int temp;
for(int i=length-1;i>0;i--)
{
int first=0;
for(int j=1;j<=i;j++)
if(a[j]>a[first])first=j; temp=a[first];a[first]=a[i];a[i]=temp;}
}

void main()
{

int arr[10];
int temp[10];
arr[0]=100;
arr[1]=33;
arr[2]=43;
arr[3]=26;
arr[4]=46;
arr[5]=88;
arr[6]=52;
arr[7]=17;
arr[8]=53;
arr[9]=77;

for(int i=0;i<10;i++)
{
temp[i]=arr[i];
cout<<arr[i]<<",";
}

//calling selection sort function
Ssort(temp,10);

//Displaying the result.
cout<<endl<<endl;

for(int i=0;i<10;i++)
cout<<temp[i]<<",";
getch();
}

Smallest of integers alternate approach

19:48 By

//code by http://CppQA.blogspot.com
//we are community of developers focusing on help for new programmers
#include <iostream>
using namespace std;
#define CHAR_BIT 8

/*Function to find minimum of x and y*/
int min(int x, int y)
{
  return  x + ((y - x) & ((y - x) >>
            (sizeof(int) * CHAR_BIT - 1)));
}

/* Function to find minimum of 3 numbers x, y and z*/
int smallest(int x, int y, int z)
{
    return min(x, min(y, z));
}

int main()
{
   int x = 1, y = 100, z = 90;
   cout << "Minimum of 3 numbers is " << smallest(x, y, z) << endl<<"http://CppQA.blogspot.com"<<endl;
   return 0;
}

you can fork this code on ideone.com

Wednesday, November 6

Swapping without use of third variable

Swapping means to interchange values of usually two variables and is done using third temp variable e.g.
int var1 = 10, var2 = 20;
int temp = var2;
var2 = var1;
var1 = temp;
//now var1 = 20 and var2 = 10
//hence values have been swapped

But take a look at code swapping without use of third variable, this trick will come handy in future assignments

int var1 = 10, var2 = 20;
var1 = var1 * var2;
var2 = var1 / var2;
var1 = var1 / var2;
//now var1 = 20 and var2 = 10
//hence values have been swapped

For the person who can explain this code to me and post a solution using (^) operator, there is a GENUINE MICROSOFT VISUAL STUDIO KEY ;)
You can post a request for guest post in comments, anywhere

Smallest Integer Without Using Comparison Operator


#include <iostream>
using namespace std;
#define CHAR_BIT 8

/*Function to find minimum of x and y*/
int min(int x, int y)
{
  return  y + ((x - y) & ((x - y) >>
            (sizeof(int) * CHAR_BIT - 1)));
}

/* Function to find minimum of 3 numbers x, y and z*/
int smallest(int x, int y, int z)
{
    return min(x, min(y, z));
}

int main()
{
   int x = 12, y = 15, z = 5;
   cout << "Minimum of 3 numbers is " << smallest(x, y, z) << endl;
   return 0;
}

Special Diamonds Continued -- Usman Sherry

21:29 By

(Continued From Special Diamonds Part 1) Part 2; Here will use 2 loops inside one loop so that it is easy to print shape in less loops and coding; It is simple because the loop we used for triangle in first page is going to be repeated in this loop only we will change the size alignment of shapes upside down;


int i,j,k,rows,size1,size2;
cin<<rows;
size1      [as described before you have to choose sequence of shapes so ]
size2      [there are two options size1=1 and size2=rows /// or size2=rows and size1=1]
do
{           j=size1;
do          
{
cout<<”required character”;
j--;
} while (j < size);
size1 ?? ;                                              //here you define corresponding proceeding size
                k=size2;
do          
{
cout<<”required character”;
k--;
} while (j < size);
size2 ?? ;                                              //here you define corresponding proceeding size
}
while(i<rows);

I hope you have got the idea
do{                                                         //the main loop
                do{                                         //corresponding first shape
}while
do{                                         //corresponding second shape
}while
}while
And in case of diamond you have to detect central row and print same size pyramid upside and then same size pyramid downside
To detect central row è              total rows / 2 = central row -1                     //do not use this in code
i.e. central row=(total rows/2 +1);            //use this in code to detect central row
in this way you can print first part then print single row of size of central row then print second part so that the both main loops will have same size and almost same coding;
first main loop                                                  
central row
last main loop

Special Diamonds Program Set

21:22 By

A post by Usman of BESE 19 at MCS.



The simple program logic for Diamond printing program:

1.       We could simply use four nested loop sets for printing this shape as in part 1;
but it would take about 8 loops for printing a sum of 4 shapes structure [ in diamond 4 triangles are summed up to make its structure ]
so to make it a bit simple [ 4 + 2 loop in which 4 loops are for triangles and 2 loops join them ] we divide it into 2 parts instead of 4 like in part 1, this is described in part 2;
2.       Part 2 has simple loop nesting by dividing main shape into 2 parts as Upper half and Lower half;

Part 1;

This will help you understanding loops for printing shape’s basic structure which is triangle in this topic;
a.       Make simple triangle of 2 loops as a set is very basic;

int i,j,rows,size;
cin>>rows;     //take input
size = rows;     //give the initial size of shape which is decreasing
size = 1;      //if shape is increasing then size is 1
do      //it will run row’s * time
{ j=size;      //it will change size of rows constantly
           do     //this loop is always as this format whatever shape is decreasing or increasing as it has to print a line of given width anywhere required
 {
                        cout<<”required character”; // do not put endl here as it is a continues line
                               j--;
               } while (j < size);
size--;     //if size of shape is decreasing downwards
size++;    //if size of shape is increasing downwards
cout<<endl; 
}while (i<given rows);

Please visit next part at Special Diamonds Part 2