Thursday, May 1, 2014

Frequency of number in an array

Program to find number of times a given integer value has occurred in an array of integers.

let array a[]= {6,1,6,3,5,6,2,2,1,3,6,2}
output:
1 -- 2
2 -- 3
3 -- 2
5 -- 1
6 -- 4

The simple solution to the problem is to increment array value at a particular index where index value is equal to the number.

Program

#include<stdio.h>
int main()
{
       int a[]={6,1,2,5,6,3,2,2,2,1,4,2,8,4,3}; // size of a =15
       int b[10]; // size 10 is sufficient because maximum number in above array is 9
       int i;

       //assigning all elements of array b to 0    
       for(i=0;i<10;i++)
            b[i]=0;

       for(i=0;i<15;i++)
            b[a[i]]++;

       for(i=0;i<10;i++)
       {
           if(b[i]!=0)
              printf("%d -- %d\n",i,b[i]);
        }
       return 0;
}



     

Wednesday, April 30, 2014

histogram printing

Program to print a histogram pattern for a given array.
For example 
Array   =     4 5 3 2
pattern 
4 5 3 2
   *
* *
* * *
* * * *
* * * *

Outer loop: depends on the maximum element in the array.
The very basic technique to find the maximum element is to assign first element of array as maximum and then subsequently checking it with other array elements and changing the maximum element appropriately.

Inner loop: controls the printing and also depends on maximum element. Find the maximum element and compare it with elements. If the given element is greater than the max element than print "*" otherwise print a space. Decrement the maximum element for a full completion of inner loop. 
                      
Program:

#include<stdio.h>
int main()
{
int a[]={5,7,3,6,4,8,2};
int size=7;
int max=a[0];
int i,j,temp;

for(i=1;i<size;i++)
{
if(max<a[i])
max=a[i];
}

for(i=0;i<size;i++)
     printf("%d ",a[i]);

printf("\n");

temp=max;

for(i=0;i<max;i++)
{
for(j=0;j<size;j++)
{
if(a[j]>=temp)
printf("* ");
else
printf("  ");
        }
temp--;
printf("\n");
}

return 0;
}




Wednesday, March 5, 2014

Printing Pattern 10

      1
    121
  12321
1234321
------------- for n values

The above pattern can be divided into two parts

        1             
       12             1
     123      +     21
   1234             321

outer loop i should run n times.
inner loop comprises of 2 loop variable for printing 1st and 2nd half of the pattern.
Number of spaces printed depends on number of values in that line
Maximum value in a line is the line number.



Outer loop
for(i=1;i<=n;i++)
{ ... }



Inner loop for spaces
Number of spaces = max lines in pattern - Values printed in that line

for(j=1;j<=n-i;j++)
printf(" ");

here 'n' number of lines in pattern which user enters and also the maximum value that appears in the pattern.

'i' is the line number of that line or number of values printed in that line.



Inner loop for 1st pattern
starts with 1 and goes till line number value for that line or max value of that line.

for(k=1;k<=i;k++)
printf("%d",k);






Inner loop for 2nd pattern

start with maximum value of that line -1 and decrements down to 1.

for(t=i-1;t>=1;t--)
printf("%d",t);




Program

#include<stdio.h>

int main()
{
int i,j,k,t,n;

//ask user to enter number of lines
printf("\nEnter the number of lines in the pattern:");
scanf("%d",&n);

//outer loop
for(i=1;i<=n;i++)
{

//loop for spaces 
        for(j=1;j<=n-i;j++)
              printf(" ");

//loop for 1st part of pattern
        for(k=1;k<=i;k++)
              printf("%d",k);

//loop for 2nd part of pattern
        for(t=i-1;t>=1;t--)
              printf("%d",l);

//move to next line
         printf("\n");
}

return 0;
}






Thursday, January 23, 2014

Printing Pattern 9

1
01
101
0101
10101

