Interviews Questions, Algorithms, Aptitude, C Interview Program, C Theory Question, Aptitude Tricks, Test Series,

Showing posts with label IOPP SEM END EXAM. Show all posts
Showing posts with label IOPP SEM END EXAM. Show all posts

Sunday, 5 May 2019

IOPP PROGRAM 8 A

Question:
Solution:

\documentclass[12pt]{article}

\begin{document}

\fbox{\begin{minipage}{30em}

List Of Mathematical Functions:\\

\begin{itemize}

      \item Trignometrical Functions

\begin{enumerate}

    \item sine

\begin{enumerate}

\item[$\star$]sine \textendash a

\item[$\star$]sine -b

\end{enumerate}

\item cosine

\begin{enumerate}

\item[$\textendash$] cosine \textendash c

\item[$\textendash$] cosine -d

    \end{enumerate}

\item tangent

\end{enumerate}

  \item Special Functions:\\

 \renewcommand{\labelenumi}{\alph{enumi})  }

 \begin{enumerate}

       \item Beta Function

       \item Gamma Function

       \item Reiman Zeta Function

\end{enumerate}

\end{itemize}

\end{minipage} }


\end{document}


Credits: Aditya

Friday, 3 May 2019

IOPP Program 5 A

Question:

Write an OpenMP program (for shared memory multiprocessor architecture) to add two matrices. Display the appropriate environment variables to demonstrate parallel programming.

Solution:

#include<stdio.h>
#include<stdlib.h>
#include<omp.h>


int main(int argc,char **argv)
{
        int a[10][10],b[10][10],c[10][10],n;
        int i,tid,nthreads,chunk=10,j;
printf("Enter the size of Matrix\n");
scanf("%d",&n);
        printf("Enter elements of 1st array :\n");
        for(i=0;i<n;i++)
for(j=0;j<n;j++){
                scanf("%d",&a[i][j]);
}
        printf("Enter elements of 2nd array :\n");
        for(i=0;i<n;i++)
for(j=0;j<n;j++){
                scanf("%d",&b[i][j]);
}

        #pragma omp parallel shared(a,b,c,nthreads,chunk) private(tid,i)
        {
                tid = omp_get_thread_num();
                if(tid==0)
                {
                        nthreads=omp_get_num_threads();
                        printf("Starting matrix addition example with %d threads.\n",nthreads);
                }
        printf("Thread %d starting matrix addition...\n", tid);
        #pragma omp parallel for schedule(static,chunk)
        for(i=0;i<n;i++) {
for(j=0;j<n;j++){
                c[i][j]=a[i][j]+b[i][j];
        }
}
        printf("Thread %d ending matrix addition...\n", tid);
        }
        printf("Resultant array :\n");
               for(i=0;i<n;i++){
for(j=0;j<n;j++){
                printf("%d ",c[i][j]);
}
printf("\n");
}
}

IOPP Program 6 B

Question:

Write a Perl script to list the contents of a directory.

Solution:

#!/usr/bin/perl
    my $directory ='.';

    opendir (DIR, $directory) or die $!;
 while (my $file = readdir(DIR)) {

        print "$file\n";

    }
   closedir(DIR);

IOPP Program 4 B

Question:
Write a shell script to search a filename in the current working directory

Solution: 

echo "Enter the filename that u want to search"

read filename
 

c=`pwd`
 

x=`find $c -name "$filename"`
 

y=$c/$filename
 

if [ "$x" = "$y" ]
 

then
 

     echo "Found"
 

else
 

     echo "Not Found"

fi


Credits: Aditya

IOPP Program 2 B

Question:
Write a shell script to find the GCD and LCM of two numbers. 

Solution:
echo "Enter the first number:"
read a
echo "Enter the second number : "
read b

if [ $a -gt $b ]
then
num=$a
den=$b
else
num=$b
den=$a
fi
r=`expr $num % $den`

while [ $r -ne 0 ]
do
num=$den
den=$r
r=`expr $num % $den`
done

gcd=$den
lcm=`expr $a \* $b / $gcd`

echo " The LCM of $a and $b is : $lcm"
echo " The GCD of $a and $b is : $gcd"

IOPP Program 1B

Question:
 Write a shell script to determine the number of lines in a given file (without using wc command). 

SOLUTION:

echo "Enter the Filename"
read filename
cline=0

