Showing posts with label Pointer. Show all posts
Showing posts with label Pointer. Show all posts
Hello Here is a C program to Arrange Elements in an array Using Dynamic Memory allocation. Pointer.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include<stdio.h>
#include<stdlib.h>
main()
{
    int *p,n,j,i,tmp;
    printf("Enter the total no of Element to Arrange in:- ");
    scanf("%d",&n);
    p=(int *) malloc(sizeof(int)*n);
    printf("Enter the Emements:- \n");
    for (i=0;i<n;i++)
    {
        scanf("%d",(p+i));
    }
    for (i=0;i<(n-1);i++)
    {
        for(j=0;j<(n-i-1);j++)
        {
            if(*(p+j)>*(p+j+1))
            {
                tmp=*(p+j);
                *(p+j)=*(p+j+1);
                *(p+j+1)=tmp;
            }
        }
    }
    printf("Finally after arranged in Accesending Order:- \n");
    for (i=0;i<n;i++)
    {
        printf("%d \n",*(p+i));
    }
}

Have a good day!
Hello Here is a C Program to Find the Smallest Element among Array element. Here we use Dynamic Memory Allocation to Set the size of Array. And all other are as usual.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<stdio.h>
#include<stdlib.h>
void main()
{
    int *p,n,i,min;
    printf("Enter the total number of elements:- ");
    scanf("%d",&n);
    p = (int *) malloc(sizeof(int) * n);
    printf("Enter the Elements of the Array:- \n");
    for(i=0;i<n;i++)
    {
        scanf("%d",(p+i));
    }
    min=*p;
    for(i=0;i<n;i++)
    {
        if(min>*(p+i))
        {
            min=*(p+i);
        }
    }
    printf("The Minimum ammong the Elements is %d",min);
}

p = (int *) malloc(sizeof(int) * n);
Here memory are allocated and the first memory is assigned to the first element.
Hello here is a C program to Calculate the factorial and the square of the give number using Call by reference function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include<stdio.h>
int ffact(int *);
int sqr(int *);

void main()
{
    int a,fact;
    printf("Enter the number:- ");
    scanf("%d",&a);
    fact=ffact(&a);
    printf("Factorial of the given number is %d\n",fact);
    fact=sqr(&a);
    printf("Square of that number is %d",fact);
}
int ffact(int *p)
{
    int i,fact=1;
    if(*p == 0)
        return 1;
    else
    for (i=1;i<=*p;i++)
    {
        fact=i*fact;
    }
    return (fact);
}
int sqr(int *p)
{
    int a;
    a = *p * *p;
    return (a);
}

And after code here is nothing I think I need to describe about.
Hello Here is a C program to Swap Two integer variable using call by reference.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<stdio.h>
void swap (int *o, int *p)
{
    int tmp;
    tmp = *o;
    *o = *p;
    *p = tmp;
}
main ()
{
    int a,b;
    printf("Enter the value of A:- ");
    scanf("%d",&a);
    printf("Enter the value of B:- ");
    scanf("%d",&b);
    printf("\nBefore Swap:-\nA = %d\nB = %d",a,b);
    printf("\nAfter Swap:-");
    swap(&a, &b);
    printf("\nA = %d\nB = %d",a,b);
}


Hello Here is a C program to Print address and values at variables
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<stdio.h>
void main()
{
    int a, *b;
    b = &a;
    printf("Enter the value of A:- ");
    scanf("%d",b);
    printf("\n(Indirect) Address of A = %u.\n", b);
    printf("(Direct) Address of A = %u.\n", &a);
    printf("(Pointer) Value of A = %d.\n", *b);
    printf("(Direct) Value of A = %d.\n", a);
    printf("(address operator) Value of A = %u.\n", *(&a));
    printf("Address of B = %u.\n", &b);
    printf("Value of B = %u.\n", b);
    printf("Value of B = %u.\n", *(&b));
}

Run the Program and check the code.