Alternating between 1 and 0. Hmm! We can use number%2 that will always be 1 or 0. % means remainder division(or modular division) and gives remainder when number is divided by another number. When a number is divided by number 'n', remainder is always between 0 and n-1. So in case of division with 2, remainder is between 0 to 1 (n-1).
0 and 1 keeps on alternating in each line as well as for the next line. How about making it dependent on line both i and j say sum of i and j

line 1 element 1 -- i=1,j=1, i+j=2, 2%2=0 but we want a 1 instead. So add 1 to i+j and then mod it will 2.
Then i (outer loop variable) loops 5 times and j (inner loop variable) loops from 1 till i for each line and then goes to the next line.

i     j    i+j+1     i+j+1%2
1    1      3              1
next line
2    1      4              0
2    2      5              1
next line
3    1      5              1
3    2      6              0
3    3      7              1
next line
and so on.

#include<stdio.h>
int main()
{
     int i,j;

    for(i=1;i<=5;i++)
    {
          for(j=1;j<=i;j++)
                 printf("%d",(i+j+1)%2);

          printf("\n");
    }

    return 0;
}

 

Printing pattern 8

1
2   7
3   8 12
4   9 13 16
5 10 14 17 19
6 11 15 18 20 21

Seeing the pattern we can figure our that 1st element of each line is the value of line no. that is dependent on i (outer loop variable)

Some further look into the pattern reveals the following
1
2 7(2+5)
3 8(3+5) 12(8+4)
4 9(4+5) 13(9+4) 16(13+3)
5 10(5+5) 14 (10+4) 17(14+3) 19(17+2)
and so on.

#include<stdio.h>
int main()
{
    int i,j,fix,curval=0;

    for(i=1;i<=6;i++)
    {      
        fix=5;
        curval=i+fix;
        for(j=1;j<=i;j++)
        {
            if(j==1)
                printf("%2d ",i);

            if(j>1)
            {
                printf("%2d ",curval);
                fix--;
                curval=curval+fix;
            }
        }
        printf("\n");
    }
    return 0;
}

Printing pattern 7

1
1*2
1*2*3
1*2*3*4

i used as outer loop variable for printing no. of lines=4
j used to control printing * and nos. in each line.
In this pattern, we can figure out that star does not get printed when value of i equals value of j.

#include<stdio.h>
int main()
{
         int i,j;

         for(i=1;i<=4;i++)
         {
                 for(j=1;j<=i;j++)
                 {
                           printf("%d",j);

                           if(i!=j)
                                 printf("*");
                 }

                  printf("\n");
            }

            return 0;
}

Saturday, January 11, 2014

Printing Pattern 6

1
3 2
6 5 4
10 9 8 7

Outer Loop : 4 Times

Inner Loop: depend on value of Outer Loop

Value in each line is maximum that line should print in reverse order

1
2 3
4 5 6
7 8 9 10

If each line is above is reversed we get desired pattern

We must start with a maximum value of the line and then it should then be decremented.
What is the maximum value for the line?
Looking at the pattern --
Maximum value        line no.             Value of i + previous Max value
1                                    1                              1+ 0 = 1
3                                    2                              1+ 2 =3
6                                    3                              3+ 3 =6
10                                  4                              4+ 6 =10

#include<stdio.h>
int main()
{
    int i,j,max_val,print_val;

    max_val=0;
    for(i=1;i<=4;i++)
    {
        max_val=max_val+i;
        print_val=max_val;
        for(j=1;j<=i;j++)
        {
            printf("%d ",print_val);
            print_val--;
        }
        printf("\n");
    }
    return 0;
}


Printing pattern-5

1
23
456
78910

This pattern is very much similar to the pattern which is earlier discussed.

Outer Loop controls no. of lines to be printed which are 4.
Inner Loop controls the digits printed in each line.
In line no.1 we have only 1 digit printed.
In line no.2 we have 2 digits printed.
and so on.
Next digit is increment of previous digits, so we take a counter to keep count of next digit to be printed.

Outer Loop
for(i=1;i<=4;i++)

Inner Loop
for(j=1;j<=i;j++)

