C& CPP Tutorials
About C
Program Structure
Running C Program
Variables, constants
Operators
Control Structure
Array
Function
Pointers
Preprocessor
Structure
File
Bitwise Operators
Recursion
 Data Structures
Introduction
Stacks
Queues
Linked List
Sorting 
Searching
 Test Your Skill
Fundamentals
Input and Output
Branching and Looping
Function
Pointers I
Pointers II
Structure and Union
Sample Problems I
Sample Problems II
 Help and Support
C Forum
Source code
C  Yahoo Group
C Compilers
 

Solutions : Pointers - Part II

[Q001] 

 /* Print Freshersworld on each line, forming a diagonal pattern */
 #include<stdio.h>
 #include<conio.h>
 #include<dos.h>
  void main()
    {
       char str[]="Freshersworld";
       int x=1,y;
       for (y=1; y<=25; y++) 
         {
             clrscr();
             gotoxy(x,y);
             printf("%s",str);
             delay(250);
               if (y < 8)
                  x+=2;
               else
                  x+=3;
         }
} /* End of main */

 [Q002] 

 

[Q003] 

 /* Compute the sum of values in the variable argument list */
  #include<stdio.h>
  #include<stdarg.h>
    int Sum(int a,...)
      {
        int total=a;
        va_list ap;
        int arg;
        va_start(ap,a);
    while ((arg = va_arg(ap,int)) != 0)
        {
          total+=arg;
        }
        va_end(ap);
        return total;
     }
     void main()
      {
        clrscr();
        printf("%d",Sum(5,6,7,8,9,10));
        getch();
   } /* End of main */

[Q004]

// Compute the sum of two values using function and return the result w/o using RETURN statement
#include<stdio.h>
#include<conio.h>
 int Sum(int a,int b)
  {
    _AX = a + b; // equivalent to : return (a + b);
  }
  void main(void)
   {
     clrscr();
     printf("Result is = %d",Sum(4,6));
     getch();
   } /* End of main */

[Q005] 

/* To swap the contents of two variables using 'asm' statement */
#include<stdio.h>
#include<conio.h>
void main()
 {
  int val=85,num=77;
  clrscr();
  printf("Before swapping : %d and %d",val,num);
  asm mov ax,val
  asm mov cx,num
  asm mov num,ax
  asm mov val,cx
  printf("\n\nAfter swapping : %d and %d",val,num);
 } /* End of main */

[Q006] 

 /* To find largest of two numbers using 'asm' statement */
#include<stdio.h>
#include<conio.h>
void main()
  {
   int val=85,num=77;
   clrscr();
   asm mov ax,val
   asm cmp ax,num
   asm jg FirstNum
   asm mov ax,num
   FirstNum:printf("Largest of two numbers is %d",_AX);
 } /* End of main */

[Q007] 

 (a)


/* To print the above pattern or output using asm statement */
#include<stdio.h>
#include<conio.h>
 void main(void)
  {
    int i=5,j;
    clrscr();
    // asm statement doesn't end with semicolon (starts with 'asm' keyword)
    asm mov ax,i // Move the value of 'i' to accumulator (ax) 
    Loopi: j=i;
    Loopj: printf("*"); // Print asterisk (*)
    j=j-1;
    asm mov cx,j // Decrement the counter : Inner Loop
    asm cmp cx,0
    asm jnz Loopj // Terminate the Inner Loop if counter is 0, otherwise goto Loopj
    i=i-1;
    printf("\n"); 
    asm mov ax,i // Decrement the counter : Outer Loop
    asm cmp ax,0
    asm jnz Loopi // Terminate the Outer Loop if counter is 0, otherwise goto Loopi
    getch();
 } /* End of main */

*****
****
***
**
*

(b)


/* To print the above pattern or output using asm statement */
  #include<stdio.h>
  #include<conio.h>
  void main(void)
   {
     int i=1,j;
     clrscr();
      // asm statement doesn't end with semicolon (starts with 'asm' keyword)
      asm mov ax,i // Move the value of 'i' to accumulator (ax) 
      Loopi:j=0;
      Loopj:printf("*"); // Print asterisk (*)
      j=j+1;
      asm mov cx,j // Increment the counter : Inner Loop
      asm cmp cx,i 
      asm jnz Loopj // Terminate the Inner Loop if Inner Loop counter = Outer Loop counter
      i=i+1;
      printf("\n");
      asm mov ax,i // Increment the counter : Outer Loop
      asm cmp ax,6
      asm jnz Loopi // Terminate the Outer Loop if counter = 6
      getch();
   } /* End of main */

   [HINT : USE 'asm' statement]

*
**
***
****
*****

[Q008] 

/* To reverse the words in a given line of text */
 #include<stdio.h>
 #include<string.h>
  void main()
    {
      char ch,str[80],*res="",*temp,*temp1;
      clrscr();
      printf("Enter a sentence :\n");
      scanf("%[^\n]",str);
      temp=strtok(str," ");
      do
      {
         strcpy(temp1,temp);
         strcat(temp1," ");
         strcat(temp1,res);
         strcpy(res,temp1);
         temp=strtok(NULL," ");
      }while (temp != NULL);

      printf("\n Result : %s",res);
      getch();
    } /* End of main */

[Q009] 

 

[Q010] 

/* To reverse a given line of test using recursive function */
 #include<stdio.h>
 #include<conio.h>
 void reverse(void)
  {
    char c;
    if ((c = getchar()) != '\n')
    reverse();
    putchar(c);
    return;
  }
  void main()
   {
     clrscr();
     printf("Please enter a line of text below\n");
     reverse();
   } /* End of main */

Back
 

Google