Thursday, 26 December 2019

Use of Functions in programming languages

For example:

an Account has account_no, balance;
if person has withdrawn some amount then balance = balance - amount;
after withdraw if we want to print the account_no and balance

you have to write print account_no, balance

again if we deposit amount

balance = balance + amount;

you have to write print account_no, balance

again and again we have to print account_no and balance after every withdrawal and deposit

so we can put display() and write account_no and balance in that method display()
and call display() inside withdraw() and deposit()
int acno,bal;
void withdraw(int amt)
{
        bal=bal-amt;
}
void deposit(int amt)
{
        bal=bal-amt;
}
void display()
{
      printf("acc number = %d and Balance = %d", acno, bal);
}
int main()
{
       printf("Press 1. for withdraw 2. for deposit 3. display 4. exit");
       int choice;
       scanf("%d",&choice);
       switch(choice)
      {
             case 1:
                          printf("enter amount to withdraw");
                          int amt;
                          scanf("%d",&amt);
                          withdraw(amt);
                          display();
                          break;
           case 2:
                          printf("enter amount to deposit");
                          int amt;
                          scanf("%d",&amt);
                          deposit(amt);
                          display();
                          break;
           case 3:
                       display();
                       break;
           case 4: exit(0);
     }
}


Saturday, 21 December 2019

String of concatenation, rotating the matrix, turning binary matrix

string concatenation
A string is called a pString if it can be represented as p concatenated copies of some string. For example, the string "aabaabaabaab" is at the same time a 1String, a 2String and a 4String, but it is not a 3String, a 5String, or a 6String and so on. Obviously any string is a 1String.
You are given a string s, consisting of lowercase English letters and a positive integer p. Your task is to find if it is possible to reorder the letters in the string s in such a way that the resulting string is a pString.
Input Format
The first input line contains integer p.
The second line contains s, all characters in s are lowercase English letters.
Constraints
1 ≤ p ≤ 1000
1 ≤ |s| ≤ 1000
Output Format
Print "YES" if it is possible to rearrange the letters in string s in such a way that the result is a pString. Print the result on a single output line. If it is not possible print "NO". (without quotes).
Sample Input 0
2
aazz
Sample Output 0
YES
Explanation 0
aazz can be rearranged to azaz which is a 2String

int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */  
    int n;
    scanf("%d",&n);
    char s[1010];
    scanf("%s",s);
    int h[26]={0},i;
    for(i=0;s[i]!='\0';i++)
        h[s[i]-'a']++;
    for(i=0;i<26;i++)
    {
        if(h[i]!=0 )
        {
            if(h[i]%n != 0)
            {
                printf("NO\n");
            return 0;
            }
        }
           
        }
    printf("YES");
    return 0;
}

=============
rotating the matrix
Thomas, a computer programmer, is led to fight an underground war against powerful computers who now rule the world with a system called 'The Matrix'.
To win against the system, he must rotate the Matrix clockwise by an angle of 90 degrees.
Unfortunately the system has wiped Thomas' memory and it is now in your hands to save the world.
The matrix can be considered as a 2Dimensional Array of size NxN.
SEE THE SAMPLE INPUT OUTPUT FOR CLARITY
Input Format
The first line contains the size of the matrix N.
The next N lines contain N integers each, together denoting the square matrix of size NxN.
Constraints
1 <= N <= 10
Output Format
Output the final matrix after rotation.
Sample Input 0
2
1 2
3 4
Sample Output 0
3 1
4 2

int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    int n;
    scanf("%d",&n);
    int a[n][n],b[n][n],i,j;
    for(i=0;i<n;i++)
        for(j=0;j<n;j++)
            scanf("%d",&a[i][j]);
    for(i=0;i<n;i++)
        for(j=0;j<n;j++)
            b[i][j]=a[n-j-1][i];
    for(i=0;i<n;i++){
        for(j=0;j<n;j++)
            printf("%d ",b[i][j]);
        printf("\n");
    }
    return 0;
}
Turning binary matrix


Consider a binary matrix A of size N X N. Now, consider the following matrices : A90 - obtained by rotating A clockwise by 90 degrees. A180 - obtained by rotating A clockwise by 180 degrees. A270 - obtained by rotating A clockwise by 270 degrees.
Note : Binary matrix implies that every element will be either 0 or 1.
Your task is to construct another binary matrix B of size N X N such that : B(i,j) = 1 iff either A(i,j) = 1 OR A90(i,j) = 1 OR A180(i,j) = 1 OR A270(i,j) = 1 B(i,j) = 0 otherwise
INPUT
First line contains the size of the matrix N (1 ≤ N ≤ 100) Next N lines contain N integers each (Only 0 or 1) denoting the matrix A
OUTPUT
Print N X N integers, denoting the matrix B.
Sample Input 0
4
0 0 0 0
0 0 0 0
0 0 1 0
1 0 0 0
Sample Output 0
1 0 0 1
0 1 1 0
0 1 1 0
1 0 0 1
int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    int n;
    scanf("%d",&n);
    int a[n][n],b[n][n],i,j;
    for(i=0;i<n;i++)
        for(j=0;j<n;j++)
            scanf("%d",&a[i][j]);
    for(i=0;i<n;i++)
        for(j=0;j<n;j++)
            b[i][j]=a[i][j]|a[n-j-1][i]|a[n-i-1][n-j-1]|a[j][n-i-1];//for 360,90,180,270 rotations
    for(i=0;i<n;i++){
        for(j=0;j<n;j++)
            printf("%d ",b[i][j]);
        printf("\n");
    }
    return 0;
}