while read  c
do
if [ "$c"=="\n" ]
then
cline=$(( $cline + 1 ))
fi
done < "$filename"
echo "Number of Lines: $cline"

Sunday, 28 April 2019

IOPP Program No.7. A

Program No.7. A

 Create a LATEX document to demonstrate the paragraph justification (left, center, right) and creating the tables. 

Solution:

\documentclass{article}
\begin{document}
Hi all... I'm new to Perl.

\flushleft Hi, all... I'm new to Perl. 

\center Hi, all... I'm new to Perl.

\flushright Hi, all... I'm new to Perl. 

\begin{center}
\begin{tabular}{|l|c|r|}\hline
left&center&right \\ \hline
1&2&3\\ \hline
left alignment&center allignment&right alignment\\ \hline
\end{tabular}
\end{center}
\end{document}

IOPP Program No.3.A

Program No.3.A

 Write an OpenMP program (for shared memory multiprocessor architecture) to sort the given list of numbers using Radix Sort. Display the appropriate environment variables to demonstrate parallel programming. 

Solution:

#include <stdio.h>
#include<omp.h>
int
main ()
{
  int b, i, j, p, q, d, z = 9, digit = 0, max,n;
  int thread;
  int a[100];
 printf("Enter the Number of Elements U Want TO enter\n");
scanf("%d",&n);
printf("Enter the Elements\n");
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}

  d = 10;
  max = a[0];
  for (i = 1; i < n; i++)
    {
      if (max < a[i])
        {
          max = a[i];
        }
    }
  while (max != 0)
    {
      max = max / 10;
      digit++;
    }
#pragma omp parallel for schedule(static,1) num_threads(9)
  for (z = 0; z < digit; z++)
    {
      for (i = 0; i < n; i++)
        {
          p = a[i] % d;
          for (j = i + 1; j < n; j++)
            {
              q = a[j] % d;
              if (p > q)
                {
                  b = a[i];
                  a[i] = a[j];
                  a[j] = b;
                  p = a[i] % d;
                }

            }
        }

      d = d * 10;
    }
  printf ("The array is:\n");
  for (i = 0; i < n; i++)
    {
      printf ("%d\n", a[i]);
    }
#pragma omp parallel for schedule(static,1) num_threads(9)
  for (i = 0; i < 11; i++)
    {
      thread = omp_get_thread_num ();
      printf ("%d\t  thread:%d\n", a[i], thread);
    }

}

IOPP Program No.6. A

Program No.6. A

 Write an OpenMP program (for shared memory multiprocessor architecture) to multiply the two matrices using the brute force approach. Display the appropriate environment variables to demonstrate parallel programming.

Solution:

#include <stdio.h>
#include<omp.h>
int main()
{
  int r1, r2, c1, c2, i, j, k,tid,nthreads,chunk=10;
  int A[10][10],B[10][10],C[10][10];


  printf("Enter number of rows and columns of first matrix : \n");
  scanf("%d%d", &r1, &c1);
  printf("Enter elements of first matrix : \n");

  for (i = 0; i < r1; i++)
    for (j = 0; j < c1; j++)
      scanf("%d", &A[i][j]);

  printf("Enter number of rows and coluimns of second matrix : \n");
  scanf("%d%d", &r2, &c2);

  if (r2 != c1){
   printf("The matrices can't be multiplied with each other.\n");
   exit(0);
  }

  printf("Enter elements of second matrix : \n");

  for (i = 0; i < r2; i++)
    for (j = 0; j < c2; j++)
      scanf("%d", &B[i][j]);

#pragma omp parallel shared(A,B,C,nthreads,chunk) private(tid,i,j,k)
{
tid = omp_get_thread_num();
if(tid==0)
{
nthreads=omp_get_num_threads();
printf("Starting matrix multiple example with %d threads.\n",nthreads);
}
printf("Thread %d starting matrix multiply...\n", tid);
  #pragma omp for schedule (static,chunk)
    for (i = 0; i < r1; i++) {
      printf("Thread %d did row %d\n",tid,i);
      for (j = 0; j < c2; j++) {
        C[i][j]=0;
        for (k = 0; k < c1; k++) {
          C[i][j] += A[i][k]*B[k][j];
        }
      }
    }
}
    printf("Product of the matrices : \n");

for(i = 0; i < r1; i++)
{
for (j = 0; j < c2; j++)
{
      printf("%d\t", C[i][j]);
}
      printf("\n");

}
  return 0;

}

