Friday 25 October 2013

Finding largest two numbers from N numbers

Finding largest two numbers from N numbers:


 This C program example finds the two largest numbers from the list of 'N' numbers.

Mechanism:
In this program, we have declared the variable of:
  • Size
  • Input
  • First largest
  • Second largest
 First, we take input of size and then we enter a for loop which executes for 'size' times. This program is designed in such a way that it checks all negative and positive integers. This program uses an if else and else if structure and for loop structure structure to execute the logic of the program. This program also works on boundary lines. Time complexity of this program is O(size). Once the loop terminates, it prints the first largest and second largest value from the N numbers.

Code:

/*              Join us at
http://shapesinc.blogspot.com 
for more codes of shapes                            
*/
#include<iostream>
using namespace std;
int main()
{
cout << "Join http://shapesinc.blogspot.com for more codes of shapes\n\n";
cout<<"CAUTION:   The input should not be other than numerical values \n\n\a";
int i, num, largest = 0, seclargest = 0, size; cout << "Enter the amount of numbers you want to enter\n"; cin>>size; if (size<=0) { cout << "No values entered\n"; goto finish; } cout << "Please enter the numbers from the keyboard.\n" << endl; for (int i=0; i<size; i++ ) { cin >> num; if(i==0) largest=num; else if(i==1) { if(num>largest) { seclargest = largest; largest = num; } else seclargest=num; } else if ( num > largest ) { seclargest = largest; largest = num; } else if ( num > seclargest ) seclargest = num; } cout << "\nThe largest of the numbers entered is " << largest << endl ; cout << "\nThe second largest of the numbers entered is " << seclargest << endl ; finish: return 0; }

Final Words:
By this post, you can learn C online practically. We have tried our best to make this program understandable and optimized.

Thursday 17 October 2013

Do While In C++

Do While C:


In this C tutorial for beginners, we are going to discuss the third repetition structure known as do…while loop in c. In for loop, initially the loop condition is checked and then the statement is executed according to the loop condition. But if you want to execute the statement initially(first) and then check the loop condition then we use do…while loop.

Syntax:

do
       statement;
while (expression);



Do while loop in C++



Statement can be either a simple or compound statement. If it is a compound, enclose it between braces. In C++, do and while are reserved keywords.

Mechanism:

The statement executes first and then the expression is evaluated. If the expression is true then statement executes again else the loop will terminate.
A popular use of c do…while loop is that it can be used for input validation. Suppose that a user prompts to enter a test score which must be greater or equal to zero and less than fifty. SO, entries other than that should be invalid. For this we use do…while loop in c.

e.g:

int score;
do
{
    cout << “Enter a score between 0 and 50\n”;
    cin>>score;
    cout<<endl;
}
while (score < 0 || score > 50);

Final words:

In this c tutorial for beginners we discussed do while loop in detail. This is an important structure frequently used in C++. 
You can study and easily understand it here.


For Loop In C++

For Loop In C++:


The while loop discussed in our previous tutorial is a general form which can be implemented in a different form of repetitions. In this C tutorial for beginners, we are going to study second repetition structure known as for loop c. In C++, for loop is a specialized form of while loop. Its primary purpose is to simplify the writing of counter-controlled loops.
In while loop, you had to declare a counter variable and write an extra code to increment the counter in a while loop and the length continues as you increase the number of counters. But in for loop, you can do this all in just one line and that’s the reason why for loop is called a counted or indexed for loop.

Syntax:

for ( initial statement; loop condition; update statement)
      statement;



For loop in C++


in C++, for is a reserved keyword. The initial statement, loop condition and update statement enclosed in parenthesis control the body of the for loop statement.

The following are some common points regarding for loop:

  1. If the loop condition is initially false then the loop body doesn’t execute.
  2. The update statement alter the value of loop control variable which ultimately sets the loop condition to false until or unless the update statement is converging towards loop condition.
  3. C++ allows you to fractional values for loop control variables of the double type.
  4. A semicolon at the end of the for statement would make it an infinite loop
                     e.g: for ( int i=0; i<10; i++);
  5. In the for loop, if the loop condition is omitted then it is assumed to be true.
  6. If we omit all three statements in for loop then it would be legal but it would be an infinite for loop.
                    e.g: for( ; ; )
                                cout << “Hello World\n”;