#include<stdio.h>
int main()
{
       int i,j count;
       count=1;

       for(i=1;i<=4;i++)
      {
             for(j=1;j<=i;j++)
            {
                   printf("%d",count);
                   count++;
            }
            printf("\n");
      }
   return 0;
 }



Friday, January 10, 2014

Printing Pattern 4( 2 Right angle triangles )

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

The pattern can seen as two right angle triangles previously discussed in  the blog.
Outer loop ( for no. of lines:7) will loop 7 times.
Inner loop should 1st increase the no. of * in pattern up till 4 lines and then decrease them.

Outer loop
for(i=1;i<=7;i++)

1st Inner loop (increasing stars upto line no.4)
  for(j=1;j<=i;j++)
                 printf("*");
For 1st loop (i=1)
j=1 , j<= i yes print *
newline

For 2nd loop(i=2)
j=1, j<=i yes print *
j=2, j<=i yes print *
newline
and so on for i= 3 and 4 prints *** and ****

 2nd Inner loop(decreasing stars after line no.4)
 for(j=i;j<=7;j++)
                 printf("*");

For 5th loop (i=5)
j=5(i) , j<= 7 yes print *
j=6(i+1) , j<= 7 yes print *
j=7(i+2),  j<=7 yes print *
newline 

For 6th loop(i=6)
j=6(i), j<=7 yes print *
j=2(i+1), j<=7 yes print *
newline 

for 7th loop (i=7)
j=7(i) , j<=7 yes print *
newline

An  if else loop to check the line no.
 if(i<=4)
{
1st Inner loop
}
else
{
2nd Inner loop
}
 
#include<stdio.h>
int main()
{
for(i=1;i<=7;i++)
     {
        if(i<=4)
        {
           for(j=1;j<=i;j++)
                 printf("*");
        }

        else
       {
           for(j=i;j<=7;j++)
                 printf("*");
        }

    printf("\n");
  }
  return 0;

}



Thursday, January 9, 2014

Printing Pattern 3

      4
    34
  234
1234



For this pattern we have to take care about three things
  1. We need to print 4 lines
  2. We need to print required spaces in lines
  3. We need to print the values.


The pattern requires three loops
  1. An outer loop to loop 4 times.
  2. An inner loop to control the no. of spaces needed to be printed in each line.
  3. An inner loop to print certain after spaces in each line.
In line no.1 we need to print 3 spaces and value is 4
In line no.2 we need to print 2 spaces and value 3 and 4.

So we see that no. of spaces in each line decrease and finally become zero.
The number of digits in each line increases.
we can also see that spaces + digits in each line are 4 and remain 4 for all the lines

Line No.             No. of Spaces         No. of Values
1                                    3                           1                       (3+1=4)
2                                    2                           2                       (2+2=4)
3                                    1                           3                       (3+1=4)
4                                    0                           4                       (4+0=4)


Outer loop variable i should iterate 4 times. As we need first digit in first line 4 we can start i=4 and then decrement it and make j dependent on i. (although the same pattern can be printed in number of ways)
for(i=4;i>=1;i--)

first inner loop is controlling the number of spaces in each line. Its number of times it iterates should reduces. As we noted that no. of digits + no. of spaces is constant = 4 for this pattern, we can make this loop dependent on i also. As i starts from 4 and number of spaces should be 3 for 1st line space =4-1 each time and decrement till 1 or the other way start from 1 and go upto i-1. For line 2 i's value will become 3 and 3-1 will become 2 and we need tw spaces to be printed as desired.
for(j=i-1;j>=1;j--)
printf(" ");

for printing value we need a another loop. For first line this loop shall only print 4. Then for second line it should print 3 and 4. We can recognize that this loop starts from i's value and go till 4 for each line because each line is printing as its last value.
for(k=i;k<=4;k++)
printf("%d",&k);

#include<stdio.h>

int main()
{
       int i, j, k;
      
       for(i=4;i>=1;i--)
       {
               for(j=i-1;j>=1;j--)
                    {
                          printf(" ");
                     }
              
               for(k=i;k<=4;k++)
                     {
                          printf("%d",k);
                      }
     
              printf("\n");
       }
        
        return 0;
     
}