Tuesday, 31 January 2017

Passing 2 Dimensional Array To a Function

In this program, we will learn to pass a 2-dimensional array to a function,



Passing 2 Dimensional Array To a Function


passing a 2-dimensional array 

/*Three are three ways in which we can pass 2-D array*/
/* Passing 2 Dimensional Array To a Function */

#include<stdio.h>
void display ( int *q, int , int) ;
void show ( int ( *q)[4], int , int ) ;
void print ( int q[ ][4], int , int ) ;
int main( )
{
int a[3][4] = {
1,2,3,4,
5,6,7,8,
9,0,1,6

  } ;
display ( a, 3, 4 ) ;
show ( a, 3, 4 ) ;
print ( a, 3, 4 ) ;
}
void display ( int *q, int row, int col )
{
int i,j ;
for ( i=0; i<row; i++)
{
for ( j=0; j<col; j++)
printf("%d",*(q+i*col+j));
printf("\n");


}
printf("\n");
}

void show (int ( *q )[4], int row, int col )
{
int i,j ;
int *p ;

for (i = 0; i< row;i++)
{
p = q + i;
for( j=0; j< col; j++)
printf("%d",*(p+j));
printf("\n");
}
printf("\n");
}

void print (int q[ ][4], int row, int col)
{
int i,j;

for(i=0;i<row;i++)
{
for(j=0; j<col; j++)
printf("%d", q[i][j]);
printf("\n");
}
printf("\n");
}


And Here is The Output....
1234
5678
9016


1234
5678
9016


1234
5678
9016

in the display() function, we have collected the base address of the 2-D array being passed to it in an ordinary int pointer. Then, through the two for loops using the expression * (q + i*col+j), we have reached the appropriate element in the array. Suppose i is equal to 2 and j is equal to 3, then we wish to reach the element a[2][3]. let us see whether the expression *( q+ i * col +j) does give this element or not. Refer below image to understand this.

Passing 2 Dimensional Array To a Function
figure 9.7
The expression *(q+i*col) becomes *(65502+2*4+3). This turns out to be *(65502+11). Since 65502 is the address of an integer, *(65502+11) turns out to be *(65546). Value at this address is 6. This is indeed same as a[2][3]. A more general formula is 6. this is ineed same as a[2][3]. A more general formula for accessing each array element would be:

*(base address + row no. *no.of columns+columns no.)

In the show( ) function, we have defined q to be a pointer to an array of 4 integers through the declaration:

int(*q)[4];
To begin with, q holds the base address of the zeroth 1-D array, i.e 65502 (refer figure 9.7). This address is then assigned to p, an int pointer, and then using this pointer, all elements of the zeroth 1-D array are accessed. Next time through the loops, when i takes a value 1, the expression q+1 fetches the address of the first 1-D array. this is because q is a pointer to a zeroth 1-D array and adding 1 to it would give us the address of the next 1-D array. This address is once again assigned to p and using it all elements of the next 1-D array are accessed.

In the third function print( ), the declaration of q looks like this:

int q[ ][4];

This is same as int ( *q )[4], where q is a pointer to an array of 4 integers. the only advantages are that we can now use the more familiar expression q[i][j] to access array elements. We could have used the same expression in the show() as well.
If you have any problem with this program comment below. and if you like this program like this page and comment below.

Saturday, 28 January 2017

pointer to an array

POINTER TO AN ARRAY
if we can have a pointer to an integer,  a pointer to a float, a pointer to a char, then can we not have a pointer to an array? We certainly can. The following program shows how to build and use it.





/* Usage of pointer to an array */

#include<stdio.h>
int main( )
{
       int s[4][2] = {
                                         { 1234, 56 },
                                         { 1212, 33 },
                                         { 1434, 80 },
                                         { 1312, 78 },
                             };

int ( *p)[2] ;
int i,j, *pint;
for (i = 0; i <=3; i++)
      
     {
              p = &s[i];
              pint = ( int * ) p;
              printf ( "\n" );
              for (j = 0 ; j <= 1 ; j++)
                        printf ("%d", *( pint + j )) ;
       }
       return 0;
}

OUTPUT IS 

1234 56
1212 33
1434 80 
1312 78

Here P  is a pointer to an array of two integers. Note that the parentheses in the declaration of  p
are necessary. Absence of them would make p an array of 2 integer pointers. In the outer for loop, each time we store the address of a new one-dimensional array. Thus first time though this loop, p would contain the address of the zeroth 1-D array. This address is then assigned to an integer pointer pint. Lastly, in the inner for loop using the pointer pint, we have printed the individual elements of the 1-D array to which P is pointing. 
But why should we use a pointer to an array to print elements of a 2-D array. Is there any situation where we can appreciate its usage better? The entity pointer to an array is immensely useful when we need to pass a 2-D array to a function. This is discussed in the next section.

Wednesday, 25 January 2017

Basic-python-programs-for-practice

Basic-python-programs-for-practice
Phyton For Beginners some basic program in phython

Python is a general purpose powerful high-level, object-oriented programming language created by Guido van Rossum.which means it can be used to build just about anything, which will be made easy with the right tools/libraries.
Guido decided to create python programming language after he realized that the ABC language had lack of features. Therefore, he made python programming language and picked the important features from Modula-3 and ABC.

1.  Hello world in phython:

This is a simple program to display "Hello World" in phython. This program basically used to describe the syntax of this language.

print('Hello world!')

Output
Hello world!

In this program we used a Built-in function print() to print the input string ('Hello world') on our screen.
string is a sequence of character. In phython strings enclosed in single quotes double quotes and triple quotes.

