Sunday, 12 January 2014

Definition
An array is basically a series of objects of same type. Each object in an array is known as an array element. Our motive here is to find the sum of elements stored in an array entered by the user. An array is declared as: 
int arr[10];
where arr[ ] is the array and arr[1], arr[2], arr[3] so on.. are the array elements. Values could be assigned to these array elements as:
arr[1]=10;
arr[2]=7;
arr[3]=13;
Sample Program
:
#include <iostream>

using namespace std;

int main()
{
    int a[10],n,i;
    int sum(int [],int n );
    cout << "Enter size of array=" << endl;
    cin>>n;
    cout<<"Enter array elements=";
    for(i=0;i<n;i++)
    {
    cin>>a[i];
    }
    cout<<"Sum is="<<sum(a,n);
}
    int sum(int a[],int n)
    {
        int s=0,i;
        for(i=0;i<n;i++)
        {
        s=s+a[i];
        }

        return s;
    }


Adding elements in array in c++ using functions

Definition
An array is basically a series of objects of same type. Each object in an array is known as an array element. Our motive here is to find the sum of elements stored in an array entered by the user. An array is declared as: 
int arr[10];
where arr[ ] is the array and arr[1], arr[2], arr[3] so on.. are the array elements. Values could be assigned to these array elements as:
arr[1]=10;
arr[2]=7;
arr[3]=13;
Sample Program
:
#include <iostream>

using namespace std;

int main()
{
    int a[10],n,i;
    int sum(int [],int n );
    cout << "Enter size of array=" << endl;
    cin>>n;
    cout<<"Enter array elements=";
    for(i=0;i<n;i++)
    {
    cin>>a[i];
    }
    cout<<"Sum is="<<sum(a,n);
}
    int sum(int a[],int n)
    {
        int s=0,i;
        for(i=0;i<n;i++)
        {
        s=s+a[i];
        }

        return s;
    }


This entry was posted in :

Thursday, 13 June 2013

getChars:  
getChars is used to select the more than one character using locations

// Program to use of getchars

class getChars
{
public static void main(String arr[])
{
String a="There is Shaminder singh";
int start=6;
int end=14;
char buf[]= new char[end-start];
a.getChars(start,end,buf,0);
System.out.println(buf);
}
}




Use of getChars

getChars:  
getChars is used to select the more than one character using locations

// Program to use of getchars

class getChars
{
public static void main(String arr[])
{
String a="There is Shaminder singh";
int start=6;
int end=14;
char buf[]= new char[end-start];
a.getChars(start,end,buf,0);
System.out.println(buf);
}
}




LENGTH():   length() is used to find the length of a string


// To print the string and its length.

class Length
{
public static void main(String arr[])
{
String s="getcoderesults.com";                   //For declare String literals
System.out.println(s);
System.out.println(s.length());

}}



Output:



Length Function

LENGTH():   length() is used to find the length of a string


// To print the string and its length.

class Length
{
public static void main(String arr[])
{
String s="getcoderesults.com";                   //For declare String literals
System.out.println(s);
System.out.println(s.length());

}}



Output:



This entry was posted in :
// To find the character on a given location

class ExactLoc
{
public static void main(String arr[])
{
String a="shaminder";
System.out.println(a.charAt(4));
}}






Use of charAt

// To find the character on a given location

class ExactLoc
{
public static void main(String arr[])
{
String a="shaminder";
System.out.println(a.charAt(4));
}}






This entry was posted in :
// A simple program to print the length of character.

class A
{
public static void main(String arr[])
{
char ch[]={'a', 'b', 'c', 'd', 'e'};
String s1=new String(ch);
System.out.println(s1.length());
}
}



Output:

Use of Lenth Function

// A simple program to print the length of character.

class A
{
public static void main(String arr[])
{
char ch[]={'a', 'b', 'c', 'd', 'e'};
String s1=new String(ch);
System.out.println(s1.length());
}
}



Output:
This entry was posted in :

Tuesday, 11 June 2013

String Handling provides a lot of concepts that can be performed on a string such as concatinating string, comparing string, substring etc. 

// Simple program to  print the length of character in a array

  class A
{
public static void main(String arr[])
{
char ch[]={'a', 'b', 'c', 'd', 'e'};
String s1=new String(ch);
System.out.println(s1.length());
}
}


Output:





























String Handling provides a lot of concepts that can be performed on a string such as concatinating string, comparing string, substring etc. 

// Simple program to  print the length of character in a array

  class A
{
public static void main(String arr[])
{
char ch[]={'a', 'b', 'c', 'd', 'e'};
String s1=new String(ch);
System.out.println(s1.length());
}
}


Output:





























This entry was posted in :

Sunday, 9 June 2013

Using the keyword interface, you can fully abstract a class interface from its implementations. Interfaces are similar to classes, but they lack instance variables, and their methods are declared without any body. Once, it is defined any number of classes can implements an interface. Also, one class can implement any number of interfaces.
      An interface is defined much like a class. This is the general form of an interface:
      
           access interface name
          {
           return-type method-name1(parameter-list);

            return-type method-name2(parameter-list);


             type final-varname1=value;
             
              type final-varname1=value; 
               
              }

//      A simple program to describe interface

interface Drawobject
{
 float P=3.14f;
public void Area(int x);

}
class Mcolor
{
void Setcolor(String col)
{
System.out.println("Color selected by you is  "+   col);
}
}

class circle extends Mcolor implements Drawobject
{
public void Area(int r)
{
float res=P*r*r;
System.out.println("Area of circle with r  "+   res);
}
}

class Testinterface
{
public static void main(String arg[])
{
circle obj=new circle();
obj.Setcolor("red");
obj.Area(5);
}
}


Output:




Interfaces

Using the keyword interface, you can fully abstract a class interface from its implementations. Interfaces are similar to classes, but they lack instance variables, and their methods are declared without any body. Once, it is defined any number of classes can implements an interface. Also, one class can implement any number of interfaces.
      An interface is defined much like a class. This is the general form of an interface:
      
           access interface name
          {
           return-type method-name1(parameter-list);

            return-type method-name2(parameter-list);


             type final-varname1=value;
             
              type final-varname1=value; 
               
              }

//      A simple program to describe interface

interface Drawobject
{
 float P=3.14f;
public void Area(int x);

}
class Mcolor
{
void Setcolor(String col)
{
System.out.println("Color selected by you is  "+   col);
}
}

class circle extends Mcolor implements Drawobject
{
public void Area(int r)
{
float res=P*r*r;
System.out.println("Area of circle with r  "+   res);
}
}

class Testinterface
{
public static void main(String arg[])
{
circle obj=new circle();
obj.Setcolor("red");
obj.Area(5);
}
}


Output:




Thursday, 6 June 2013