0% found this document useful (0 votes)
414 views9 pages

Q1: Write A Program in C Language For Addition of Two Polynomials Using Pointers

The program summarizes a document containing code snippets for adding two polynomials using pointers in C and performing depth first search on a graph represented as an adjacency list in C#. The C# program adds two polynomials by taking each term of the first polynomial and adding it to the corresponding term of the second polynomial. It then prints the sum polynomial. The C# graph representation uses an adjacency list to store the graph. It performs depth first search recursively by marking the current node as visited, recursively searching adjacent unvisited nodes, and printing the node. The program contains code snippets for additional questions on adding polynomials in C, performing DFS on a graph in C#, accepting user input text file in a shell script and counting
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
414 views9 pages

Q1: Write A Program in C Language For Addition of Two Polynomials Using Pointers

The program summarizes a document containing code snippets for adding two polynomials using pointers in C and performing depth first search on a graph represented as an adjacency list in C#. The C# program adds two polynomials by taking each term of the first polynomial and adding it to the corresponding term of the second polynomial. It then prints the sum polynomial. The C# graph representation uses an adjacency list to store the graph. It performs depth first search recursively by marking the current node as visited, recursively searching adjacent unvisited nodes, and printing the node. The program contains code snippets for additional questions on adding polynomials in C, performing DFS on a graph in C#, accepting user input text file in a shell script and counting
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

PART-1: MCS-021

Q1: Write a program in C language for addition of two polynomials using Pointers.
ANS

C# program to add two polynomials


using System;
class GFG {

// A utility function to return maximum of two integers


static int max(int m, int n)
{
return (m > n) ? m : n;
}

// A[] represents coefficients of first polynomial


// B[] represents coefficients of second polynomial
// m and n are sizes of A[] and B[] respectively
static int[] add(int[] A, int[] B, int m, int n)
{
int size = max(m, n);
int[] sum = new int[size];

// Initialize the porduct polynomial


for (int i = 0; i < m; i++)
{
sum[i] = A[i];
}

// Take ever term of first polynomial


for (int i = 0; i < n; i++)
{
sum[i] += B[i];
}

return sum;
}

// A utility function to print a polynomial


static void printPoly(int[] poly, int n)
{
for (int i = 0; i < n; i++)
{
Console.Write(poly[i]);
if (i != 0)
{
Console.Write("x^" + i);
}
if (i != n - 1)
{
Console.Write(" + ");
}
}
}

// Driver code
public static void Main()
{
// The following array represents
// polynomial 5 + 10x^2 + 6x^3
int[] A = {5, 0, 10, 6};
// The following array represents
// polynomial 1 + 2x + 4x^2
int[] B = {1, 2, 4};
int m = A.Length;
int n = B.Length;
Console.WriteLine("First polynomial is");
printPoly(A, m);
Console.WriteLine("\nSecond polynomial is");
printPoly(B, n);
int[] sum = add(A, B, m, n);
int size = max(m, n);
Console.WriteLine("\nsum polynomial is");
printPoly(sum, size);

}
}

//This Code is Contributed

Q2: Write a program in C language that will accept a Graph as input and will perform a Depth First
Search on it. Make necessary assumptions.
Ans

// C# program to print DFS traversal


// from a given graph
using System;
using System.Collections.Generic;

// This class represents a directed graph


// using adjacency list representation
class Graph
{
private int V; // No. of vertices

// Array of lists for


// Adjacency List Representation
private List<int>[] adj;

// Constructor
Graph(int v)
{
V = v;
adj = new List<int>[v];
for (int i = 0; i < v; ++i)
adj[i] = new List<int>();
}

//Function to Add an edge into the graph


void AddEdge(int v, int w)
{
adj[v].Add(w); // Add w to v's list.
}

// A function used by DFS


void DFSUtil(int v, bool[] visited)
{
// Mark the current node as visited
// and print it
visited[v] = true;
Console.Write(v + " ");

// Recur for all the vertices


// adjacent to this vertex
List<int> vList = adj[v];
foreach (var n in vList)
{
if (!visited[n])
DFSUtil(n, visited);
}
}

// The function to do DFS traversal.


// It uses recursive DFSUtil()
void DFS(int v)
{
// Mark all the vertices as not visited
// (set as false by default in c#)
bool[] visited = new bool[V];

// Call the recursive helper function


// to print DFS traversal
DFSUtil(v, visited);
}

// Driver Code
public static void Main(String[] args)
{
Graph g = new Graph(4);

g.AddEdge(0, 1);
g.AddEdge(0, 2);
g.AddEdge(1, 2);
g.AddEdge(2, 0);
g.AddEdge(2, 3);
g.AddEdge(3, 3);

Console.WriteLine("Following is Depth First Traversal " +


"(starting from vertex 2)");

g.DFS(2);
Console.ReadKey();
}
}

PART-2: MCS-022

Q1:Write a shell script in Linux/Unix that accepts a text file as input and prints the number of words
that have at least one vowel

