Monday, 9 November 2015

Write a program that uses a table of integers whose size will be specified interactively at run time

#include <stdio.h>
#include <stdlib.h>

void main()
{
    int *p, *table;
    int size;
    printf("\nWhat is the size of table?");
    scanf("%d",&size);
    printf("\n");

    if((table = (int*)malloc(size *sizeof(int))) == 0)
       {
           printf("No space available \n");
           exit(1);
       }
    printf("\n Address of the first byte is  %u \n", table);
    /* Reading table values*/
    printf("\nInput table values\n");

    for (p=table; p<table + size; p++)
         scanf("%d",p);
    /* Printing table values <span id="IL_AD3" class="IL_AD">in reverse</span> order*/
    for (p = table + size -1; p >= table; p --)
       printf("%d is stored at address %u \n",*p,p);

getch();
}


Output:

What is the size of the table? 5
Address of the first byte is 2262
Input table values
11 12 13 14 15
15 is stored at address 2270
14 is stored at address 2268
13 is stored at address 2266
12 is stored at address 2264
11 is stored at address 2262

No comments:

Post a Comment