-->





Translate

Showing posts with label compiler. Show all posts
Showing posts with label compiler. Show all posts

Passing array to a function cpp code

Leave a Comment
 This Cpp Tutorial contains
  • 3 ways to pass an array to a function
  • Each way and its usage
  • 3 c++ codes examples
  • How codes are working 
  • Sample output
  • Compiler used "C++ CodeBlocks Compiler"

Write a program which declares an array, initialize it then pass to a function. Function takes square of array values and update it. Show updated values in main function.

Concept used:
How programs works
  •   Declare array of size 10
  •   Initialize it using for loop
  •   Calling "UpdateArray" function and passing array as           argument
  •   Take squares of all values and update it
  •   Display final result in main function
 
Note: Function return type is void why? 
          As we know array name is constant pointer memory location if we get this location we can update it after updating if we access this memory location any where in the program we will get updated values. So no necessarily needs to return array or pointer.

First way
As we know array name is a constant pointer if we can get the this pointer we can access all values of array.
declaring  array in function parameter
Prototype: void  UpdateArray(int array[] );

 c++ code

  1.  #include <iostream>
  2.  using namespace std;
  3.  void UpdateArray(int array[])
  4.  {
  5.      for(int i=0;i<=9;i++)
  6.           array[i]=array[i]*array[i];
  7.  }
  8.  int main()
  9.  {
  10.      int array[10];
  11.      for(int i=0;i<=9;i++)
  12.           array[i]=i+1;
  13.      UpdateArray(array);
  14.  // Updated Values of Array
  15.      for(int i=0;i<=9;i++)
  16.           cout<<array[i]<<endl;
  17.      return 0;
  18.  }


Second way
Prototype: void  UpdateArray(int *array );
Declaring an integer pointer in function parameter
This pointer will receive arrays name(constant pointer) and we can access all values and update too.

  1.   #include <iostream>
  2.  using namespace std;
  3.  void UpdateArray(int *array)
  4.  {
  5.      for(int i=0;i<=9;i++)
  6.           array[i]=array[i]*array[i];
  7.  }
  8.  int main()
  9.  {
  10.      int array[10];
  11.      for(int i=0;i<=9;i++)
  12.           array[i]=i+1;
  13.      UpdateArray(array);
  14.  // Updated Values of Array
  15.      for(int i=0;i<=9;i++)
  16.           cout<<array[i]<<endl;
  17.      return 0;
  18. }


Third way
 In this way we pass two arguments one is arrays name and second   is its size
 In function receiving parameters we declares an array or integer pointer (your choice) to receive address of array and another integer variable size which contains the size of array.
Prototype:  void  UpdateArray(int *array, int size );

  1.  #include <iostream>
  2.  using namespace std;
  3.  void UpdateArray(int *array, int size)
  4.  {
  5.      for(int i=0;i<size;i++)
  6.           array[i]=array[i]*array[i];
  7.  }
  8.  int main()
  9.  {
  10.      int array[10];
  11.      for(int i=0;i<=9;i++)
  12.           array[i]=i+1;
  13.      UpdateArray(array,10);
  14.  // Updated Values of Array
  15.      for(int i=0;i<=9;i++)
  16.           cout<<array[i]<<endl;
  17.      return 0;
  18.  }
 Sample output all the codes have same
passing array to a function cpp code different ways
more Arrays Examples Here
C++ Simple Examples
For best practice do experiment with codes and examine output

if any confusion found dry run the code on a paper.
Read More...

Matrices Multiplication in C++ Code Tutorial

Leave a Comment
Write a simple c++ program to multiply two matrices using 2D arrays. Use any compiler turbo c, visual studio or codeblocks compiler.

Program Explanation:

  • Here is the simple example for beginners to understand the basic working of 2Dimensional arrays.
  • Program is very simple it takes input in two 2D arrays each with size of 2X2. 
  • After taking the input program multiply two matrix in 3 nested for loops.
  • Size of arrays is defined using "const int variables" Note: if we do not use const here there will be error because array size needs constant value in square brackets e.g.  array[const value]
Program code:
Compiler used: CodeBlocks C++ Compiler

#include<iostream>
using namespace std;
int main(){
//Using const int for array size
    const int row=2,col=2;
// if not use const error found
cout<<"Size of Matrices : "<<row<<" X "<<col<<endl;
cout<<"Enter Value For FirstMatrix Matrix:"<<endl;

    int firstMatrix[row][col];
    int secondMatrix[row][col];
    int resultantMatrix[row][col], var;

int i,j;
    for( i=0;i<row;i++){
        cout<<"Enter value for row number: "<<i+1<<endl;
        for( j=0;j<col;j++){
            cin>>firstMatrix[i][j];
        }
    }
cout<<"\n\n\nEnter Value For SecondMatrix Matrix:"<<endl;
    for( i=0;i<row;i++){
        cout<<"Enter value for row number: "<<i+1<<endl;
        for( j=0;j<col;j++){
            cin>>secondMatrix[i][j];
        }
    }
var=0;
        // ResultantMatrixant Matrix
        for( i=0;i<row;i++){
            for( j=0;j<col;j++){
                for(int k=0;k<row;k++){
                   var=var+(firstMatrix[i][k]*secondMatrix[k][j]);
                   cout<<var<<endl;
                    }
                 resultantMatrix[i][j]=var;
                 var=0;
            }
        }
cout<<"\n\n\t\tResultant Matrix:"<<endl;

    for( i=0;i<row;i++){
        cout<< endl;
        for( j=0;j<col;j++){
            cout<<"\t\t"<<resultantMatrix[i][j]<<"    ";
        }
    }
return 0;
}






