Pages

Tuesday, January 26, 2010

Labsheet1

Ni labsheet untuk Data Structure & Algorithm. Aku letak kat sini untuk share ngan sesape yang perlukan je. Kalau just nak copy and paste pun xde hal. Itu hal korang la. Aku harapkan feedback yang leh kita kongsi sama-sama..

Try to understand...
Jangan just nak ambik je. Aku xrugi.....

/*1.     Write a program which prints the characters in a cstring
        in reverse order.
                char s[10] = "abcdefgh";
                char* cptr; */

    // ADD YOUR OWN CODE


#include

void main (){
    char s[10] = "abcdefgh";
    char* cptr;

    printf("Characters is %s\n",s);
   printf("Reverse order: ");
    for(int i=7;i>=0;i--){
    cptr = &s[i];
    printf("%c",*cptr);
    }
}




---------------------------------------------------------------------------------------------------------------

/*2.    Write a program with a function that returns a pointer to the
        maximum value of an array of double's.  If the array is empty,
        return NULL.
 
        double* maximum(double* a, int size);*/

#include
#include
void main()
{
    int maximum(int a[],int c);
    int limit,b,f=2;
    int a[2];

    printf("Enter list of numbers:\n");

    for(b=0;b
        scanf("%d",&a[b]);
    limit=maximum(a,f);
    printf("The largest number is %d",limit);


}
int maximum(int a[],int f)
{
    int b,e=1;
    for(b=0;b

{
        if(a[b]>e)
            e=a[b];
}
    return e;
}



-----------------------------------------------------------------------------------------------------------------

/*3 (a)    Refer to the statement below:

#include

void main()
{
clrscr();
char a = 'o', d = 'h';
char *p1, *p2;
p1 = &a;
p2 = &d;
printf("p1 = %c p2 = %c", *p1, *p2);
}

Write a segment in C language to generate the output:
    p1= h   
    p2=h    */

     #include
     #include


     void main(){

        //clrscr();

        char a = 'o', b = 'h';
        char *p1, *p2;
        p1 = &a;
        p2 = &b;

        printf("p1 = %c ,p2 = %c", *p1,*p2);

        p1 = p2;
      printf("\np1 = %c ,p2 = %c", *p1,*p2);

        }



----------------------------------------------------------------------------------------------------------------------

/*(b)     Write a program that takes three variables (a, b, c) in as separate parameters
        and rotates the values stored so that value a goes to b, b, to c and c to a.
        You MUST use pointers to points to the elements.*/

     #include
     #include


     void main(){

        //clrscr();

        char x = 'a', y = 'b', z = 'c';
        char *p1, *p2 , *p3;
        p1 = &x;
        p2 = &y;
        p3 = &z;

        printf("1st given variable:\n(%c,%c,%c)", *p1,*p2,*p3);

        p3 = p1;
        p1 = p2;
        printf("\n\nAfter rotates:\n(%c,%c,%c)", *p1,*p2,*p3);

        }

0 komen:

Post a Comment