IOPP Program No.4. A

Program No.4. A

 Write an OpenMP program (for shared memory multiprocessor architecture) to multiply two matrices using Strassen’s Multiplication technique. Display the appropriate environment variables to demonstrate parallel programming. 

Solution:

#include<stdio.h>
int main(){
  int a[2][2],b[2][2],c[2][2],i,j;
  int m1,m2,m3,m4,m5,m6,m7;
  int thread;

  printf("Enter the 4 elements of first matrix: ");
  for(i=0;i<2;i++)
      for(j=0;j<2;j++)
           scanf("%d",&a[i][j]);

  printf("Enter the 4 elements of second matrix: ");
  for(i=0;i<2;i++)
      for(j=0;j<2;j++)
           scanf("%d",&b[i][j]);

  printf("\nThe first matrix is\n");
  for(i=0;i<2;i++){
      printf("\n");
      for(j=0;j<2;j++)
           printf("%d\t",a[i][j]);
  }

  printf("\nThe second matrix is\n");
  for(i=0;i<2;i++){
      printf("\n");
      for(j=0;j<2;j++)
           printf("%d\t",b[i][j]);
  }

#pragma omp parallel num_threads(3)
{
 m1= (a[0][0] + a[1][1])*(b[0][0]+b[1][1]);
  m2= (a[1][0]+a[1][1])*b[0][0];
  m3= a[0][0]*(b[0][1]-b[1][1]);
  m4= a[1][1]*(b[1][0]-b[0][0]);
  m5= (a[0][0]+a[0][1])*b[1][1];
  m6= (a[1][0]-a[0][0])*(b[0][0]+b[0][1]);
  m7= (a[0][1]-a[1][1])*(b[1][0]+b[1][1]);
 thread=omp_get_thread_num();
}
  c[0][0]=m1+m4-m5+m7;
  c[0][1]=m3+m5;
  c[1][0]=m2+m4;
  c[1][1]=m1-m2+m3+m6;

   printf("\nAfter multiplication \n");
   for(i=0;i<2;i++){
      printf("\n");
      for(j=0;j<2;j++){
        printf("%d,thread:%d  ",c[i][j],thread);
   }
}
   return 0;

}

IOPP Program No.8.B

Program No.8.B 

Write a Perl script to convert the decimal number to binary number

Solution:

print"Enter a number to convert : ";
$num=<STDIN>;

$intnum=sprintf("%d",$num);

$i=0;

while($intnum>0)
{
        $array[$i]=$intnum%2;
        $intnum=int($intnum/2);
        $i=$i+1;
}

print "Binary value : ";
for($j=$i;$j>=0;$j--)
{
        print "$array[$j]";

}

IOPP Program No.7.B

Program No.7.B 

Write a Perl script to find the highest marks of a subject. The marks of different subject along with the names of the students are maintained in a “result.txt” file.

Solution:

#!/usr/bin/perl

$filename="result.txt";
open(filedesc,$filename) or die "File can't be opened.";

$maxphy=0;
$maxchem=0;
$maxmath=0;

while(<filedesc>)
{
        @fields=split(':',$_);

        if($fields[1]>$maxphy) {
                $maxphy=$fields[1];
                $namephy=$fields[0];
        }

        if($fields[2]>$maxchem) {
                $maxchem=$fields[2];
                $namechem=$fields[0];
        }

        if($fields[3]>$maxmath) {
                $maxmath=$fields[3];
                $namemath=$fields[0];
        }
}
print"Physics max marks : $maxphy, Name : $namephy\n";
print"Chemistry max marks : $maxchem, Name : $namechem\n";

print"Maths max marks : $maxmath Name : $namemath\n";

IOPP Program No.5.B

Program No.5.B 

Write a Perl script to find the average marks of each subject. The marks of the different subject along with the names of the students are maintained in a “result.txt” file.

Solution:

#!/usr/bin/perl

$filename="result.txt";
open(filedesc,$filename) or die "Can't open file.";

$avg=0;
$n=0;
$phytotal=0;
$chemtotal=0;
$mathtotal=0;