Ans .
#!/bin/bash
file=$1
v=0
if [ $# -ne 1 ]
then
echo "$0 fileName"
exit 1
fi
if [ ! -f $file ]
then
echo "$file not a file"
exit 2
fi
while read -n 1 c
do
l=$(echo $c | tr [:upper:] [:lower:])
[[ "$l" == "a" || "$l" == "e" || "$l" == "i" || "$l" == "o" || "$l" == "u" ]] && (( v++ ))
done < $file
echo "Vowels : $v"
echo "Characters : $(cat $file | wc -c)"
echo "Blank lines : $(grep -c '^$' $file)"
echo "Lines : $(cat $file|wc -l )"

OUTPUT:

Q2:Your PC is on a network. Find the number of Printers on whom you can command a Print from
your PC.
STEPS To Connect Printer: -
1. Click on Start in the bottom left corner of your screen. A popup list will appear.
2. Select Control Panel from the popup list. Type the word network in the search box.
3. Click on Network and Sharing Center.
4. Click on Change advanced shared settings, in the left pane.
5. Click on the down arrow, which will expand the network profile.
6. Select File and printer sharing and choose Turn on file and printer sharing.
7. Click on Save changes.
You're now ready to share your printer.
1. Click on Start in the bottom left corner of your screen. A popup list will appear.
2. Click on Devices and Printers, from the popup list.
3. Right click the printer you want to share. A dropdown list will appear.
4. Select Printer properties from the dropdown list.
5. Click on the Sharing tab
6. Select the Share this printer check box.
In order for other people to connect to the printer, they just have to add the network printer that you just opened for sharing to their
computers. Here's how to do this.

1. Click on Start in the bottom left corner of your screen. A popup list will appear.
2. Click on Devices and Printers from the popup list.
3. Select Add a printer.
4. Click on Add a network, wireless or Bluetooth printer.
5. Click the shared printer.

6. Click Next. Continue according to the instructions on the screen.


PART-3: MCS-023

Q1: (10 marks) create a database consisting of Name of Bank, Number of Branches, Total number of
Customers, Average number of Customers who visit Bank in a Day After creating the database,
perform the following tasks:

(i) List the names of Banks which have more than 1000 branches

Ans:

Step 1

Create Database Bank;

Show Databases;

OUTPUT

Step 2
create Table Bank_Info (

-> Bank_Name varchar(20),

-> Number_of_Branches int(5),

-> Total_Customer int(9),

-> Avg_no_of_customer_perday int(5)

-> );

Step3
SQL QUERY
INSERT INTO Bank_Info (Bank_Name,Number_of_Branches,Total_Customer,Avg_no_of_customer_perday)

-> VALUES ('SBI','1220','90000','500');

mysql>INSERT INTO Bank_Info


(Bank_Name,Number_of_Branches,Total_Customer,Avg_no_of_customer_perday)

-> VALUES ('PNB','1500','100000','700');


mysql> INSERT INTO Bank_Info
(Bank_Name,Number_of_Branches,Total_Customer,Avg_no_of_customer_perday)

-> VALUES ('Vijaya Bank','800','50000','300');

mysql> INSERT INTO Bank_Info


(Bank_Name,Number_of_Branches,Total_Customer,Avg_no_of_customer_perday)

-> VALUES ('Yes Bank','250','20000','800');

mysql> INSERT INTO Bank_Info


(Bank_Name,Number_of_Branches,Total_Customer,Avg_no_of_customer_perday)

-> VALUES ('HDFC','2500','120000','1000');

mysql> INSERT INTO Bank_Info


(Bank_Name,Number_of_Branches,Total_Customer,Avg_no_of_customer_perday)

-> VALUES ('BOB','250','50000','520');

Select * from Bank_Info;

OUTPUT

(i) List the names of Banks which have more than 1000 branches
Part-4: MCS-024

Q1: Write a program in Java for multiplication of two sparse matrices

Ans.

The Code:

1. import java.util.Scanner;
2.
3. class MatrixMultiplication
4. {
5. public static void main(String args[])
6. {
7. int m, n, p, q, sum = 0, c, d, k;
8.
9. Scanner in = new Scanner(System.in);
10. System.out.println("Enter the number of rows and columns of first matrix");
11. m = in.nextInt();
12. n = in.nextInt();
13.
14. int first[][] = new int[m][n];
15.
16. System.out.println("Enter elements of first matrix");
17.
18. for (c = 0; c < m; c++)
19. for (d = 0; d < n; d++)
20. first[c][d] = in.nextInt();
21.
22. System.out.println("Enter the number of rows and columns of second matrix");
23. p = in.nextInt();
24. q = in.nextInt();
25.
26. if (n != p)
27. System.out.println("The matrices can't be multiplied with each other.");
28. else
29. {
30. int second[][] = new int[p][q];
31. int multiply[][] = new int[m][q];
32.
33. System.out.println("Enter elements of second matrix");
34.
35. for (c = 0; c < p; c++)
36. for (d = 0; d < q; d++)
37. second[c][d] = in.nextInt();
38.
39. for (c = 0; c < m; c++)
40. {
41. for (d = 0; d < q; d++)
42. {
43. for (k = 0; k < p; k++)
44. {
45. sum = sum + first[c][k]*second[k][d];
46. }
47.
48. multiply[c][d] = sum;
49. sum = 0;
50. }
51. }
52.
53. System.out.println("Product of the matrices:");
54.
55. for (c = 0; c < m; c++)
56. {
57. for (d = 0; d < q; d++)
58. System.out.print(multiply[c][d]+"\t");
59.
60. System.out.print("\n");
61. }
62. }
63. }
64. }
The Output:

Q2:(5Marks)

Write a program in Java that connects to a database and generates a report that consists of the list of
Universities which are offering a specific Programme of Study. Input to the Java program will be
name or abbreviation of a Programme of Study (such as MCA). Make assumptions wherever
necessary.

Ans:
import java.io.*;
import java.sql.*;
public class Univercity
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
// Input programme name from the user
System.out.println("Enter the programme name ...");
String pname=br.readLine();
// SQL query for search Universities for the given programme
String SQL="SELECT university_name FROM university_details WHERE programme_name="+pname+"'";
String url="jdbc:mysql://localhost:3306/University";
String user="Admin";
String password="Admin1234";
int count=0;
try
{
// Connect to the database and retrieve data for the above query
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(url,user,password);
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(SQL);
while(rs.next())
{
System.out.println(rs.getString("university_name"));
count++;
}
con.close();
if(count == 0)
System.out.println("University not found");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

You might also like