Sleeksoftwares

Banner 468

Facebook
RSS

Variables in programming, variables in c++, variables learning point, what is variables




During programming we need to store data. This data is stored in variables. Variables are locations in memory for storing data. The memory is divided into blocks. It can be viewed as pigeon-holes. You can also think of it as PO Boxes. In post offices there are different boxes and each has an address. Similarly in memory, there is a numerical address for each location of memory (block). It is difficult for us to handle these numerical addresses in our programs. So we give a name to these locations. These names are variables. We call them variables because they can contain different values at different times.



The variable names in C may be started with a character or an underscore ( _ ). But avoid starting a name with underscore ( _ ). C has many libraries which contain variables and function names normally starting with underscore ( _ ). So your variable name starting with underscore ( _ ) may conflict with these variables or function names.
In a program every variable has
o Name o Type  o Size  o Value

The variables having a name, type and size (type and size will be discussed later) are just empty boxes. They are useless until we put some value in them. To put some value in these boxes is known as assigning values to variables. In C language, we use assignment operator for this purpose.
[ Read More ]

The best way to learn C is to start coding right away. So here is our very first program in C.


# include <iostream.h> 
main()
{     
 cout << "Welcome to Agricultural university Faisalabad computer science department";
 }


Description with details of every thing that is used in this program.



We will look at this code line by line and try  to understand them. 
# include <iostream.h> #include: This is a pre-processor directive. It is not part of our program; it is an instruction to the compiler. It tells the C compiler to include the contents of a file, in this case the system file iostream.h. The compiler knows that it is a system file, and therefore.
looks for it in a special place.  The features of preprocessor will be discussed later. For the time being take this line on faith. You have to write this line. The sign # is known as HASH and also called SHARP. 
<iostream.h> This is the name of the library definition file for all Input Output Streams. Your program will almost certainly want to send stuff to the screen and read things from the keyboard. iostream.h is the name of the file in which has code to do that work for you   
main() The name main is special, in that the main is actually the one which is run when your program is used. A C program is made up of a large number of functions. Each of these is given a name by the programmer and they refer to each other as the program runs. C regards the name "main" as a special case and will run this function first. If you forget to have a main function, or mistype the name, the compiler will give you an error. 
Notice that there are parentheses (“( )”, normal brackets) with main. Here the parentheses contain nothing. There may be something written inside the parentheses. It will be discussed in next lectures.
{ }
Next, there is a curly bracket also called braces("{ }"). For every open brace there must be a matching close. Braces allows to group together pieces of a program. The body of main is enclosed in braces. Braces are very important in C; they enclose the blocks of the program.
cout <<  “ Welcome to agricultural university Faisalabad computer science department”
cout:  
This is known as out put stream in C and C++. Stream is a complicated thing, you will learn about it later. Think a stream as a door. The data is transferred through stream, cout takes data from computer and sends it to the output. For the moment it is a screen of the monitor. hence we use cout for output.
<<
The sign << indicates the direction of data. Here it is towards cout and the function of cout is to show data on the screen.
“ Welcome to agricultural university Faisalabad”
The thing between the double quotes (“ ”) is known as character string. In C programming character strings are written in double quotes. Whatever is written after << and within quotation marks will be direct it to cout,  cout will  display it on the screen.
;
There is a semicolon (;) at the end of the above statement. This is very important. All C statements end with semicolon (;). Missing of a semicolon (;) at the end of statement is a syntax error and compiler will report an error during compilation. If there is only a semicolon (;) on a line than it will be called a null statement. i.e. it does nothing. The extra semicolons may be put at the end but are useless and aimless. Do not put semicolon (;) at a wrong place, it may cause a problem during the execution of the program or may cause a logical error.
In this program we give a fixed character string to cout and the program prints it to the screen as:
[ Read More ]

Lecture 07-11-2013 Intro to programming, Programming basics


Cout---------------Console output
Cin--------------------console input