while(<filedesc>)
{
        @fields=split(':',$_);
        $physics=$fields[1];
        $chemistry=$fields[2];
        $maths=$fields[3];

        $phytotal=$phytotal+$physics;
        $chemtotal=$chemtotal+$chemistry;
        $mathtotal=$mathtotal+$maths;

        $n=$n+1;
}
$phyavg=$phytotal/$n;
print "Physics average=$phyavg\n";
$chemavg=$chemtotal/$n;
print "Chemistry average=$chemavg\n";
$mathavg=$mathtotal/$n;

print "Maths average=$mathavg\n";

IOPP Program No.3. B

Program No.3. B 

Write a shell script to display the lines of a given file. The line numbers are specified as a range. For example, if the range specified is 10-25, then the lines from 10 to 25 of the file are to be displayed. 

Solution:

echo "Enter the filename."
read filename
echo "Enter the starting line."
read x
echo "Enter the ending line."
read y
if [ $x -gt $y ]
then
echo "ERROR: Ending Line Should Greater Starting Line"
exit
fi
sed -n $x,$y\p $filename > cat > newline
cat newline

IOPP Program No.2.A

Program No.2. a)

 Write OpenMP program (for shared memory multiprocessor architecture) to sort the given list of names using Bubble Sort. Display the appropriate environment variables to demonstrate parallel programming.

Solution:
#include<stdio.h>
#include<stdlib.h>
#include<omp.h>
#include<string.h>
int main(int argc,char **argv)
{
        int i,j,tid,nthreads,n,chunk=10;
        char name[20][10],t_name[20][10],temp[10];
        printf("Enter how many names to be sorted:\n");
        scanf("%d",&n);
        printf("Enter names one by one :\n");
        for(i=0;i<n;i++) {
                scanf("%s",name[i]);
                strcpy(t_name[i],name[i]);
        }
        #pragma omp parallel shared (name,t_name,temp,nthreads,chunk) private(tid,i,j)
        {
        tid = omp_get_thread_num();
        if(tid==0) {
        nthreads=omp_get_num_threads();
        printf("Starting bubble sort example with %d Threads.\n", nthreads);
        }
        printf("Thread %d starting bubble sort.\n", tid);
        #pragma omp parallel for schedule(static,chunk)
        for(i=0;i<n-1;i++)
        {
                for(j=0;j<n-i-1;j++)
                {
                        if(strcmp(name[j],name[j+1])>0)
                        {
                                strcpy(temp,name[j]);
                                strcpy(name[j],name[j+1]);
                                strcpy(name[j+1],temp);
                        }
                }
        }
        printf("Thread %d ending bubble sort.\n", tid);
        }

        printf("List before Sorting :\n");
        for(i=0;i<n;i++)
                printf("%s\n",t_name[i]);

        printf("List after Sorting :\n");
        for(i=0;i<n;i++)
                printf("%s\n",name[i]);

}

IOPP Program NO.1 A

Program No.1.A

Demonstrate the following coding standards by writing a C program to evaluate a postfix evaluation:
 1. Naming convention.
 2. Active Names for functions.
 3. Consistent indentations and brace styles. 
4. Proper parenthesizing to resolve the ambiguity. 
5. Proper documentation. 

Solution:

#include<stdio.h>

struct stack{
int array[20];
int top;
}s;

void push(int element)
{
        s.array[++s.top] = element;
}

int pop()
{
        return s.array[s.top--];
}

int main()
{
        char exp[20];
        char *e;
        int n1,n2,n3,num;
        s.top=-1;
        printf("Enter the expression :: ");
        scanf("%s",exp);
        e = exp;
        while(*e != '\0')
        {
                if(isdigit(*e))
                {
                        num = *e - 48;
                        push(num);
                }
                else
                {
                        n1 = pop();
                        n2 = pop();
                        switch(*e)
                        {
                                case '+':
                                {
                                        n3 = n1 + n2;
                                        break;
                                }
                                case '-':
                                {
                                        n3 = n2 - n1;
                                        break;
                                }
                                case '*':
                                {
                                        n3 = n1 * n2;
                                        break;
                                }
                                case '/':
                                {
                                        n3 = n2 / n1;
                                        break;
                                }
                        }
                        push(n3);
                }
                e++;
        }
        printf("\nThe result of expression %s  =  %d\n\n",exp,pop());
        return 0;


}