Skip to content

VNSGU BCA Sem 2: Programming Skills Using C (204) Practical Solutions - Set B

Paper Details

  • Subject: Programming Skills Using C
  • Subject Code: 204
  • Set: B
  • Semester: 2
  • Month/Year: April 2025
  • Max Marks: 25
  • Time Recommendation: 45 Minutes
  • Paper: View Paper | Download PDF

Questions & Solutions

All questions are compulsory

Q1: Matrix Operations

Max Marks: 10

Write a C program to accept the values of 3x3 matrix and perform following operations on it. 1. Display transpose of matrix 2. Display sum of diagonal of matrix

1. Matrix Input & Structure

Accept a 3x3 grid from the user.

Hint

Use a 2D array matrix[3][3] and nested for loops to read values.

flowchart TD
start[Start]
input[Accept 9 Values]
store[Store in 3x3 Array]
done[Matrix Ready]

start --> input
input --> store
store --> done
View Solution & Output
#include <stdio.h>

int main() {
    int matrix[3][3], i, j, sum = 0;

    // [1] Input values
    printf("Enter elements for 3x3 matrix:\n");
    for(i = 0; i < 3; i++) {
        for(j = 0; j < 3; j++) {
            printf("Element [%d][%d]: ", i, j);
            scanf("%d", &matrix[i][j]);
        }
    }

    // [2] Transpose Logic
    printf("\nTranspose of Matrix:\n");
    for(i = 0; i < 3; i++) {
        for(j = 0; j < 3; j++) {
            printf("%d\t", matrix[j][i]);
        }
        printf("\n");
    }

    // [3] Diagonal Sum Logic
    for(i = 0; i < 3; i++) {
        sum += matrix[i][i];
    }
    printf("\nSum of Diagonal Elements: %d\n", sum);

    return 0;
}

Step-by-Step Explanation: 1. Initialization: Declare a 3x3 integer matrix and a sum variable to store the diagonal total. 2. Logic Flow: Use nested loops for input, then print the transpose by swapping row/column indices and add matrix[i][i] to sum. 3. Completion: Output the resulting transpose matrix and the sum of its diagonal elements.

Output:

Enter elements for 3x3 matrix:
...
Transpose of Matrix:
1   4   7   
2   5   8   
3   6   9   

Sum of Diagonal Elements: 15

Q2: Python Data Frame Analysis

Max Marks: 10

Write a Python program to create a data frame from a dictionary and display it. Dictionary contains information of students like Roll No, Name and Total marks (out of 500). Also display student records in descending order of Total marks.

1. DataFrame Creation & Sorting

Convert student records into a table and sort them by performance.

Hint

Use pd.DataFrame(dict) to create the table and df.sort_values(by='Total marks', ascending=False) for sorting.

flowchart TD
dict[Create Dictionary]
df[pd.DataFrame Conversion]
sort[sort_values Descending]
show[Display Result]

dict --> df
df --> sort
sort --> show
View Solution & Output
import pandas as pd

# [1] Create dictionary
student_dict = {
    'Roll No': [101, 102, 103, 104, 105],
    'Name': ['Rahul', 'Priya', 'Amit', 'Sia', 'Aryan'],
    'Total marks': [450, 380, 490, 410, 425]
}

# [2] Convert to DataFrame
df = pd.DataFrame(student_dict)
print("Original DataFrame:")
print(df)

# [3] Sort in descending order
sorted_df = df.sort_values(by='Total marks', ascending=False)
print("\nStudents Sorted by Marks (Descending):")
print(sorted_df)

Step-by-Step Explanation: 1. Initialization: Import pandas and create a student dictionary with Roll No, Name, and Total Marks. 2. Logic Flow: Convert the dictionary into a DataFrame and use sort_values with ascending=False to sort by marks. 3. Completion: Display the original DataFrame followed by the sorted results in descending order.

Q3: Viva Preparation

Max Marks: 5

Potential Viva Questions
  1. Q: What is a Transpose of a matrix?
  2. A: A matrix formed by interchanging its rows and columns.
  3. Q: How do you identify diagonal elements in a 2D array?
  4. A: Elements where the row index and column index are equal (e.g., matrix[i][i]).
  5. Q: Why use Pandas DataFrames instead of just Dictionaries?
  6. A: DataFrames provide more powerful features for sorting, filtering, and statistical analysis.
  7. Q: What is the maximum size of an array in C?
  8. A: It depends on the available memory (stack/heap size) and the compiler, but typically it is limited by the maximum value of the size_t type.
  9. Q: How do you handle multiple sorting columns in Pandas?
  10. A: Pass a list to by, e.g., df.sort_values(by=['Marks', 'Name'], ascending=[False, True]).
  11. Q: What does df.head(2) do?
  12. A: It returns only the first 2 rows of the DataFrame.

Common Pitfalls

  • Loop Indices: Ensure nested loops use different variables (e.g., i and j) to avoid infinite loops or logic errors.
  • Library Import: Don't forget import pandas as pd at the start of your Python script.

Quick Navigation

Set Link
Set A Solutions
Set B Current Page
Set C Solutions
Set D Solutions
Set E Solutions
Set F Solutions

Last Updated: April 2025