#include <iostram.h>
Void main()
{
Int a,b, sum;
A=3;
B=2;
Sum=a+b;
Cout <<sum;

Input  variables



                                                Computer memory


 



                                                                                                                                                                                                            Name



 




                                                                    Address of variable                                                 value of variable
White Spaces
·         Ignore By C++ Compiler
·         Spacebar, tab, enter,
·         Readability increase



Cout<<”sum”a;
Constant variable
Cout<<”Sum” <<a;

1st Method
Cout<<a<<b<<a+b;
2nd Method
Cout<<a;
Cout<<b;
Cout<<a+b;


How to print a single line

include<iostream.h>
void main()


{


cout<<"Traboc++";
}




[ Read More ]

C++ programme using a class student to calculate the percentage of marks obtained, percentage Code, How to calculatet percentage in C++

C++ programme using a class student to calculate the percentage of marks obtained by him, Calculate percentage in C++ with simple code.


//--Student Name--//
//--Muhammad Idrees--//
#include<iostream.h>
#include<conio.h>

class student
{
               int roll_no;
               char name[20];
               char class_st[8];
               int marks[5];
               float percentage;
               float calculate();
               public:
               void readmarks();
               void displaymarks();
};


float student::calculate()
{
               percentage=0;
               for(int i=0;i<5;i++)
                              percentage+=marks[i];
               percentage=(percentage/5);
               return percentage;
}


void student::readmarks()
{
               cout<<"Enter the roll no.:";
               cin>>roll_no;
               cout<<"Enter the name:";
               cin>>name;
               cout<<"Enter the class studing in:";
               cin>>class_st;
               cout<<"Enter the marks:"<<endl;
               for(int j=0;j<5;j++){
                              cout<<"\tEnter mark "<<j+1<<":";
                              cin>>marks[j];
               }
}

//-----display marks of student---//
void student::displaymarks()
{
               cout<<"Roll no:"<<roll_no<<endl;
               cout<<"Name:"<<name<<endl;
               cout<<"Class:"<<class_st<<endl;
               cout<<"Percentage:"<<calculate()<<endl;
}

//-----enter student details-----//
int main()
{
               student s1;
               s1.readmarks();
               s1.displaymarks();
               return 0;
}
//------Output---------//
OUTPUT:

Enter the roll no.:12
Enter the name:Muhammad Idrees                                                          
Enter the class studing in:M.sc Computer sciences                                                  
Enter the marks:                                                               
        Enter mark 1:99                                                         
        Enter mark 2:95                                                        
        Enter mark 3:90                                                        
        Enter mark 4:80                                                         
        Enter mark 5:99                                                        

Roll no:12                                                                     
Name:Muhammad Idrees                                                                    
Class:12                                                                       
Percentage:92.59

return 0;
end of program.


[ Read More ]

Introduction to programming languages, what is programming & why we do this, How we learn basic concepts about programming.


Today’s computers are incredibly fast, and getting faster all the time. Yet with this speed comes some significant constraints. Computers only natively understand a very limited set of instructions, and must be told exactly what to do. The set of instructions that tells a computer what to do is known as software. The computer machinery that executes the instructions is the hardware.
A computer’s CPU is incapable of speaking C++. The very limited set of instructions that a CPU natively understands is called machine code, or machine language, or an instruction set. How these instructions are organized is beyond the scope of this introduction, but it is interesting to note two things. First, each instruction is composed of a number of binary digits, each of which can only be a 0 or a 1. These binary numbers are often called bits (short for binary digit). For example, the MIPS architecture instruction set always has instructions that are 32 bits long. Other architectures (such as the x86, which you are likely using) have instructions that can be a variable length.
For example, here is a x86 machine language instruction: 10110000 01100001
Second, each set of binary digits is translated by the CPU into an instruction that tells it to do a very specific job, such ascompare these two numbers, or put this number in that memory location. Different types of CPUs will typically have different instruction sets, so instructions that would run on a Pentium 4 would not run on a Macintosh PowerPC based computer. Back when computers were first invented, programmers had to write programs directly in machine language, which was a very difficult and time consuming thing to do.
Because machine language is so hard to program with, assembly language was invented. In an assembly language, each instruction is identified by a short name (rather than a set of bits), and variables can be identified by names rather than numbers. This makes them much easier to read and write. However, the CPU can not understand assembly language directly. Instead, it must be translated into machine language by using an assembler. Assembly languages tend to be very fast, and assembly is still used today when speed is critical. However, the reason assembly language is so fast is because assembly language is tailored to a particular CPU. Assembly programs written for one CPU will not run on another CPU. Furthermore, assembly languages still require a lot of instructions to do even simple tasks, and are not very human readable.
Here is the same instruction as above in assembly language: mov al, 061h
To address these concerns, high-level programming languages were developed. C, C++, Pascal, Ada, Java, Javascript, and Perl, are all high level languages. Programs written in high level languages must be translated into a form that the CPU can understand before they can be executed. There are two primary ways this is done: compiling and interpreting.
A compiler is a program that reads code and produces a stand-alone executable that the CPU can understand directly. Once your code has been turned into an executable, you do not need the compiler to run the program. Although it may intuitively seem like high-level languages would be significantly less efficient than assembly languages, modern compilers do an excellent job of converting high-level languages into fast executables. Sometimes, they even do a better job than human coders can do in assembly language!
Here is a simplified representation of the compiling process:



An interpreter is a program that reads code and essentially compiles and executes (interprets) your program as it is run. One advantage of interpreters is that they are much easier to write than compilers, because they can be written in a high-level language themselves. However, they tend to be less efficient when running programs because the compiling needs to be done every time the program is run. Furthermore, the interpreter is needed every time the program is run.
Here is a simplified representation of the interpretation process



Any language can be compiled or interpreted, however, traditionally languages like C, C++, and Pascal are compiled, whereas “scripting” languages like Perl and Javascript are interpreted. Some languages, like Java, use a mix of the two.
High level languages have several desirable properties. First, high level languages are much easier to read and write.
Here is the same instruction as above in C/C++: a = 97;
Second, they require less instructions to perform the same task as lower level languages. In C++ you can do something like a = b * 2 + 5; in one line. In assembly language, this would take 5 or 6 different instructions.
Third, you don’t have to concern yourself with details such as loading variables into CPU registers. The compiler or interpreter takes care of all those details for you.
And fourth, they are portable to different architectures, with one major exception, which we will discuss in a moment.





The exception to portability is that many platforms, such as Microsoft Windows, contain platform-specific functions that you can use in your code. These can make it much easier to write a program for a specific platform, but at the expense of portability. In these tutorials, we will explicitly point out whenever we show you anything that is platform specific.
[ Read More ]

Basics of C++ Programming, Learn C++ in a very simple way, C++ Learing point.

staring with step by step code and learn what is this code and why we insert this code in program.













[ Read More ]

Dev-C++ Download Free for C++ Code, Dev-C++ Download, Download Dev-C++

If you want to download Dev-C++ Please click here to download.


[ Read More ]