Friday 16 April 2010

Write a program to read the address

#include <stdio.h>

int main(void)
{

  char a;
  char b;

  printf("The address of variable a is: %p\n",&a);
  printf("The address of variable b is: %p\n",&b);


  return 0;

}//end main

Printing character Array using pointers with puts function

#include<stdio.h>
int main(void)
{
  char* ptr0;

  ptr0="ptr0 points to this string.";
  puts(ptr0);
  ptr0="A shorter string.";
  puts(ptr0);
  ptr0="A new string for ptr0 that is longer than the previous.";
  puts(ptr0);
  return 0;
}

Printing character Array using pointers


#include < stdio.h >

    int main(void) {
        char anyPhrase[] = "Go Jaguars!";
        char * ptr = anyPhrase;
        int i;

        for (i = 0; i < sizeof(anyPhrase) - 1; i++) {
            putchar('\n');
            putchar( * ptr++);
        }

        putchar('\n');

        return 0;

    }

Example program on pointer to pointer

#include <stdio.h>

int main (void)
{

  int a;
  int* pa = &a;
  int** pp = &pa;

  printf("\nPlease enter an integer value: ");
  scanf("%d",&a);

  printf("\nValue of variable a: %d", a);
  printf("\nAddress of variable a: %p", &a);
  printf("\nValue of pointer pa: %p", pa);
  printf("\nDe-referenced value of pointer pa: %d", *pa);
  printf("\nValue of pointer pp: %p", pp);
  printf("\nDe-referenced value of pointer pp: %d\n\n", **pp);

  return 0;

}

Pointers

Example program on Pointer array

#include < stdio.h >

    void PrintErrorMsg(int errorNum);

int main(void) {

        PrintErrorMsg(3);

        return 0;

    } //end main

void PrintErrorMsg(int errorNum) {

        static char * errorList[] = {
            "Cannot open file.\n",
            "Read error.\n",
            "Write error.\n",
            "Media failure.\n"
        };

        printf("%s", errorList[errorNum]);

        return;

    } //end PrintErrorMsg





Write a program to show same pointer can point to different data variables in different statements

/*    This program shows how the same pointer can point to different data variables in different statements. */
#
include < stdio.h >

    int main(void) {
        //    Local Declarations
        int a;
        int b;
        int c;
        int * pMult;

        //    Statements
        printf("Enter three numbers and key return: ");
        scanf("%d %d %d", & a, & b, & c);
        pMult = & a;
        printf("%3d\n", * pMult);
        pMult = & b;
        printf("%3d\n", * pMult);
        pMult = & c;
        printf("%3d\n", * pMult);
        return 0;
    } // main

/*    Results
Enter three numbers and key return: 10 20 30

 10
 20
 30
*/


Pointers comparasion

//Pointers comparasion 

#include <stdio.h>

int* ReturnSmaller(int* p1, int* p2);

int main (void)
{

  int a;
  int b;
  int* pSmaller = NULL;

  printf("Please enter an integer: ");
  scanf(" %d", &a);

  printf("Please enter another integer: ");
  scanf(" %d", &b);

  pSmaller = ReturnSmaller(&a, &b);

  printf("\n%d is the smaller value.\n\n", *pSmaller);

  return 0;

}//end main



int* ReturnSmaller(int* p1, int* p2)
{

  return (*p1 < *p2 ? p1 : p2);

}


Pointers

Pointers: C program for addition for two numbers using Pointers

/*    This program adds two numbers using pointers to demonstrate the concept of pointers.*/
#include < stdio.h >

    int main(void) {
        //    Local Declarations
        int a;
        int b;
        int r;
        int * pa = & a;
        int * pb = & b;
        int * pr = & r;

        //    Statements
        printf("\nEnter the first number : ");
        scanf("%d", pa);
        printf("Enter the second number: ");
        scanf("%d", pb); * pr = * pa + * pb;
        printf("\n%d + %d is %d\n\n", * pa, * pb, * pr);
        return 0;
    } // main

/*    Results:
    Enter the first number : 15
    Enter the second number: 51

    15 + 51 is 66
*/

enumerations: Write a program to display selected TV Stations using Enumerations

/*    Print selected TV stations for our cable TV system.
*/
#include < stdio.h >

    int main(void) {
        // Local Declarations
        enum TV {
            fox = 2, nbc = 4, cbs = 5,
                abc = 11, hbo = 15, show = 17,
                max = 31, espn = 39, cnn = 51
        };

        // Statements
        printf("Here are my favorite cable stations:\n");
        printf("  ABC: \t%2d\n", abc);
        printf("  CBS: \t%2d\n", cbs);
        printf("  CNN: \t%2d\n", cnn);
        printf("  ESPN:\t%2d\n", espn);
        printf("  Fox: \t%2d\n", fox);
        printf("  HBO: \t%2d\n", hbo);
        printf("  Max: \t%2d\n", max);
        printf("  NBC: \t%2d\n", nbc);
        printf("  Show:\t%2d\n", show);
        printf("End of my favorite stations. \n");
        return 0;
    } // main

/*    Results:
Here are my favorite cable stations:
  ABC:    11
  CBS:     5
  CNN:    51
  ESPN:   39
  Fox:     2
  HBO:    15
  Max:    31
  NBC:     4
  Show:   17
End of my favorite stations.
*/

Structures: Write a program to sort an array of student using Structures

/*    This program sorts an array of student structures*/
#include < stdio.h >
#include < string.h >
#include < stdbool.h >

    #define NUM_STU 5

//    Global Type Declaration
typedef struct {
    char name[26];
    int midterm[3];
    int final;
}
STUDENT;

//    Function Declarations
void insertionSort(STUDENT list[], int last);