Example input/output
c++ program to calculate matrix multiplication using 2D arrays
This program is very helpful for beginners it can be a complex by adding a matrix multiplication rule.
It is recommended to run this code and add the condition where number of colums of first matrix should be equal than the number of rows of second matrix.
For another example : matrices addition code here
Read More...

c++ program to add two matrices

Leave a Comment
Write a c++ program to add two matrix using 2-D arrays use any c++ compiler, turbo c++, visual studio or codeblocks.
2D arrays are just like tables which consist of rows and colums and each cell contains a value.
Matrix addition as


1     2         1     2           1+1     2+2                       2         4
            +                = >                           Result=
3     4         3     6          3+3      4+6                       6        10


Concept used:
  • const int  (Important concept here for array size)
  • 2D arrays
  • Nested for loops


Program explanation:
  • For 2D array size there must be constant value in square brackets like
    array[constant value][constant value]
  • Two const variables row and col are used to define size
    if we do not make both const then error found because without const reserve word they are
    behaving as variable.
  • Before placing both variable in square brackets they must initialized else error will be found
  • three nested for loops are used two for taking input in matrix 2D arrays and one for resultant matrix

c++ code
compiler:  
codeblocks c++ compiler



#include<iostream>
using namespace std;

int main(){

//Using const int for array size
    const int row=2,col=2;
// if not use const error found

cout<<"Size of Matrices : "<<row<<" X "<<col<<endl;
cout<<"Enter Value For First Matrix:"<<endl;

int first[row][col], second[row][col];

int i,j;
for( i=0;i<row;i++){

    cout<<"Enter value for row number: "<<i+1<<endl;
    for( j=0;j<col;j++){
        cin>>first[i][j];
    }

}


cout<<"\n\n\nEnter Value For Second Matrix:"<<endl;
for( i=0;i<row;i++){

    cout<<"Enter value for row number: "<<i+1<<endl;

    for( j=0;j<col;j++){
        cin>>second[i][j];

    }

}

// Resultant Matrix
for( i=0;i<row;i++){

    for( j=0;j<col;j++){
        first[i][j]=first[i][j]+second[i][j];

    }

}

cout<<"\n\n\t\tResultant Matrix:"<<endl;
for( i=0;i<row;i++){
    cout<< endl;
    for( j=0;j<col;j++){
        cout<<"\t\t"<<first[i][j]<<"    ";

    }

}
return 0;
}



sample input out:
c++ program to add two matrices using 2D array




This program is helpful for c++ beginners to understand the basic working of 2D arrays. It is recommended to change the code and examine the output.
Read More...

inverted triangle shape in c++ code using for loop

2 comments
write a c++ program which displays an inverted triangle using nested for loop and asterisk character

Use any Compiler IDE you want.
Required Output should be look like below image.
inverted triangle shape in c++ code using for loop and asterisk character

 

Compiler used: CodeBlocks c++ Compiler
c++ code:



#include<iostream>
using namespace std;
int main()
{

     cout<<"\"Inverted Triangle Shape\"\n\n";


  int w=6;

   for(int g=0;g<9;g++)
 {
  cout<<"*";    // Displaying asterisk here
 }
  cout<<endl; // endl is for new line

  for(int a=1;a<=3;a++)
 {

    for(int b=0;b<a;b++)
   {
    cout<<" ";    // displaying space here
   }
    cout<<"*";


      for(int c=1;c<w;c++)
     {
      cout<<" ";
     }
      cout<<"*"<<endl;
      w=w-2;
 }

  for(int e=1;e<=1;e++)
 {

    for(int f=4;f>=e;f--)
   {
    cout<<" ";
   }
    cout<<"*";
 }

  return 0;
}


sample output

inverted triangle shape code using for loop sample output c++ programming



These kinds of programs are basic level program can be coded in more than one way it is recommended to change the logic of program and do experiment with the code for better understanding

More C++ Shapes Here

Read More...

Linear search program in c++ using recursion

Leave a Comment
This program runs linear search recursively in an array c++ code
Compiler used: C++ Codeblocks Compiler Concept used:
  • Recursive function Linear Search
  • if else statement

How Program Works
  • Program takes size of array
  • Input elements in array
  • Passing array, key and size to the recursive function recursiveLinearSearch(int array[],int key, int size)
  • Recursive function calls it self until certain conditions fulfill
  • Function returns 1 if record found in array else returns -1



