Sunday, February 25, 2018

C program : Create a database using Structure

Create a database of students using structures, where in each entry of the database will have the following fields:
  1. a name, which is a string with at most 128 characters
  2. their marks in physics which is an int between 0 and 100
  3. their marks in chemistry which is an int number between 0 and 100
  4. their marks in mathematics which is an int number between 0 and 100
You have to output a list of students in the following order.
  1. if a student 'A' has lower marks in physics than a student 'B', then A's data is listed before B.
  2. If A and B have the same physics marks and A has lower chemistry marks than B, then A is listed before B.
  3. If A and B have the same marks in physics and chemistry, and A has lower marks in mathematics than B, then A is listed before B.
  4. If all marks are equal and A's name precedes B's name in the dictionary order, then A is listed before B.
Input Format :First line contains the number of students n, where 1<=n<=100.
In following n lines each line contains(space separated)  a name and their respective marks in physics, chemistry, maths, where 0<=marks<=100.

Output Format :Sorted database of n lines.


 #include<stdio.h>
#include<string.h>
struct stu
{
  char name[128];
  int p,c,m;
};
void main()
{
  int i=0,n=0,j=0;
  scanf("%d",&n);
  struct stu db[n],temp;
  if(n==1)
  {
    scanf("%s%d%d%d",db[0].name,&db[0].p,&db[0].c,&db[0].m);
    printf("%s\t%d\t%d\t%d",db[0].name,db[0].p,db[0].c,db[0].m);
  }
  else
{

  for(i=0;i<n;i++)
  {scanf("%s%d%d%d",db[i].name,&db[i].p,&db[i].c,&db[i].m);}
  for(i=0;i<n;++i)
  {
    for(j=i+1;j<n;++j)
    {

       if(db[i].p>db[j].p)
          {
                  temp=db[i];
                  db[i]=db[j];
                  db[j]=temp;
           }

          else if(db[i].p==db[j].p && db[i].c>db[j].c)
            {
                temp=db[i];
                db[i]=db[j];
                db[j]=temp;
              }

          else if(db[i].p==db[j].p && db[i].c==db[j].c && db[i].m>db[j].m)
        {
            temp=db[i];
            db[i]=db[j];
            db[j]=temp;
        }


      else  if(db[i].p==db[j].p && db[i].c==db[j].c && db[i].m==db[j].m)
        {

                  if(strcmp(db[i].name,db[j].name)>0)
                    {
                      temp=db[i];
                      db[i]=db[j];
                      db[j]=temp;
                    }

          }


    }
  }
  for(i=0;i<n-1;i++)
  {printf("%s\t%d\t%d\t%d\n",db[i].name,db[i].p,db[i].c,db[i].m);}
    printf("%s\t%d\t%d\t%d",db[n-1].name,db[n-1].p,db[n-1].c,db[n-1].m);
}
}

C Recursive Program which outputs the line with the characters reversed.

Example:
INPUT
This is easy

OUTPUT
ysae si sihT

 #include<stdio.h>
void reverse()
{
int c = getchar();
  if (c != EOF) {
    reverse();
    putchar(c);
  }
}

int main()
{
 
  reverse();
  return 0;
}

Thanks
Happy Computing !

C program : The smallest quadrant index


Consider a matrix M of integers. Divide M into 4 sub-matrices. These sub-matrices are called as Quadrants. Report the Quadrant number which has the smallest minimum-element. If two or more quadrants have same smallest minimum, report the smallest quadrant index. The matrix M is divided into four quadrants by halving the rows and columns. If row/column is an odd number, divide them in such a way that the first half of the row/column should be one smaller than the second half.
The four quadrants are numbered from 1 to 4 in the structure shown below: Q1 | Q2
---+---
 Q3 | Q4
INPUT FORMAT: M is a matrix of integers. You would be given two numbers m and n specifying the number of rows and columns. This would be followed by m lines of n integers each specifying the data of the m*n matrix M.
n and m will be greater than 1 and less than 12. OUTPUT FORMAT: Print in a separate line, the quadrant number with the smallest minimum-element. #include<stdio.h>
void main()
{
int a[12][12],m,n,i,j,p,q,min,row=0,col=0;
scanf("%d%d",&m,&n);

for(p=0;p<m;p++)
{

for(q=0;q<n;q++)
{
scanf("%d",&a[p][q]);

}
}
min = a[0][0];

for(i=0;i<m;i++)
{

for(j=0;j<n;j++)
{

if(min>a[i][j])
{
min=a[i][j];
row=i;
col=j;
}
}
}
if((row<m/2)&&(col<n/2))
printf("1");
else if((row<m/2)&&(col>=n/2))
printf("2");
else if((row>=m/2)&&(col<n/2))
printf("3");
else if((row>=m/2)&&(col>=n/2))
printf("4");
}
 


