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
00:05
By
Usman Shery
c++
,
C++ Functions Explained
,
usman sherry
,
void functions c++ use
3
comments
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)
------------------------------------------------------------------------------------------------------------
- #include<iostream>
- using namespace std;
- void function(); //with or without arguments dose not effect it's output type
- void main()
- {
- function();
- }
- void function()
- {
- cout<<"To display something "<<endl;
- } // void type returns no value with or without argument
------------------------------------------------------------------------------------------------------------
Output will be:
To display something Press any key to continue . . .
------------------------------------------------------------------------------------------------------------
Void: (with arguments)
------------------------------------------------------------------------------------------------------------
- #include<iostream>
- using namespace std;
- void function(int); //with or without arguments dose not effect it's output type
- void main()
- {
- int y;
- cin>>y;
- function(y);
- }
- void function(int y)
- {
- cout<<"Square is :"<<y*y<<endl;
- } // 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
------------------------------------------------------------------------------------------------------------
- #include<iostream>
- using namespace std;
- int function(); //with or without arguments dose not matter it's output type
- void main()
- {
- cout<<function()<<endl;
- }
- int function()
- {
- return 5*99;
- } // must return a value
------------------------------------------------------------------------------------------------------------
Output will be:
495 Press any key to continue . . .
------------------------------------------------------------------------------------------------------------
with argument
------------------------------------------------------------------------------------------------------------
- #include<iostream>
- using namespace std;
- int function(int,int); //with or without arguments dose not matter it's output type
- void main()
- {
- int x,y;
- cin>>x>>y;
- cout<<function(x,y)<<endl;
- }
- int function(int x,int y)
- {
- return y*x;
- } // 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
Many of you has question in your mind that how to declare and define Pointer to Array in C++.
I 'll show how?
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
In computer science, selection 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
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
//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
23:14
By
Unknown
swapping in three steps
,
swapping using multiplication operator
,
swapping variables without another variable
8
comments
Swapping means to interchange values of usually two variables and is done using third temp variable e.g.
But take a look at code swapping without use of third variable, this trick will come handy in future assignments
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
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
(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 sizek=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 ideado{ //the main loopdo{ //corresponding first shape}whiledo{ //corresponding second shape}while}whileAnd in case of diamond you have to detect central row and print same size pyramid upside and then same size pyramid downsideTo detect central row è total rows / 2 = central row -1 //do not use this in codei.e. central row=(total rows/2 +1); //use this in code to detect central rowin 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 loopcentral rowlast main loop
Special Diamonds Program Set
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