2. PHYTHON PROGRAM TO ADD TWO NUMBERS

In this program you will learn how to add two numbers and display the result on screen. In this program we have used the arithmetic addition operator (+) to add two numbers

num1 = 2.5
num2 = 4.5

#This line will add two numbers

sum = float(num1) + float(num2)

#This line will display the sum of two numbers

print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))


OUTPUT

The sum of 2.5 and 4.5 is 7.0

By changing the operator of (+), we can do subtract (-) , multiply (*), divide(/) ,  or find remainder (%)of two numbers but first we have to put operators for each .

ADD TWO NUMBERS ENTER BY USER

In this program we will ask users to enter two numbers. we will use built-in function input() to take the input. input() returns a string, so we convert it into number using the float() function.

# Python Program - Add Two Numbers

# Store input numbers
num1 = input('Enter first number: ')
num2 = input('Enter second number: ')

# This line will Add two numbers
sum = float(num1) + float(num2)

#This line will  Display the sum
print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))

3.PHYTHON PROGRAM TO FIND SQUARE ROOT
In this program you will learn to find square root of a number using cmath module and exponent operator.
cmath module

import cmath

# you can change this value for a different result
num = 1+2j


#num = eval(input('Enter a number: '))
num_sqrt = cmath.sqrt(num)
print('The square root of {0} is {1:0.3f}+{2:0.3f}j'.format(num ,num_sqrt.real,num_sqrt.imag))

OUTPUT


The square root of (1+2j) is 1.272+0.786j
we use the sqrt() function in the cmath



exponent operator

we will store number in num and using exponent operator ** we will find square root.

 num = 8

# uncomment to take the input from the user
#num = float(input('Enter a number: '))
num_sqrt = num ** 0.5
print('The square root of %0.3f is %0.3f'%(num ,num_sqrt))

4.Python Program to Calculate the Area of a Triangle
This is the most common program in phython. in this program we will learn to calculate the are of a trianlge.

# Python Program to find the area of triangle
# Three sides of the triangle a, b and c are provided by the user

a = float(input('Enter first side: '))
b = float(input('Enter second side: '))
c = float(input('Enter third side: '))

# calculate the semi-perimeter
s = (a + b + c) / 2

# calculate the area
area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
print('The area of the triangle is %0.2f' %area)


OUTPUT
The area of the triangle is 14.70

If you have any problem with this program comment below. and if you like this program like this page and comment below .
Can you guess the benefits of using python programming language? People can use this language since it is very easy to learn. It has easy syntax and is fitted for the experts.

Thursday, 12 January 2017

How to Reverse an Array in java

How to Reverse an Array in java
To simply Reverse an array in java you can follow our simple program. Here is the code for reversing an Array.

import java.io.*;
ReverseArray {

/*Function to reverse arr[] from start to end*/
static void reverseArray(int arr[], int start, int end)
{
       int temp;
       if(start >= end)
         return;
       temp = arr[start];
       arr[start] = arr[end];
       arr[end] = temp;
       reverseArray(arr, start+1, end-1);
}
/* Utility that prints out an array on a line */
static void printArray(int arr[], int size)
{
   int i;
   for (i=0; i < size; i++)
       System.out.println(arr[i] +" ");
     System.out.println("");
}
/* Driver function to check for above functions*/
public static void main (String[] args) {
       int arr[] = {1,2,3,4,5,6};
       printArray(arr,0,5);
       System.out.println("Reversed array is");
       printArray(arr, 6);
   }
}

Another Program to reverse an array in java Without using an additional array.


import java.util.Arrays;

public class ArrayReverseExample
{
    static void reverseArray(int inputArray[])
    {
        System.out.println("Array Before Reverse : "+Arrays.toString(inputArray));
       
        int temp;
       
        for (int i = 0; i < inputArray.length/2; i++)
        {
            temp = inputArray[i];
           
            inputArray[i] = inputArray[inputArray.length-1-i];
           
            inputArray[inputArray.length-1-i] = temp;
        }
       
        System.out.println("Array After Reverse : "+Arrays.toString(inputArray));
    }
   
    public static void main(String[] args)
    {  
        reverseArray(new int[]{4, 5, 8, 9, 10});
       
        System.out.println("...................................");
       
        reverseArray(new int[]{12, 9, 21, 17, 33, 7});
       
        System.out.println("...................................");
       
        reverseArray(new int[]{891, 569, 921, 187, 343, 476, 555});
    }  
}

OUTPUT OF THE PROGRAM

Array Before Reverse : [4, 5, 8, 9, 10]
Array After Reverse : [10, 9, 8, 5, 4]
..................................
Array Before Reverse : [12, 9, 21, 17, 33, 7]
Array After Reverse : [7, 33, 17, 21, 9, 12]
...................................
Array Before Reverse : [891, 569, 921, 187, 343, 476, 555]
Array After Reverse : [555, 476, 343, 187, 921, 569, 891]

Another Program


import java.util.ArrayList;
import java.util.Collections;

public class Main {
   public static void main(String[] args) {
      ArrayList arrayList = new ArrayList();
      arrayList.add("A");
      arrayList.add("B");
      arrayList.add("C");
      arrayList.add("D");
      arrayList.add("E");
      System.out.println("Before Reverse Order: " + arrayList);
      Collections.reverse(arrayList);
      System.out.println("After Reverse Order: " + arrayList);
   }
}

if you like this program then let us know. Do a like and comment.

Other programs in java

 Java Program to display triangle 1 23 456 78910

Factorial In Java