Thanks


Happy Computing ! 

C program : The Collatz function

The Collatz function is defined for a positive integer n as follows.
f(n) = 3n+1 if n is odd
        n/2     if n is even

We consider the repeated application of the Collatz function starting with a given integer n, as follows:
f(n), f(f(n)), f(f(f(n))), …

It is conjectured that no matter which positive integer n you start from, this sequence eventually will have 1 in it. It has been verified to hold for numbers up to 5 × 260

You will be given a positive number <= 32,000. You have to output how many times f has to be applied repeatedly in order to first reach 1.

 #include<stdio.h>
int main()
{

  int n,y=0,count=0;
  scanf("%d",&n);
  if(n==1)
  {printf("0");
  }
  else
  {
  while(y!=1)
  {
      if(n%2==0)
      {
        y=n/2;
        n=y;
      }
      else
      {
         y=3*n+1;
        n=y;
      }
    count++;
  }
  printf("%d",count);
  }
  return 0;
  }


Thanks
Happy Computing ! 

C program : That replaces the occurence of a given character (say c) in a primary string (say PS) with another string (say s).

Input:
The first line contains the primary string (PS)
The next line contains a character (c)
The next line contains a string (s)

Output:
Print the string PS with every occurence of c replaced by s.
 
 
 #include<stdio.h>
#include<string.h>

int main()
{
    char PS[1000],s[1000],ch;

    scanf("%s %c %s", PS,&ch,s);

    for(int i=0;i<strlen(PS);i++)
        {
            if(PS[i]==ch)
                {
                    for(int i=0;i<strlen(s);i++)
                        printf("%c",s[i]);
                }
            else
                printf("%c",PS[i]);
        }


    return 0;
}

Thanks
Happy Computing!

C program : Given two arrays of integers output the smallest number in the first array not present in the second one.

Input Specification: 
The first line contains the size N1 of the first array.
Next line give the contents of the first array.
Next line contains the size N2 of the second array.
Next line give the contents of the second array. 
 
Output Format:
Output must be a single number which is the smallest number occurring
in the first array that does not occur in the second. In case there is
no such number, output NO.
 
 #include<stdio.h>
# define MAX_SIZE 20

int main(){
int a[MAX_SIZE], b[MAX_SIZE],comn[MAX_SIZE];
int na,nb,count=0,g=0;
scanf("%d",&na);
if(na>0)
{
for(int i=0;i<na;i++)
{

    scanf("%d ",&a[i]);

}
scanf("%d",&nb);
for(int j=0;j<nb;j++)
{

    scanf("%d ",&b[j]);

}

for(int k=0;k<na;k++){

    for(int l=0;l<nb;l++)
    {
        if(a[k]==b[l])
        {
            count++;
        }


    }
    if(count==0)
    {
        comn[g]=a[k];
        g+=1;
    }
    if(count>=1)
    {
        ;
    }
    count=0;
}
int smallest=comn[0];
for(int j=0;j<g;j++)
{
    if (comn[j]<smallest)
    {
        smallest = comn[j];

    }

}
if(g!=0)
{
    printf("%d", smallest);
}
else
{
    printf("NO");
}
}
else
{
    printf("NO");
}

return 0;
}
 
Thanks
Happy Computing ! 
 

C program : To count the number of words in the sentence.

A line of English text will be given, where words are separated by one
of the following symbols:

 ' ', '\t', '.', ',' and ';'
 
You have to count the number of words
in the sentence.
 
 
 #include<stdio.h>

int main(){
  int  foundLetter = 0,count= -1,c;
 
    c = getchar();
 
    while ( c != EOF )
    {
       c = getchar();
     
      if(c == ' '||c =='.'||c =='\t'||c ==';'||c ==',')
      {foundLetter=0;}
      else
      {
          if (foundLetter == 0)
          count++;
          foundLetter = 1;
      }
    
      
    }
    printf("%d",count);
}
 
Thanks
Happy Computing !

What is a superconductor?

 A superconductor is a material that can conduct electricity with zero electrical resistance when it is cooled below a specific, very cold t...