Example:

for(int i=0;i<10;i++)
      cout<<i<<"  ";

this program will print numbers from 0 to 9 on console.It is a common example of c for loop.

Final words:

For loop is the most frequently used loop in programming languages.
It has been explained with detail  in this c tutorial for beginners.

While Loop In C++

While Loop In C++:

In C++, there are times when it becomes necessary to repeat a set of statements several times. One way is to write the code in the program over and over but that would make the code very long and it would become a messy code. Just imagine that if you have to repeat a set of statements 100 times then you have to write that 100 times again and imagine where the length of the code would go.
Fortunately, we have a way that finishes all this fuss and that is repetition structures. There are three repetition structures but in this C tutorial for beginners, we are going to study c while loop.

General form:

while (expression)
    statement




while loop in C++


In C++, while is a reserved keyword. The statement is a simple or compound statement. The expression acts as a decision maker and it is usually a logical expression. The body of the c while loop is between parenthesis.
The expression provides an entry condition. If the condition is true then the program enters into the while loop else the loop doesn’t execute. If the value is true then the while loop c will keep on going until the expression value becomes false or any break statement executes. But if the expression value never changes into false and there is no break statement then the while loop becomes an infinite loop. Infinite loop means that it will keep on executing.

Designing while loops:

There are many ways to design a while loop in c. Below are some cases that gives us the idea of the usefulness of this structure.

Counter-controlled while loops in c:

Suppose that we want to execute a simple or compound statement known number of times then we used an int variable which acts as counter and initialize it with zero and increment its value till the number of times we want to execute the statement. 

Below is an example:

int counter=0;
while(counter < size)            //size is the number of times 

                                           //we want to executes this while 
                                           //loop
    {
        .
        .
        counter++;
        .
        .   
    }

Sentinel-controlled while loop in c:

Sometimes, we don’t know that how many pieces of data need to be read but you know that there is a value on which this loop will terminate. Such special value is called sentinel. In this case, you read one value before the loop and then while loop will execute. 

Below is the example of this type of loop:


cin>>variable;                //taking the input before the loop
while (variable != sentinel)        //test the loop control //variable and sentinel is the terminating value that you know
{
    cin>>variable
    .
    .
    .
}

Flag-controlled while loop:

Flag controlled while loop uses a bool variable to control the loop. This loop looks like the one shown below:

bool found = false;
while (!found)
{
    If (expression)
        found = true;
    .
    .
}

Final words:

C while loop with its different types has been discussed in this c tutorial for beginners. By studying the given examples one can easily understand how to use while loop in c.

Preprocessor Directives In C

Preprocessor directives in C:

In C++, we don’t have large number of operations. Only small number of operation like arithmetic and assignment operations are available. But to run a C++ program many type of functions are required. C++ has a collection of libraries. We use them to get access to many interesting and important operations. Every library has a unique name and is referred to by a header file.

Examples:

The function needed to perform input/output (I/O) are contained in the header file iostream.
For performing useful mathematical operations like power, absolute and sine, we use a header file named cmath
If you want to use these functions, you need to tell the computer where to find the necessary code as you know computer is dumb. You use c pre-processor directives and the names of header files to tell the computer the locations of the code provided in libraries.
Preprocessor directives c
are commands supplied to the preprocessor that cause the preprocessor to modify the text of a C++ program before it is compiled. All preprocessor commands begin with #.

Syntax:

#include <header file name>

Example:

  • #include<iostream>
  • #include<cmath>


Note: These headerfiles are defined at the start so that you can use them throughout the program.



Namespace:

In earlier tutorials, we learnt about cin and cout. These functions are defined in iostream but within a namespace. The name of this namespace is std. We’ll discuss the namespace in detail in further tutorials. For now, we’ll stick to basics.

We declare it by writing:

using namespace std;

If we don’t use it then every time we have to introduce cout and cin like the one defined below throughout the program.
std::cin
std::cout

Final Words:

Preprocessor directives in c are very important.Without them the programming must have been very difficult.In this c tutorial for beginners we discussed them in detail so that our viewers can understand them easily.