PROGRAMMERS ADDA

Pages

.

CALL BY REFERENCE AND CALL BY VALUE

Thursday, 15 January 2015
Calling function in a programming is a normal case and in c and c++ it is an art by which one can conquer the heart of these languages.
There are two types of calling which can be done in c and c++. Call by value and call by reference.
In call by value- simple a value is passed to the other method and changes made in that value will not be reflected back in calling function.

In call by  reference - simple a value is passed to the other method and changes made in that value will be reflected back in calling function.

Int a;                                                                      call by value                                                        call by reference
                                                                     
                a                                                              a                              p                                                              a ,p
Calling -method2(a);                                       method3(a)       
Receiving –method2(int p)                          method3(int &p)
Meaning – int p =a;                                         int p=a; but p memory
                                                                                Is same for both.
Void method1()
{
Int  a ;
Cout<<enter the value of a”:
Cin>>a;
//Value  is passed to method2 by call by value function
method2(a);  
//Value  is passed to method3 by call by reference function
Method3(a);
}
/* value is received in method2 and it will be received like p=a; any changes occurring in p cannot be reflected in a  because both are operating on different blocks*/
void method2(int p)
{
//change in p does not mean change in a
P=p+1;
Cout<<p;
}

/* value is received in method3 and it will received like p=a; any changes occurring in p will be reflected in a  because both are operating on same same block */

Void method3(int &p)
{
// change in p means change in a
P=p+1;
Cout<<p;


}
Read more ...

Arrays in JAVA

Saturday, 20 April 2013
After building basics in java ,now its time to pack your thoughts and come in the world of arrays.
you have already wanted to learn arrays but due to some way or another you didn't do that.your.probably your instructor or your friends call you dumb who can not learn arrays.so here is goes THE MAKING OF ARRAYS

An array is like a tray of cups.
Steps to declare an array.
step 1. Declare an int array variable .an array variable is a remote control to an array object.
            int[] nums;
step 2.  create a new int array with length of 7,and assign it to the previously declared int[] variable nums
             nums=new int[7];
step 3.  give each element in the array an int value.
              remember elements in an int array are just int variables.
    nums[0]=6;
    nums[1]=7;
    nums[2]=12;
    nums[3]=16;
    nums[4]=62;
    nums[5]=36;
    nums[6]=55;
you can also make arrays of Dogs  instead of nums..............

declare a DOG array variable
Dog[] pets;
create a new name array with a length of five ,and assign it to previously declared name[] variable names.
pets=new Dog[7];
create a new dog objects and assign them to the array elements.
pets[0]=new Dog();
pets[1]=new Dog();
............................
you can also access them as
Dog fido=new Dog();
fido.name="FIDO";
fido.bark(0;
fido.chasecat(0;
*********************************************************************************
programs
MATRIX ADDITION
import java.util.scanner.*;

class Addm
{
 public static void main(String[] arg)
 {
  int i,j;
 int a[][]=new int[3][3];
 Scanner sc=new Scanner(System.in);
 for(i=0;i<3;i++)
 {
   for(j=0;j<3;j++)
 {
  a[i][j]=sc.nextInt();
 }
}
  int b[][]=new int[3][3];
 for(i=0;i<3;i++)
 {
   for(j=0;j<3;j++)
 {
  b[i][j]=sc.nextInt();
}
}
int c[][]=new int[3][3];
 for(i=0;i<3;i++)
 {
   for(j=0;j<3;j++)
 {
  c[i][j]=a[i][j]+b[i][j];
 System.out.println("Matrix Addition="+c[i][j]);
}
}
}
}

*********************************************************************************
DIAGONAL OF MATRIX


import java.util.Scanner;
class Diagonalm
{
 public static void main(String[] arg)
 {
  int i,j;
 int a[][]=new int[3][3];
 Scanner sc=new Scanner(System.in);
 for(i=0;i<3;i++)
 {
   for(j=0;j<3;j++)
 {
  a[i][j]=sc.nextInt();
 }
}
System.out.println("Left diagonal elements=0");
 for(int k=0;k<3;k++)
 {
   for(int l=0;l<3;l++)
 {
 if(k==l)
 {
 System.out.println(a[k][l]);
}
}
}
 System.out.println("Right diagonal elements=0");
 for(int m=0;m<3;m++)
 {
   for(int n=0;n<3;n++)
 {
  if(m+n==2)
 {
  System.out.println(a[m][n]);
 }
}
}
}
}
*********************************************************************************
example to see a dog barking

class Dog
{
String name;
public static void main(string[] args)
{
//make a dog object and access it
Dog dog1=new Dog();
dog1.bark();
dog1.name="bart";
// now make a dog array
Dog[] mydogs=new Dog[3];
// put some dogs in it
mydogs[0]=new dog();
mydogs[1]=new dog();
mydogs[2]=dog1;
//now access dogs using arrays and references
mydog[0].name="fred";
mydogs[1].name="marge";
//now loop through the array
// nad tell all the dogs to bark
int x=0;
while(x<mydogs.length)
{
mydogs[x].bark();
x=x+1;
}
}

public void bark(0
{
System.out.println(name+"says ruff|");
}
public void eat()
{
}
public void chasecat()
{}
}


******************************************************************************
this was just enough to get your hands in array for nay further information comment below.



                   

Read more ...

.

Comment here