Tuesday, 30 April 2019
Aptitude Hack#66(30-4-19)
Question:
A zookeeper counted the heads of the animals in a zoo and found it to be 80. When he counted the legs of the animals he found it to be 260. If the zoo had either pigeons or horses, how many horses were there in the zoo?
A) 40
B) 30
C) 50
D) 60
E) None of These
A zookeeper counted the heads of the animals in a zoo and found it to be 80. When he counted the legs of the animals he found it to be 260. If the zoo had either pigeons or horses, how many horses were there in the zoo?
A) 40
B) 30
C) 50
D) 60
E) None of These
CLICK HERE |
Monday, 29 April 2019
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¢er&right \\ \hline
1&2&3\\ \hline
left alignment¢er allignment&right alignment\\ \hline
\end{tabular}
\end{center}
\end{document}
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¢er&right \\ \hline
1&2&3\\ \hline
left alignment¢er 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);
}
}
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;
}
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;
}
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]";
}
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";
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.6.B
Program No.6.B
Write a Perl script to copy the contents of one file to another file.
Solution:
open(file1,"<source.txt");
open(file2,">destination.txt");
open(file3,">>appendfile.txt");
while(<file1>)
{
print file2 $_;
print $_;
print file3 $_;
}
Write a Perl script to copy the contents of one file to another file.
Solution:
open(file1,"<source.txt");
open(file2,">destination.txt");
open(file3,">>appendfile.txt");
while(<file1>)
{
print file2 $_;
print $_;
print file3 $_;
}
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";
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.4.B
Program No.4.B
Write a shell script that displays “Good morning”, “Good afternoon” or “Good evening”, depending upon the time at which the user logs in.
Solution:
echo "Greeting of the day."
x=`date | cut -c 12-13`
echo $x
y=`date | cut -c 12-16`
echo $y
if [ $x -ge 0 ] && [ $x -lt 10 ]
then
echo "Good morning."
elif [ $x -ge 10 ] && [ $x -lt 14 ]
then
echo "Good afternoon."
elif [ $x -ge 14 ] && [ $x -lt 20 ]
then
echo "Good evening."
else
echo "Good night."
fi
Write a shell script that displays “Good morning”, “Good afternoon” or “Good evening”, depending upon the time at which the user logs in.
Solution:
echo "Greeting of the day."
x=`date | cut -c 12-13`
echo $x
y=`date | cut -c 12-16`
echo $y
if [ $x -ge 0 ] && [ $x -lt 10 ]
then
echo "Good morning."
elif [ $x -ge 10 ] && [ $x -lt 14 ]
then
echo "Good afternoon."
elif [ $x -ge 14 ] && [ $x -lt 20 ]
then
echo "Good evening."
else
echo "Good night."
fi
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
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.B
Program No.2.B
Write a shell script to check whether a number is a palindrome or not.
Solution:
echo "Enter a number."
read n
p=$n
sd=0
rev=0
while [ $n -gt 0 ]
do
sd=$(( $n % 10 ))
rev=`expr $rev \* 10 + $sd`
n=$(( $n / 10 ))
done
if [ $p -eq $rev ]
then
echo "Palindrome."
else
echo "Not Palindrome."
fi
Write a shell script to check whether a number is a palindrome or not.
Solution:
echo "Enter a number."
read n
p=$n
sd=0
rev=0
while [ $n -gt 0 ]
do
sd=$(( $n % 10 ))
rev=`expr $rev \* 10 + $sd`
n=$(( $n / 10 ))
done
if [ $p -eq $rev ]
then
echo "Palindrome."
else
echo "Not Palindrome."
fi
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]);
}
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.B
Program No.1.B
Write a shell script to determine the type of triangle.
Solution:
echo "Enter the first side of the triangle."
read x
echo "Enter the second side of the triangle."
read y
echo "Enter the third side of the triangle."
read z
if [ $x -eq $y ] && [ $y -eq $z ]
then
echo "Equilateral triangle."
elif [ $x -eq $y ] || [ $y -eq $z ] || [ $x -eq $z ]
then
echo "Isoceles triangle."
else
echo "Scalene triangle."
fi
Write a shell script to determine the type of triangle.
Solution:
echo "Enter the first side of the triangle."
read x
echo "Enter the second side of the triangle."
read y
echo "Enter the third side of the triangle."
read z
if [ $x -eq $y ] && [ $y -eq $z ]
then
echo "Equilateral triangle."
elif [ $x -eq $y ] || [ $y -eq $z ] || [ $x -eq $z ]
then
echo "Isoceles triangle."
else
echo "Scalene triangle."
fi
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;
}
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;
}
Saturday, 27 April 2019
Tricky Interview#2
Question:
Write a function that moves the last element to the front in a given Singly Linked List.
For example:
Input Linked List is 1->2->3->4->5.
Output Linked List 5->1->2->3->4.
Solution:
Move last element to front of a given Linked List
April 27, 2019C interview Question, C Theory Question, Move last element to front of a given Linked List, Technical Round, Tricky Interview QuestionNo comments
Question:
Write a function that moves the last element to the front in a given Singly Linked List.
For example:
Input: 1->2->3->4->5
Output: 5->1->2->3->4.
Algorithm:
Traverse the list till the last node. Use two pointers: one to store the address of the last node and other for the address of the second last node. After the end of the loop do the following operations.
i) Make second last as last (secLast->next = NULL).
ii) Set next of last as head (last->next = *head_ref).
iii) Make last as head ( *head_ref = last)
C Program :
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node *next;
};
void moveToFront(struct Node **head_ref)
{
/* If linked list is empty, or it contains only one node,
then nothing needs to be done, simply return */
if (*head_ref == NULL || (*head_ref)->next == NULL)
return;
/* Initialize second last and last pointers */
struct Node *secLast = NULL;
struct Node *last = *head_ref;
/*After this loop secLast contains address of second last
node and last contains address of last node in Linked List */
while (last->next != NULL)
{
secLast = last;
last = last->next;
}
/* Set the next of second last as NULL */
secLast->next = NULL;
/* Set next of last as head node */
last->next = *head_ref;
/* Change the head pointer to point to last node now */
*head_ref = last;
}
/* UTILITY FUNCTIONS */
/* Function to add a node at the begining of Linked List */
void push(struct Node** head_ref, int new_data)
{
/* allocate node */
struct Node* new_node =
(struct Node*) malloc(sizeof(struct Node));
/* put in the data */
new_node->data = new_data;
/* link the old list off the new node */
new_node->next = (*head_ref);
/* move the head to point to the new node */
(*head_ref) = new_node;
}
/* Function to print nodes in a given linked list */
void printList(struct Node *node)
{
while(node != NULL)
{
printf("%d ", node->data);
node = node->next;
}
}
int main()
{
struct Node *start = NULL;
push(&start, 5);
push(&start, 4);
push(&start, 3);
push(&start, 2);
push(&start, 1);
printf("\n Linked list before moving last to the front\n");
printList(start);
moveToFront(&start);
printf("\n Linked list after removing last to the front\n");
printList(start);
return 0;
}
Output:
Linked list before moving last to the front
1 2 3 4 5
Linked list after removing last to the front
5 1 2 3 4
Write a function that moves the last element to the front in a given Singly Linked List.
For example:
Input: 1->2->3->4->5
Output: 5->1->2->3->4.
Algorithm:
Traverse the list till the last node. Use two pointers: one to store the address of the last node and other for the address of the second last node. After the end of the loop do the following operations.
i) Make second last as last (secLast->next = NULL).
ii) Set next of last as head (last->next = *head_ref).
iii) Make last as head ( *head_ref = last)
C Program :
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node *next;
};
void moveToFront(struct Node **head_ref)
{
/* If linked list is empty, or it contains only one node,
then nothing needs to be done, simply return */
if (*head_ref == NULL || (*head_ref)->next == NULL)
return;
/* Initialize second last and last pointers */
struct Node *secLast = NULL;
struct Node *last = *head_ref;
/*After this loop secLast contains address of second last
node and last contains address of last node in Linked List */
while (last->next != NULL)
{
secLast = last;
last = last->next;
}
/* Set the next of second last as NULL */
secLast->next = NULL;
/* Set next of last as head node */
last->next = *head_ref;
/* Change the head pointer to point to last node now */
*head_ref = last;
}
/* UTILITY FUNCTIONS */
/* Function to add a node at the begining of Linked List */
void push(struct Node** head_ref, int new_data)
{
/* allocate node */
struct Node* new_node =
(struct Node*) malloc(sizeof(struct Node));
/* put in the data */
new_node->data = new_data;
/* link the old list off the new node */
new_node->next = (*head_ref);
/* move the head to point to the new node */
(*head_ref) = new_node;
}
/* Function to print nodes in a given linked list */
void printList(struct Node *node)
{
while(node != NULL)
{
printf("%d ", node->data);
node = node->next;
}
}
int main()
{
struct Node *start = NULL;
push(&start, 5);
push(&start, 4);
push(&start, 3);
push(&start, 2);
push(&start, 1);
printf("\n Linked list before moving last to the front\n");
printList(start);
moveToFront(&start);
printf("\n Linked list after removing last to the front\n");
printList(start);
return 0;
}
Output:
Linked list before moving last to the front
1 2 3 4 5
Linked list after removing last to the front
5 1 2 3 4
Friday, 26 April 2019
Thursday, 25 April 2019
Wednesday, 24 April 2019
Tuesday, 23 April 2019
Monday, 22 April 2019
Aptitude Hack#58(22-4-19)
Question:
There are two examinations rooms A and B. If 10 students are sent from A to B, then the number of students in each room is the same. If 20 candidates are sent from B to A, then the number of students in A is double the number of students in B. The number of students in room A is:
A. 20
B. 80
C. 100
D. 200
There are two examinations rooms A and B. If 10 students are sent from A to B, then the number of students in each room is the same. If 20 candidates are sent from B to A, then the number of students in A is double the number of students in B. The number of students in room A is:
A. 20
B. 80
C. 100
D. 200
Sunday, 21 April 2019
Saturday, 20 April 2019
Friday, 19 April 2019
Thursday, 18 April 2019
Wednesday, 17 April 2019
Monday, 15 April 2019
Sunday, 14 April 2019
Saturday, 13 April 2019
Aptitude Hack#49(13-4-19)
Question:
8 litres are drawn from a cask full of wine and is then filled with water. This operation is performed three more times. The ratio of the quantity of wine now left in cask to that of water is 16 : 65. How much wine did the cask hold originally?
A. 18 litres
B. 24 litres
C. 32 litres
D. 42 litres
8 litres are drawn from a cask full of wine and is then filled with water. This operation is performed three more times. The ratio of the quantity of wine now left in cask to that of water is 16 : 65. How much wine did the cask hold originally?
A. 18 litres
B. 24 litres
C. 32 litres
D. 42 litres
Friday, 12 April 2019
Integer Promotions in C
Integer Promotions in C
Some data types like char, short int take less number of bytes than int, these data types are automatically promoted to int or unsigned int when an operation is performed on them. This is called integer promotion. For example, no arithmetic calculation happens on smaller types like char, short and enum. They are first converted to int or unsigned int, and then arithmetic is done on them. If an int can represent all values of the original type, the value is converted to an int. Otherwise, it is converted to an unsigned int.
For example see the following program:
#include <stdio.h>
int main()
{
char a = 30, b = 40, c = 10;
char d = (a * b) / c;
printf ("%d ", d);
return 0;
}
Output:
120
At first look, the expression (a*b)/c seems to cause arithmetic overflow because signed characters can have values only from -128 to 127 (in most of the C compilers), and the value of subexpression ‘(a*b)’ is 1200 which is greater than 128. But integer promotion happens here in arithmetic done on char types and we get the appropriate result without any overflow.
Some data types like char, short int take less number of bytes than int, these data types are automatically promoted to int or unsigned int when an operation is performed on them. This is called integer promotion. For example, no arithmetic calculation happens on smaller types like char, short and enum. They are first converted to int or unsigned int, and then arithmetic is done on them. If an int can represent all values of the original type, the value is converted to an int. Otherwise, it is converted to an unsigned int.
For example see the following program:
#include <stdio.h>
int main()
{
char a = 30, b = 40, c = 10;
char d = (a * b) / c;
printf ("%d ", d);
return 0;
}
Output:
120
At first look, the expression (a*b)/c seems to cause arithmetic overflow because signed characters can have values only from -128 to 127 (in most of the C compilers), and the value of subexpression ‘(a*b)’ is 1200 which is greater than 128. But integer promotion happens here in arithmetic done on char types and we get the appropriate result without any overflow.
Thursday, 11 April 2019
Tricky Interview #1
Question:
W A C Program That Prints All The vowels Given In String Without Using IF and Break Statements In program?
Submit Ur Answers (Follow The Link)
Link: https://docs.google.com/forms/d/e/1FAIpQLSeBXalNTB5cLX8dC8BlFmzGcItV52GBiUkqaaCM2q_bpaDnKg/viewform?usp=sf_link
W A C Program That Prints All The vowels Given In String Without Using IF and Break Statements In program?
Submit Ur Answers (Follow The Link)
Link: https://docs.google.com/forms/d/e/1FAIpQLSeBXalNTB5cLX8dC8BlFmzGcItV52GBiUkqaaCM2q_bpaDnKg/viewform?usp=sf_link
Wednesday, 10 April 2019
Tuesday, 9 April 2019
Generic Storing of Number
Question:
Write a Java program the implements a generic sort method using any of the sorting techniques.
Solution:
public class Students{
public static <T extends Comparable<T>> void sortingMethod(Number[] i2) {
int i,j,n;
n=i2.length;
for(i=0;i<n;i++)
for(j=0;j<n-i-1;j++)
if(((Comparable<T>) i2[j]).compareTo((T) i2[j+1])>0)
{
T temp;
temp=(T) i2[j];
i2[j]=i2[j+1];
i2[j+1]=(Number) temp;
}
System.out.println();
System.out.println("After Sorting The Elements");
for(Number element: i2)
System.out.printf("%s, ", element);
}
public static void main(String args[])
{
Number i[]={3.2,2.5,10.02,7.012,7.06};
System.out.println("Before Sorting The Elements");
for(Number element:i)
System.out.printf("%s, ",element);
sortingMethod(i);
}
}
Output:
Before Sorting The Elements
3.2, 2.5, 10.02, 7.012, 7.06,
After Sorting The Elements
2.5, 3.2, 7.012, 7.06, 10.02,
Credits: Gaurav
Write a Java program the implements a generic sort method using any of the sorting techniques.
Solution:
public class Students{
public static <T extends Comparable<T>> void sortingMethod(Number[] i2) {
int i,j,n;
n=i2.length;
for(i=0;i<n;i++)
for(j=0;j<n-i-1;j++)
if(((Comparable<T>) i2[j]).compareTo((T) i2[j+1])>0)
{
T temp;
temp=(T) i2[j];
i2[j]=i2[j+1];
i2[j+1]=(Number) temp;
}
System.out.println();
System.out.println("After Sorting The Elements");
for(Number element: i2)
System.out.printf("%s, ", element);
}
public static void main(String args[])
{
Number i[]={3.2,2.5,10.02,7.012,7.06};
System.out.println("Before Sorting The Elements");
for(Number element:i)
System.out.printf("%s, ",element);
sortingMethod(i);
}
}
Output:
Before Sorting The Elements
3.2, 2.5, 10.02, 7.012, 7.06,
After Sorting The Elements
2.5, 3.2, 7.012, 7.06, 10.02,
Credits: Gaurav