int main(void) {
        //    Local Declarations
        STUDENT stuAry[NUM_STU] = {
            {
                "Charles, George",
                {
                    85,
                    94,
                    79
                },
                93
            },
            {
                "Adams, Karin",
                {
                    75,
                    91,
                    89
                },
                89
            },
            {
                "Nguyen, Tuan",
                {
                    87,
                    88,
                    89
                },
                90
            },
            {
                "Oh, Bill",
                {
                    78,
                    96,
                    88
                },
                91
            },
            {
                "Chavez, Maria",
                {
                    83,
                    79,
                    93
                },
                91
            }
        }; // stuAry

        STUDENT * pStuPtr;

        //    Statements
        printf("Unsorted data:\n");
        for (pStuPtr = stuAry; pStuPtr < stuAry + NUM_STU; pStuPtr++)
            printf("%-26s %4d %4d %4d %4d\n",
                pStuPtr - > name,
                pStuPtr - > midterm[0],
                pStuPtr - > midterm[1],
                pStuPtr - > midterm[2],
                pStuPtr - > final);
        printf("\n");

        insertionSort(stuAry, NUM_STU - 1);

        printf("Sorted data:\n");
        for (pStuPtr = stuAry; pStuPtr < stuAry + NUM_STU; pStuPtr++)
            printf("%-26s %4d %4d %4d %4d\n",
                pStuPtr - > name,
                pStuPtr - > midterm[0],
                pStuPtr - > midterm[1],
                pStuPtr - > midterm[2],
                pStuPtr - > final);
        return 0;
    } // main

/*    ================== insertionSort ==================
    Sort list using Insertion Sort. The list is divided
    into sorted and unsorted lists. With each pass, the
    first element in unsorted list is inserted into
    sorted list.
       Pre   list must contain at least one element
             last is index to last element in list
       Post  list has been rearranged.
*/
void insertionSort(STUDENT list[], int last) {
        //    Local Declarations
        bool located;
        STUDENT temp;
        STUDENT * pCurrent;
        STUDENT * pWalker;
        STUDENT * pLast;

        //    Statements
        for (pCurrent = list + 1, pLast = list + last; pCurrent <= pLast; pCurrent++) {
            located = false;
            temp = * pCurrent;

            for (pWalker = pCurrent - 1; pWalker >= list && !located;)
                if (strcmp(temp.name, pWalker - > name) < 0) { * (pWalker + 1) = * pWalker;
                    pWalker--;
                } // if
                else
                    located = true; * (pWalker + 1) = temp;
        } // for pCurrent
        return;
    } // insertionSort
    //    ================= End of Program =================

/*    Results:
Unsorted data:
Charles, George              85   94   79   93
Adams, Karin                 75   91   89   89
Nguyen, Tuan                 87   88   89   90
Oh, Bill                     78   96   88   91
Chavez, Maria                83   79   93   91

Sorted data:
Adams, Karin                 75   91   89   89
Charles, George              85   94   79   93
Chavez, Maria                83   79   93   91
Nguyen, Tuan                 87   88   89   90
Oh, Bill                     78   96   88   91
*/

Structures: Write a program to simulate the time using 'structures'

/*   Write a program to simulate the time using 'structures' */
#include < stdio.h >

    typedef struct {
        int hr;
        int min;
        int sec;
    }
CLOCK;

//    Function Declaration
void increment(CLOCK * clock);
void show(CLOCK * clock);

int main(void) {
        //    Local Declaration
        CLOCK clock = {
            14,
            38,
            56
        };
        int i;

        //    Statements
        for (i = 0; i < 6; ++i) {
            increment( & clock);
            show( & clock);
        } // for
        return 0;
    } // main

/*    ==================== increment ====================
    This function accepts a pointer to clock and 
    increments the time by one second.
       Pre   previous clock setting
       Post  clock incremented by one second.
*/
void increment(CLOCK * clock) {
        //    Statements
        (clock - > sec) ++;
        if (clock - > sec == 60) {
            clock - > sec = 0;
            (clock - > min) ++;
            if (clock - > min == 60) {
                clock - > min = 0;
                (clock - > hr) ++;
                if (clock - > hr == 24)
                    clock - > hr = 0;
            } // if 60 min
        } // if 60 sec
        return;
    } // increment

/* ====================== show ======================
    This function shows the current time in military form.
       Pre   clock time
       Post  clock time displayed
*/
void show(CLOCK * clock) {
        //    Statements
        printf("%02d:%02d:%02d\n",
            clock - > hr, clock - > min, clock - > sec);
        return;
    } // show

/*    Results:
14:38:57
14:38:58
14:38:59
14:39:00
14:39:01
14:39:02
*/

Structures: Program on simulate the multiplication of fractions using Structures

/*    This program uses structures to simulate the
    multiplication of fractions.
*/

#include < stdio.h >

    //    Global Declarations

    typedef struct

{
    int numerator;
    int denominator;
}
FRACTION;

int main(void) {
        //    Local Declarations
        FRACTION fr1;
        FRACTION fr2;
        FRACTION res;

        //    Statements
        printf("\nKey first fraction in the form of x/y:  ");
        scanf("%d /%d", & fr1.numerator, & fr1.denominator);
        printf("Key second fraction in the form of x/y: ");
        scanf("%d /%d", & fr2.numerator, & fr2.denominator);

        res.numerator = fr1.numerator * fr2.numerator;
        res.denominator = fr1.denominator * fr2.denominator;

        printf("\nThe result of %d/%d * %d/%d is %d/%d\n",
            fr1.numerator, fr1.denominator,
            fr2.numerator, fr2.denominator,
            res.numerator, res.denominator);
        return 0;
    } // main

/*    Results:
Key first fraction in the form of x/y:  2/6
Key second fraction in the form of x/y: 7/4

The result of 2/6 * 7/4 is 14/24
*/