SEC1-P2 Computer Science 2022 PYQ NBU

0

 







1. What are the different components of computer?

Different components of a computer:
Central Processing Unit (CPU): Executes instructions and processes data.
Memory (RAM): Temporarily stores data and instructions for quick access.
Storage (Hard Drive/SSD): Permanently stores data and programs.
Input Devices: Allow users to input data (e.g., keyboard, mouse).
Output Devices: Display results (e.g., monitor, printer).
Motherboard: Connects all components and allows communication between them.

2. What are variables?

Variables are storage locations in memory with a name associated with them. They hold data that can be changed during program execution. In MATLAB, you can create a variable by simply assigning a value to a name, e.g., x = 10;.

3. Name some built in functions in MATLAB.

sin: Computes the sine of an angle.
cos: Computes the cosine of an angle.
sum: Sums all elements of an array.
mean: Computes the mean of an array.
plot: Creates a 2D plot of data.

4. What are Waveforms in MATLAB?

Waveforms are graphical representations of signals over time. In MATLAB, you can generate and analyze various types of waveforms like sine waves, square waves, and sawtooth waves using built-in functions like sin, square, and sawtooth.

5. What are M-files?

M-files are script or function files written in MATLAB. They have a .m extension. Script files contain a sequence of MATLAB commands, while function files define functions that can accept inputs and return outputs.

6. Write the code in MATLAB to add two matrices.
% Define two matrices
A = [1, 2; 3, 4];
B = [5, 6; 7, 8];

% Add the matrices
C = A + B;

% Display the result
disp(C); (code-box)
7. Demonstrate arguments and return values in MATLAB with the help of an
example.

In MATLAB, functions can accept input arguments and return output values. Here's an example:

% Define a function to calculate the area of a rectangle
function area = rectangleArea(length, width)
    % Calculate the area
    area = length * width;
end

% Call the function with arguments and display the result
length = 5;
width = 3;
result = rectangleArea(length, width);
disp(['The area of the rectangle is: ', num2str(result)]); (code-box)

In this example, rectangleArea is a function that takes two input arguments (length and width) and returns the area of the rectangle. The result is displayed using 'disp'.

8. Write a program in MATLAB to find the determinant of a matrix.

% Define a matrix
A = [1, 2, 3; 4, 5, 6; 7, 8, 9];

% Calculate the determinant
determinant = det(A);

% Display the result
disp(['The determinant of the matrix is: ', num2str(determinant)]); (code-box)

This code defines a 3x3 matrix 'A', calculates its determinant using the 'det' function, and displays the result.

9. Draw a graph that joins the points (1, 0), (5, 4) and (2, 1).

% Define the points
x = [1, 5, 2];
y = [0, 4, 1];

% Plot the points and join them with lines
plot(x, y, '-o');

% Add labels and title
xlabel('X-axis');
ylabel('Y-axis');
title('Graph joining the points (1, 0), (5, 4), and (2, 1)');

% Display the grid
grid on; (code-box)

This code plots the points (1, 0), (5, 4), and (2, 1) and joins them with lines.

10. Discuss assignment statement in MATLAB with the help of examples.

An assignment statement in MATLAB is used to assign values to variables. The syntax is 'variable = value;'.

Example: 
% Assign a number to a variable
x = 10;

% Assign the result of an expression to a variable
y = x + 5;

% Assign a string to a variable
name = 'MATLAB';

% Assign a matrix to a variable
A = [1, 2; 3, 4]; (code-box)

In these examples, 'x' is assigned the value 10, 'y' is assigned the result of 'x + 5', 'name' is assigned the string 'MATLAB', and 'A' is assigned a 2x2 matrix.

11. Write a program in MATLAB to arrange ‘n’ numbers in descending order.

% Define an array of numbers
numbers = [3, 1, 4, 1, 5, 9, 2];

% Sort the numbers in descending order
sortedNumbers = sort(numbers, 'descend');

% Display the result
disp('The numbers in descending order are:');
disp(sortedNumbers); (code-box)

This code defines an array of numbers, sorts them in descending order using the 'sort' function with the 'descend' option, and displays the result.

12. Discuss ‘if-else’ in MATLAB.

The if-else statement in MATLAB is used for conditional execution of code. The basic syntax is:

if condition
    % Code to execute if condition is true
elseif another_condition
    % Code to execute if another_condition is true
else
    % Code to execute if all conditions are false
end (code-box)

Example:

% Define a variable
x = 10;

% Use if-else to check conditions
if x > 0
    disp('x is positive');
elseif x < 0
    disp('x is negative');
else
    disp('x is zero');
end (code-box)

In this example, if 'x' is greater than 0, it displays 'x is positive'. If 'x' is less than 0, it displays 'x is negative'. If neither condition is met, it displays 'x is zero'.

13. Write a program in MATLAB to demonstrate binary division with the help of an
example.

Binary division can be demonstrated by converting decimal numbers to binary, performing the division, and then converting the result back to decimal. Here's a step-by-step example:

% Define two binary numbers as strings
binaryNum1 = '1010'; % 10 in decimal
binaryNum2 = '0101'; % 5 in decimal

% Convert binary numbers to decimal
decNum1 = bin2dec(binaryNum1);
decNum2 = bin2dec(binaryNum2);

% Perform division in decimal
quotientDec = floor(decNum1 / decNum2);
remainderDec = mod(decNum1, decNum2);

% Convert the results back to binary
quotientBin = dec2bin(quotientDec);
remainderBin = dec2bin(remainderDec);

% Display the results
disp(['Binary Division: ', binaryNum1, ' / ', binaryNum2]);
disp(['Quotient: ', quotientBin, ' (', num2str(quotientDec), ' in decimal)']);
disp(['Remainder: ', remainderBin, ' (', num2str(remainderDec), ' in decimal)']); (code-box)

14. Write a program in MATLAB to demonstrate polynomial multiplication with the
help of an example.

% Define two polynomials as coefficient vectors
p1 = [1 2 3]; % Represents 1*x^2 + 2*x + 3
p2 = [4 5];   % Represents 4*x + 5

% Perform polynomial multiplication
result = conv(p1, p2);

% Display the result
disp('Polynomial Multiplication:');
disp(['P1(x): ', poly2str(p1, 'x')]);
disp(['P2(x): ', poly2str(p2, 'x')]);
disp(['Result: ', poly2str(result, 'x')]); (code-box)

15. Explain different types of loops in MATLAB with the help of an example.

MATLAB supports for loops and while loops for repeating a block of code multiple times.

  • For Loop:

% For loop example
for i = 1:5
    disp(['Iteration: ', num2str(i)]);
end (code-box)

  • While Loop:

% While loop example
i = 1;
while i <= 5
    disp(['Iteration: ', num2str(i)]);
    i = i + 1;
end (code-box)

16. Write a program in MATLAB to read a text file containing integers and search a
particular item from the list using binary search.

First, create a text file named integers.txt with the following content:

% Read integers from the text file
fileID = fopen('integers.txt', 'r');
integers = fscanf(fileID, '%d');
fclose(fileID);

% Define the item to search for
item = 11;

% Perform binary search
left = 1;
right = length(integers);
found = false;

while left <= right
    mid = floor((left + right) / 2);
    if integers(mid) == item
        found = true;
        break;
    elseif integers(mid) < item
        left = mid + 1;
    else
        right = mid - 1;
    end
end

% Display the result
if found
    disp(['Item ', num2str(item), ' found at index ', num2str(mid)]);
else
    disp(['Item ', num2str(item), ' not found in the list']);
end (code-box)



Post a Comment

0Comments
Post a Comment (0)