Assignment No-3
Assignment No-3
Assignment No-3
Q.9. Write a generic C++ function that takes an array of generic elements and a
scalar of the same type as the array elements. The type of the array elements
and the scalar is the generic parameter. The function must search the given
array for the given scalar and return the subscript of the scalar in the array. If
the scalar is not in the array, the function must return –1. Test the function for
int and float types.
Solution :-
#include <iostream>
using namespace std;
template <typename Type>
int search(Type arr[], Type key,int n)
{
for(int i=0;i<n;i++)
{
if(arr[i]== key)
{
return i;
}
}
return -1;
}
int main()
{
double arr[6]= {2.4, 5.53,44.4, 54.45, 65.7,89.54};
double key=54.45;
int arri[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int keyi=5;
int v= search(arr,key,6);
if(v != -1)
{
cout<<"your search found at index "<<v <<endl;
}
else
{
cout<<"your search not found"<<endl;
}
int k= search(arri,keyi,9);
if(k != -1)
{
cout<<"your search found at index "<<k;
}
else
{
cout<<"your search not found";
}
return 0;
}
OUTPUT :
your search found at index 3
your search found at index 4
--------------------------------
Process exited after 0.2068 seconds with return value 0
Press any key to continue . . .
Q.4. Consider the following programming problem: The values of three integer
variables—first, second, and third—must be placed in the three variables max,
mid, and min, with the obvious meanings, without using arrays or user-defined
or predefined subprograms. Write two solutions to this problem, one that uses
nested selections and one that does not. Compare the complexity and
expected reliability of the two.
Solution :-
USING NESTED IF – ELSE :-
#include<stdio.h>
int main()
{
int first,second,third;
first=4;
second=3;
third=9;
if(first>second)
{
if(first>third)
{
printf("max value is first\n");
if(third>second)
{
printf("mid value is third\n");
printf("min value is second\n");
}
else
{
printf("mid value is second\n");
printf("min value is third\n");
}
}
else
{
printf("max value is third\n");
printf("mid value is first\n");
printf("min value is second\n");
}
}
else
{
if(second>third)
{
printf("max value is second\n");
if(first>third)
{
printf("mid value is second\n");
printf("min value is third\n");
}
else
{
printf("mid value is third\n");
printf("min value is first\n");
}
}
else
{
printf("max value is third\n");
printf("mid value is first\n");
printf("min value is second\n");
}
}
return 0;
}
OUTPUT :
max value is third
mid value is first
min value is second
--------------------------------
Process exited after 0.1295 seconds with return value 0
Press any key to continue . . .
}
if(second>third && third>first)
{
printf("max value is second\n");
printf("mid value is third\n");
printf("min value is first\n");
}
if(third>first && first>second)
{
printf("max value is third\n");
printf("mid value is first\n");
printf("min value is second\n");
}
if(third>second && second>first)
{
printf("max value is third\n");
printf("mid value is second\n");
printf("min value is first\n");
}
return 0;
}
OUTPUT :
max value is third
mid value is first
min value is second
--------------------------------
Process exited after 0.1269 seconds with return value 0
Press any key to continue . . .
Conclusion :- after observing the output time we can say that nested selection
program is more complex, and less readability .
#include<stdio.h>
int main()
{
int i,j = -3;
for (i = 0; i < 3; i++) {
switch (j + 2) {
case 3:
case 2: j--;
case 0: j += 2;
default: j = 0;
}
if (j > 0) {}
j = 3 - i;
}
return 0;
}
2-
#include<stdio.h>
int main()
{
int i,j = -3;
for (i = 0; i < 3; i++) {
TOP:
if((j+2) ==3)
{
}
else if((j+2) ==2)
{
j++;
goto TOP;
}
else if((j+2) ==0)
{
j +=2;
goto TOP;
}
else
{
j=0;
}
}
if (j > 0)
{
goto OUT;
}
j = 3 - i;
OUT:
return 0;
3-
Q.3. In a letter to the editor of CACM, Rubin (1987) uses the following code
segment as evidence that the readability of some code with gotos is better
than the equivalent code without gotos. This code finds the first row of an n by
n integer matrix named x that has nothing but zero values.
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++)
if (x[i][j] != 0)
goto reject;
println ('First all-zero row is:', i);
break;
reject:
}
Rewrite this code without gotos in one of the following languages: C, C++ or
Java. Compare the readability of your code to that of the example code.
Solution :-
IN C :-
#include<stdio.h>
int main()
{
int n=3;
int arr[3][3]={{0,0,0},{1,2,3},{0,0,0}};
int i,j,sum=0;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++){
if (arr[i][j] == 0)
{
sum=sum+1;
}
}
if(sum==n)
{
printf("First all-zero row is: %d", i);
break;
}
}
return 0;
}
OUTPUT :
First all-zero row is: 0
--------------------------------
Process exited after 0.1419 seconds with return value 0
Press any key to continue . . .
IN JAVA :-
class check
{
public static void main(String args[])
{
int n=3;
int arr[][]={{0,0,0},{1,2,3},{0,0,0}};
int i,j,sum=0;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++){
if (arr[i][j] == 0)
{
sum=sum+1;
}
}
if(sum==n)
{
System.out.println("First all-zero row is: "+ i);
break;
}
}
}
}
OUTPUT :
run:
First all-zero row is: 0
BUILD SUCCESSFUL (total time: 0 seconds)
Q.6. Write a program in some language that has both static and stack-dynamic
local variables in subprograms. Create six large (at least 100 * 100) matrices in
the subprogram—three static and three stack-dynamic. Fill two of the static
matrices and two of the stack-dynamic matrices with random numbers in the
range of 1 to 100. The code in the subprogram must perform a large number of
matrix multiplication operations on the static matrices and time the process.
Then it must repeat this with the stack-dynamic matrices. Compare and explain
the results.
Solution :-
#include <stdio.h>
#include<conio.h>
#include <time.h>
#include <stdlib.h>
void fun1()
{
int x=100,i,j;
static int y=100;
static int a[100][100];
static int b[100][100];
static int c[100][100];
for(i=0;i<100;i++)
for(j=0;j<100;j++)
a[i][j]= rand()%100 ;
for(i=0;i<100;i++)
for(j=0;j<100;j++)
b[i][j]= rand()%100 ;
for(i=0;i<100;i++)
for(j=0;j<100;j++)
c[i][j]= a[i][j]*b[i][j] ;
for(i=0;i<100;i++)
for(j=0;j<100;j++)
printf("%d\n",c[i][j]);
}
void fun2()
{
int a=100,i,j;
static int b=100;
int arr1[a][a];
int arr2[a][a];
int arr3[a][a];
for(i=0;i<100;i++)
for(j=0;j<100;j++)
arr1[i][j]= rand()%100 ;
for(i=0;i<100;i++)
for(j=0;j<100;j++)
arr2[i][j]= rand()%100 ;
for(i=0;i<100;i++)
for(j=0;j<100;j++)
arr3[i][j]= arr1[i][j]*arr2[i][j] ;
for(i=0;i<100;i++)
for(j=0;j<100;j++)
printf("%d\n",arr3[i][j]);
}
int main()
{
printf("static array multiplication :\n");
double time_taken = 0.0;
clock_t begin = clock();
fun1();
clock_t end = clock();
time_taken =time_taken + ((double)(end-begin))/CLOCKS_PER_SEC;
printf("fun1() took %f seconds to execute \n", time_taken);
getch();
time_taken=0.0;
printf("stack dynamic array multiplication :\n");
begin = clock();
fun2();
end = clock();
time_taken =time_taken + ((double)(end-begin))/CLOCKS_PER_SEC;
printf("fun2() took %f seconds to execute \n", time_taken);
return 0;
}
OUTPUT :
fun1() took 13.776000 seconds to execute
fun2() took 4.889000 seconds to execute
--------------------------------
Process exited after 24.4 seconds with return value 0
Press any key to continue . . .
Conclusion :- after observing the output we can say that function who contain
static dynamic arrays and its multiplication takes less amount of time compare
then who takes static arrays.
Q.1. Rewrite the following pseudocode segment using a loop structure in C,
C++, Java and Python:
k = (j + 13) / 27
loop:
if k > 10 then goto out
k=k+1
i=3*k–1
goto loop
out: . . .
Assume all variables are integer type. Discuss which language, for this code,
has the best writability, the best readability, and the best combination of the
two. Redo the exercise, except this time make all the variables and constants
floating-point type, and change the statement
k=k+1
to
k = k + 1.2
Solution :
// PROGRAMS FOR INTEGERS
C PROGRAM
#include<stdio.h>
int main () {
int i, j, k;
scanf("%d",&j);
k = (j + 13)/27;
while(k < 10)
{
k = k + 1;
i = 3 * k -1;
}
return 0;
}
C++ PROGRAM
#include<iostream>
using namespace std;
int main () {
int i, j, k;
cin >> j;
k = (j + 13)/27;
while(k < 10)
{
k = k + 1;
i = 3 * k -1;
}
return 0;
}
JAVA PROGAM
import java.io.*;
import java.util.Scanner;
public class Demo {
public static void main (String args[])
{
int i, j, k;
Scanner sc = new Scanner(System.in);
j = sc.nextInt();
k = (j + 13)/27;
while(!(k > 10))
{
k = k + 1;
i = 3 * k -1;
}
}
}
PYTHON PROGRAM
j = int(input())
k = (j + 13)/27;
while(k < 10):
k = k + 1;
i = 3 * k - 1;
#include<stdio.h>
int main () {
float i, j, k;
scanf("%f",&j);
k = (j + 13.0)/27.0;
while(k <10)
{
k = k + 1.2;
i = 3.0 * k - 1.0;
}
return 0;
}
// C++
#include<iostream>
using namespace std;
int main () {
float i, j, k;
cin >> j;
k = (j + 13.0)/27.0;
while(k<10.0)
{
k = k + 1.2;
i = 3.0 * k - 1.0;
}
return 0;
}
//JAVA
import java.io.*;
import java.util.Scanner;
public class Demo {
public static void main (String args[])
{
float i, j, k;
Scanner sc = new Scanner(System.in);
j = sc.nextFloat();
k = (j + 13.0)/27.0;
while(k ><10.0)
{
k = k + 1.2;
i = 3.0 * k – 1.0;
}
}
}
// PYTHON
j = float(input())
k = (j + 13.0)/27.0;
k = k + 1.2;
i = 3.0 * k - 1.2;