SEC1-P2 Computer Science 2023 PYQ NBU

0

 






1. Discuss machine code.

Machine code is a low-level programming language consisting of binary instructions that a computer's CPU can execute directly. Each instruction performs a very specific task, such as arithmetic operations or memory access, and is unique to the architecture of the CPU.

2. How do you use variables in MATLAB?

In MATLAB, variables are created by simply assigning a value to a name. For example:

x = 5;     % Assigns the value 5 to the variable x
y = x + 3; % Uses the value of x to assign a new value to y
z = 'Hello'; % Assigns a string to the variable z (code-box)

3. How do you use arguments and return values in MATLAB?

Functions in MATLAB can take input arguments and return output values. For example:

function result = add(a, b)
    result = a + b;
end

% Calling the function
sum = add(3, 4); % sum will be 7 (code-box)

4. What are control statements?

Control statements in MATLAB manage the flow of execution. Common control statements include:
if-else: Conditional execution.
for: Looping a specific number of times.
while: Looping while a condition is true.
switch: Multi-way branching based on the value of an expression.

5. What are M-files?

M-files are files that contain MATLAB code and have a .m extension. They can be either scripts or functions. Scripts run a series of commands, while function M-files define reusable functions with inputs and outputs.

6. What is randomising a list?

Randomising a list means rearranging its elements in a random order. In MATLAB, this can be done using the 'randperm' function. For example:

list = [1, 2, 3, 4, 5];
randomList = list(randperm(length(list)));
% randomList will be a random permutation of [1, 2, 3, 4, 5] (code-box)

7. WAP in MATLAB to find the sum of first 'n' even natural numbers.

% Define the number of terms
n = 10;

% Initialize the sum
sum_even = 0;

% Loop through the first 'n' even natural numbers
for k = 1:n
    sum_even = sum_even + 2*k;
end

% Display the result
disp(['The sum of the first ', num2str(n), ' even natural numbers is: ', num2str(sum_even)]); (code-box)

This code calculates the sum of the first n even natural numbers by iterating through them and summing them up.

8. WAP in MATLAB to count the number of characters in a string.

% Define the string
str = 'Hello, MATLAB!';

% Count the number of characters
num_chars = length(str);

% Display the result
disp(['The number of characters in the string is: ', num2str(num_chars)]); (code-box)

This code uses the length function to count the number of characters in the string str.

9. Discuss graph plotting in MATLAB.

MATLAB provides powerful tools for plotting graphs. Here are the basic steps and functions:

  • Creating a Plot:

x = 0:0.1:10; % Define the x-axis values
y = sin(x);   % Define the y-axis values
plot(x, y);   % Plot the graph (code-box)

  • Adding Labels and Titles:

xlabel('X-axis');
ylabel('Y-axis');
title('Sine Wave'); (code-box)

  • Customizing the Plot:

plot(x, y, 'r--'); % Red dashed line
grid on;           % Add grid lines (code-box)

  • Multiple Plots in One Figure:

y1 = sin(x);
y2 = cos(x);
plot(x, y1, 'b', x, y2, 'r');
legend('Sine', 'Cosine'); (code-box)

These commands help in creating, labeling, and customizing plots in MATLAB. 

11. WAP in MATLAB to arrange some integers in ascending order using bubble sort.

% Define the array of integers
arr = [5, 2, 9, 1, 5, 6];

% Get the length of the array
n = length(arr);

% Bubble Sort algorithm
for i = 1:n-1
    for j = 1:n-i
        if arr(j) > arr(j+1)
            % Swap the elements
            temp = arr(j);
            arr(j) = arr(j+1);
            arr(j+1) = temp;
        end
    end
end

% Display the sorted array
disp('The sorted array in ascending order is:');
disp(arr); (code-box)

12. Discuss different string handling functions available in MATLAB.

MATLAB provides various functions for string handling, including:

  • strcat: Concatenates strings.

str1 = 'Hello';
str2 = 'World';
result = strcat(str1, ' ', str2); % 'Hello World' (code-box)

  • strfind: Finds occurrences of a substring.

