Tampilkan postingan dengan label C++. Tampilkan semua postingan
Tampilkan postingan dengan label C++. Tampilkan semua postingan

Rabu, 09 Desember 2015

Making Pyramid with C/C++

#include<stdio.h>
#include<conio.h>

void main()
{
int i = 0, j , n ;
clrscr();
printf("How many rows in pyramid:");
scanf("%d",&n);

while(i<n)
{
j=0;
while(j<=i)
{
gotoxy(30+j,10+i);
printf("%d",j);
j++;
}
j=0;
while(j<=i)
{
gotoxy(30-j,10+i);
printf("%d",j);
j++;
}
i++;
printf("\n");
}

getch();
}

Remarks
For making two sides pyramid we have used two while loops and another while loop for controlling rows. Here gotoxy () function takes the curser to the specified location.

Selasa, 08 Desember 2015

Sorting and Searching Program with C/C++ Language

Sorting and Searching is an important task in programming. Here we will see such a C Program. This program is used to sorting and searching a string array. Running this program we will provide names and given names are sorted and searched by the program. Programmingcode is given bellow :

#include<stdio.h>  
#include<conio.h>
#include<string.h>

  struct name            // Custom Data Type
  {
      char a[10];  
      
   }b [10];                 // Custom data type’s variable

void main()
{

  int N,i,j;
  int c=0;
  char name[10];
  clrscr();
  printf("How many names :");
  scanf("%d",&N);  fflush(stdin);
  for( i=0;i<N;i++)
  {
   printf("Enter %d Name :",i+1);
   gets(b[i].a);              
  }

  printf("\n\nEntered name list :");
  for(i=0;i<N;i++)
  {
            printf("\n%s",b[i].a);
  }

  for(i=0;i<N;i++)
  {
            for(j=i+1;j<N;j++)
            {
             if((strcmp(b[i].a,b[j].a))>0)
               char temp[10];
               strcpy( temp,b[i].a);
               strcpy(b[i].a,b[j].a);
               strcpy( b[j].a,temp);
             }
   }
  }
  printf("\n\nSorted name list :");
  for(i=0;i<N;i++)
   {
             printf("\n%s",b[i].a);
   }



 printf("\n\nEnter name to search :");
 gets(name);
 for(i=0;i<N;i++)
 {
   if((strcmp(b[i].a,name))==0)
   c++;
 }
 if(c==0)
 {
   printf("%s not found .",name);
 }
 else
 {
   printf("%s remains %d times .",name,c);
 }
 getch();
 }

Remarks

After giving name (string) we can see the list of name and then sorted list. When we will give a name and search, we will see that name if it is in array otherwise name not found on the screen. Here strcpy () function is used to copy one variable’s value another variable and strcmp()function is used to compare two string. fflush(stdin) is used to flush buffer.

Jumat, 23 Januari 2015

How to sort an array’s elements

Here we have used Bubble Sort algorithm. First we will take the array’s elements. For this we have used scanf() function. Then we will sort the array’s elements using Bubble Sort Algorithm.


#include<stdio.h>
#include<conio.h>
  void main()
  {
            int a[20],N;
            clrscr();

            printf("How many item:");
            scanf("%d",&N);
            printf("Enter items :\n");
            for(int i=0;i<N;i++)   // Taking array’s elements
            {
                  scanf("%d",&a[i]);
            }
            printf("Array is :");   // Showing  array’s elements
            for(i=0;i<N;i++)
            {
                  printf("%d  ",a[i]);
            }
// sorting array’s elements
            for(i=0;i<N;i++) 
            {
              int ptr=0;
              while(ptr<N-1-i)
              {
                if(a[ptr]>a[ptr+1])
                 {
                         int temp=a[ptr];
                         a[ptr]=a[ptr+1];
                         a[ptr+1]=temp;
                 }
                        ptr=ptr+1;
              }
            }

            printf("\nSorted array :");  // Showing sorted array’s elements
            for(i=0;i<N;i++)
            {
                   printf("%d  ",a[i]);
            }
   getch ();
  }