C++ code:
C++ program linear search program using recursive function



    #include<iostream>
    using namespace std;
  
    int recursiveLinearSearch(int array[],int key,int size){
    size=size-1;
      if(size <0){
      return -1;
      }
      else if(array[size]==key){
      return 1;
      }
      else{
      return recursiveLinearSearch(array,key,size);
      }
    }

 
    int main() {
  
      cout<<"Enter The Size Of Array:   ";
    int size;
    cin>>size;
    int array[size], key,i;
    
    // Taking Input In Array
    for(int j=0;j<size;j++){
    cout<<"Enter "<<j<<" Element : ";
    cin>>array[j];
    }
    //Your Entered Array Is
    for(int a=0;a<size;a++){
       cout<<"array[ "<<a<<" ]  =  ";
       cout<<array[a]<<endl;
    }
    cout<<"Enter Key To Search  in Array";
    cin>>key;
    int result;
    result=recursiveLinearSearch(array,key,size--);
    if(result==1){
    cout<<"Key Found in Array  ";
    }
    else{
    cout<<"Key NOT Found in Array  ";
    }
       return 0;
    }
Linear search program in c++ using recursion code end


More Programs.
Linear Search in  C++

Find Prime Number in C++

For more learning change the program and examine the output
Read More...

Define source code, object code, compiler and interpreter

1 comment
What is a Program or a Software?
Set of instructions given to the computer to solve any problem is called a program. A program is written in a computer language

Define programming languages ?
  • User can learn and understand high-level language(HLL) easily. 
  • The instruction of HLL are written in English Statements. 
  • The programs of HLL are not directly executed on the computer. 
  • A language translator is required to translate the HLL into low level language. 

What is low-level language (LLL)?
A language which is close to machine language
The low level language requires a deep understanding of the machine architecture.

What is machine language ?
  • It is the native language of computer . 
  • Every machine language instruction consist of 0's and 1's. 
  • It is difficult for human beings to understand and learn it. 
  • And it is also difficult to locate remove errors in the program. 
  • It is directly executed by the computer. The machine language programs are machine dependent. 

What is meant by program portability?
The programs written in HLL are closed to human language and programs are machine independent i.e. a program can be run o different types of computers this is called program portability.

Define source code?
The program written in HLL is called source program.
The computer does not understand the source code.
The source code is converted into machine code and then it is directly executed ton the computer.

What is Object Program ?
The program which is written in a machine language is called object program the computer understands the object code directly.
What is Linker C programming?
  • The linker is a program that combines the object program with additional library files and produces one executable file with .exe extension. 
  • Linking is a process is which the object file is produced by the compiler is linked to many other files by linker 
  • What is Loader in C programming?
  • For execution, the loader loads the executable files in the memory. 
  • It is also system software in turbo c++ compiler Ctrl+F9 key is used to load and run the program. 
  • The executable file (.exe) runs directly on the computer after loading process. 

What is Language Processor or a language Translator?
It is software that is used to translate the HLL programs into machine language.
Each language has its own translator.
There are three types of language processor
  1. Compiler 
  2. Interpreter 
  3. Assembler 

What is a compiler?
A Translator which translates the source code into object code and the whole program is translated at the same time.
If a program contains other than logical errors then compiler cannot convert the source code into machine code until all the errors are removed from the source program.

What is an Interpreter?
A translator which translates the source code into object code statement by statement.
The working of interpreter is slower that the compiler. Any error in the program stops execution.

Read More...

Compiler downloading,installing,setting directories and working

1 comment
What is a Compiler?
"A compiler is a language translator which converts one  language to other language"  In a C language program compiler converts high level language to low level language and generates results   
There are lots of compilers for C++ usually three compilers are used in start to learn programming 
1)Turbo C++    2)Borland   3)Visual studio 4) Code blocks C++
if you are beginner of 
programming i will recommend you Turbo C++
Installing TURBO C ++
Step 1> Download Turbo C++
Step 2> save the file.

Step 3> Open the file tc3.zip, find
INSTALL.exe from its contents and double click to open it.

Step 4> A new window should open up now, press ENTER.

Step 5> Enter the drive letter you want to install turbo c on and press ENTER.

Step 6> Now on following screen type the source path or press ENTER to select the default path.

Step 7> After this just press F9 on the screen that follows to continue installation.


CONGRACTULATIONS you have set up turbo c on your computer successfully
This tutorial is for Installation of Turbo C on windows XP 32-bit.
If you are a windows 7(64 bit OS) user first you have to download Dos Box to run Turbo C++

Download Dos Box

To set Directories in Turbo C
1.open turbo C
2.Go to options from menu bar.

3.select directories.

then compiler will show menu option like that


4.In first two options write the path where u have installed compiler.
5.In second two options write the path where u want save your file or program.


Working of Compiler
It is very important to understand how compiler works during the execution of program.Compiler works from top to bottom and left to write.

like that our typing method we type word by word left to write and top to bottom.
Read More...