str = 'Hello World';
idx = strfind(str, 'World'); % Returns the starting index of 'World' (code-box)

  • strcmp: Compares two strings for equality.

isEqual = strcmp('MATLAB', 'MATLAB'); % Returns 1 (true) (code-box)

  • strrep: Replaces occurrences of a substring.

str = 'Hello World';
newStr = strrep(str, 'World', 'MATLAB'); % 'Hello MATLAB' (code-box)

  • lower and upper: Converts strings to lower or upper case.

str = 'Hello World';
lowerStr = lower(str); % 'hello world'
upperStr = upper(str); % 'HELLO WORLD' (code-box)

  • sprintf: Formats data into a string.

num = 10;
str = sprintf('The number is %d', num); % 'The number is 10' (code-box)

These functions allow for a wide range of string manipulations in MATLAB.

13. WAP in MATLAB to multiply two matrix of order 4x4.

% Define two 4x4 matrices
A = [1, 2, 3, 4; 5, 6, 7, 8; 9, 10, 11, 12; 13, 14, 15, 16];
B = [16, 15, 14, 13; 12, 11, 10, 9; 8, 7, 6, 5; 4, 3, 2, 1];

% Multiply the matrices
C = A * B;

% Display the result
disp('The product of matrices A and B is:');
disp(C); (code-box)

14. WAP in MATLAB to find the roots of the polynomial p(x) = x^4 + 8x^3 + 25x^2 - 40x + 30

% Define the polynomial coefficients
p = [1 8 25 -40 30];

% Find the roots of the polynomial
roots_p = roots(p);

% Display the roots
disp('The roots of the polynomial p(x) = x^4 + 8x^3 + 25x^2 - 40x + 30 are:');
disp(roots_p); (code-box)

15. WAP in MATLAB to create a script file which will call two functions circle and rectangle where circle calculates the area of circle and draw the circle and rectangle calculates the area of rectangle and draw the same. The inputs are radius of circle and length and width of rectangle.

Script File (main.m):

% Input radius of circle
radius = 5;

% Input length and width of rectangle
length = 10;
width = 4;

% Call the circle function
[area_circle, h_circle] = circle(radius);
disp(['Area of the circle: ', num2str(area_circle)]);

% Call the rectangle function
[area_rectangle, h_rectangle] = rectangle(length, width);
disp(['Area of the rectangle: ', num2str(area_rectangle)]); (code-box)

Function File for Circle (circle.m):

function [area, h] = circle(radius)
    % Calculate the area of the circle
    area = pi * radius^2;
    
    % Draw the circle
    theta = linspace(0, 2*pi, 100);
    x = radius * cos(theta);
    y = radius * sin(theta);
    h = figure;
    plot(x, y);
    axis equal;
    title('Circle');
    xlabel('X-axis');
    ylabel('Y-axis');
end (code-box)

Function File for Rectangle (rectangle.m):

function [area, h] = rectangle(length, width)
    % Calculate the area of the rectangle
    area = length * width;
    
    % Draw the rectangle
    x = [0, length, length, 0, 0];
    y = [0, 0, width, width, 0];
    h = figure;
    plot(x, y);
    axis equal;
    title('Rectangle');
    xlabel('X-axis');
    ylabel('Y-axis');
end (code-box)

16. Discuss different types of control statements used in MATLAB with the help of examples.

MATLAB provides several control statements to manage the flow of execution:

if-else Statement:

% Example of if-else statement
x = 5;
if x > 0
    disp('x is positive');
elseif x < 0
    disp('x is negative');
else
    disp('x is zero');
end (code-box)

for Loop:

% Example of for loop
for i = 1:5
    disp(['Iteration: ', num2str(i)]);
end

while Loop:

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

switch Statement:

% Example of switch statement
x = 3;
switch x
    case 1
        disp('x is 1');
    case 2
        disp('x is 2');
    case 3
        disp('x is 3');
    otherwise
        disp('x is something else');
end (code-box)


Post a Comment

0Comments
Post a Comment (0)