0% found this document useful (0 votes)
17 views

SOL Computer Programming MS Winter 2024 25

This document outlines the Winter Mid-Semester Examination for Computer Programming at the Indian Institute of Technology (ISM), Dhanbad, detailing the structure, evaluation schemes, and sample questions for both parts of the exam. Part A focuses on programming concepts, including minimum value calculations, loop comparisons, and diamond pattern printing, while Part B covers flowcharts, substring generation, and array manipulations. Each question includes a specific evaluation scheme to guide grading based on correctness and completeness.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

SOL Computer Programming MS Winter 2024 25

This document outlines the Winter Mid-Semester Examination for Computer Programming at the Indian Institute of Technology (ISM), Dhanbad, detailing the structure, evaluation schemes, and sample questions for both parts of the exam. Part A focuses on programming concepts, including minimum value calculations, loop comparisons, and diamond pattern printing, while Part B covers flowcharts, substring generation, and array manipulations. Each question includes a specific evaluation scheme to guide grading based on correctness and completeness.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

SOLUTION SET

INDIAN INSTITUTE OF TECHNOLOGY (INDIAN SCHOOL OF MINES), DHANBAD


Winter Mid-Semester Examination, Session 2024-25
Examination: II UG (Common) Time: 2 Hours
Subject: Computer Programming (NCSV101) Max. Marks: 60

Part A (Maximum Marks: 30)


Q. No. Question
1. (a)

Evaluation Scheme:
 Only dividing by 4 : 2 marks
 Complete flow chart : 5 marks

(b) #include <stdio.h>


#define MAX_INT 2147483647
int main( )
{
int n, i, m, a[100];
int min, min2, min3;
printf("Enter the number of elements:");
scanf("%d", &n);
for(i = 0; i < n; i++){
scanf("%d",&a[i]);
}
min = MAX_INT;
min2 = MAX_INT;
min3 = MAX_INT;
for(i = 0; i < n; i++){
if(a[i] < min){
min3 = min2;
min2 = min;
min = a[i];
} else if (a[i] < min2) {
min3 = min2;
min2 = a[i];
} else if (a[i] < min3) {
min3 = a[i];
}
}
printf("Enter any integer between 1 and 3 \n");
scanf("%d", &m);
switch (m){
case 1:
printf("Smallest number is: %d\n",min);
break;
case 2:
printf("2nd Smallest number is: %d\n", min2);
break;
case 3:
printf("3rd Smallest number is: %d\n", min3);
break;
default:
printf("invalid option\n");
break;
}
return 0;
}
Evaluation Scheme:
 Only input: 2 marks
 Computing minimum element: 2 marks
 Computing 2nd and 3rd smallest: 3 marks
 Printing the elements: 3 marks
2. (a) i Implicit Conversion:
○ Performed automatically by the compiler when it considers it safe to convert a type
(usually from a smaller to a larger data type).
○ Controlled by the compiler based on type promotion rules.

Explicit Casting:
○ Performed manually by the programmer using the cast operator (type) to force a
conversion.
○ Gives the programmer full control over how and when the conversion happens.

Example: Explicit Casting to Avoid Data Loss:


#include <stdio.h>
int main() {
int a = 10, b = 3;
float result1, result2;
// 1. Implicit Conversion (Integer Division -> possible loss of fractional part)
result1 = a / b; // both a and b are ints, so this is integer division
printf("Result without explicit casting: %f\n", result1);
// Output typically: 3.000000 (fraction lost)
// 2. Explicit Casting (Floating-Point Division -> preserves decimal part)
result2 = (float)a / b;
printf("Result with explicit casting: %f\n", result2);
// Output typically: 3.333333 (fraction preserved)
return 0;
}
Evaluation Scheme:
 Both explanation and example given: 3Marks
 Either explanation or example given: 1½ Marks
 Explanation or example partially correct: 1Mark
 Both incorrect / not answered: 0 Mak

ii Difference between Prefix (++i) and Postfix (i++) Operators:


○ Prefix (++i): Increments the variable first, then uses the updated value in the
expression.
○ Postfix (i++): Uses the variable’s current value in the expression, then increments it
after using that value.
○ Example:
int i = 5;
// Prefix
printf("%d\n", ++i); // Output: 6 (i becomes 6 and 6 is printed)
// Reset i
i = 5;
// Postfix
printf("%d\n", i++); // Output: 5 (i becomes 6 after printing)

Evaluation Scheme:
 Both Explanation and example given: 2 Marks
 Either explanation or example: 1 Mark
 Incorrect/ not answered: 0 Mark

(b) Comparison of Syntax:

for Loop Syntax:


for (initialization; condition; update) {
// loop body
}
 Initialization: Occurs once at the beginning of the loop.
 Condition: Checked before each iteration. If true, the loop body executes; if false, the loop
ends.
 Update: Executes at the end of each iteration (common for increment/decrement).
 Combines initialization, condition check, and update in one line, making it concise for
counter-based loops.

while Loop Syntax:


while (condition) {
// loop body
// (update or other statements)
}
 Condition: Checked before entering the loop body. If true, the body executes; if false, the loop
ends.
 Update: Performed manually inside the loop body (not built into the syntax).

Practical Example:
When to Choose a for Loop
Scenario: You know you need to iterate exactly 10 times to print numbers from 1 to 10.

// 'for' loop is clearer here since we know exactly how many times we iterate.
for(int i = 1; i <= 10; i++) {
printf("%d\n", i);
}

When to Choose a while Loop:


Scenario: You want to read user input until the user enters a negative number.
int number;

printf("Enter a number (negative to stop): ");


scanf("%d", &number);
while(number >= 0) {
// Process the number
printf("You entered: %d\n", number);
// Update
printf("Enter a number (negative to stop): ");
scanf("%d", &number);
}

In the first case, a for loop makes it clear we’re iterating a fixed number of times. In the second, a
while loop is appropriate because the loop continues based on a condition rather than a fixed count.

Evaluation Scheme:
 Both comparison and practical example given: 4Marks
 Comparison and example of either one given: 3 Marks
 Either comparison or example given: 2 Marks
 Partially correct comparison or example: 1 Mark
 Incorrect or not answered: 0 Mark

(c) #include <stdio.h>


int main() {
int n;

// 1. Prompt until an odd integer is entered


while (1) {
printf("Enter an odd integer n (max width): ");
scanf("%d", &n);
if (n % 2 != 0) {
// Valid odd number
break;
} else {
// Even number entered, show error and prompt again
printf("Error: You must enter an odd integer.\n");
}
}

// 2. Print the top half of the diamond (including middle line)


for (int stars = 1; stars <= n; stars += 2) {
// Calculate spaces on each side
int spaces = (n - stars) / 2;
// Print leading spaces
for (int i = 0; i < spaces; i++) {
printf(" ");
}
// Print stars
for (int i = 0; i < stars; i++) {
printf("*");
}
// New line after each row
printf("\n");
}
// 3. Print the bottom half of the diamond
for (int stars = n - 2; stars > 0; stars -= 2) {
int spaces = (n - stars) / 2;
for (int i = 0; i < spaces; i++) {
printf(" ");
}
for (int i = 0; i < stars; i++) {
printf("*");
}
printf("\n");
}
return 0;
}

Evaluation Scheme:
 Completely correct: 6 Marks
 diamond printing correct: 4 Marks
 Even odd check: 2 Marks
 If code correct but slight syntax error or some statement missing: 2 Marks
 Incorrect or not answered: 0 Mark
Part B (Maximum Marks: 30)
Q. No. Question
1. (a) next = first + second + third ;
first = second ;
second = third;
third = next ;

Evaluation Scheme:
 Correct statement: 1 Mark each
 Correct statement, but the order is wrong: ½ Mark

(b) P & Q = (1011)2 & (1101)2 = (1001)2 = (1*23+0*22+0*21+1*20)10 = (9)10


P ^ Q = (1011)2 ^ (1101)2 = (0110)2 = (0*23+1*22+1*21+0*20)10 = (6)10
P << 2 = (101100)2 = (1*25+0*24+1*23+1*22+0*21+0*20)10 = (44)10

Evaluation Scheme:
 Correct answer: 1 Mark each
 Correct answer, but written in binary form: ½ Mark each
 Correct answer is written directly in decimal form: ½ Mark each
 Correct answer, but no computational step: ½ Mark each

(c) #include <stdio.h>


int main( )
{
int a[ ] = {-2,3,4,-1,-2,1,5,-3} ; /* Assign values to an array */
int i, j ;
int s = 0, m = a[0], c, co, loc ;
int n = (int) sizeof(a)/sizeof(a[0]) ;
for(i=0; i <n ; i++) /* Computation of largest subarray */
{
s = a[i] ; c = 0 ;
for(j=i+1; j<n; j++)
{
s = s + a[j] ;
c++ ;
if(s>m)
{
m= s;
loc = i ;
co = c ;
}
}
}
printf("Largest sum subarray is :\n") ; /* Display the largest subarray */
for(i=loc; i<=loc+co; i++)
printf("%d ", a[i]) ;
return 0 ;
}
Evaluation Scheme:
 Assign values to an array: 1 mark
o Assign values at the time of array declaration / during run time: 1 Mark
o Partially correct: ½ Mark
 Computation of largest subarray: 5 Marks
o Fully correct: 5 Marks
o Partially correct (70-80%): 4 Marks
o Partially correct (50-60%): 3 Marks
o Partially correct (30-40%): 2 Marks
o Partially correct (10-20%): 1 Marks
o Attempted, but completely wrong: ½ Mark
 Display the largest subarray: 2 Marks
o Fully correct: 2 Marks
o Partially correct: 1 Marks
o Attempted, but completely wrong: ½ Mark

2. (a) A flowchart is a visual representation of the sequence of steps in a process or algorithm. It uses
different symbols to illustrate the flow of control, making it easier to understand complex logic.

[i] Clarity – It helps programmers and non-programmers understand the logic of an algorithm.
[ii] Debugging – Identifies logical errors before coding.
[iii]Efficiency – Helps in planning and organizing code structure before implementation.
[iv] Communication – Provides a clear way to explain an algorithm to others.

Evaluation Scheme:
 Flowchart Explanation: 2 Marks
 Correct Flowchart: 5 Marks
 Pseudo Code / C Program (as an alternative to the Flowchart): 2 Marks
(b) #include <stdio.h>
int main() {
char str[100]; // Array to store input string
int length = 0;

// Get input from user


printf("Enter a string (without spaces): ");
scanf("%s", str);

// Calculate string length


while (str[length] != '\0') {
length++;
}
printf("\n All possible substrings are:\n");

// First loop - Starting point of substring


for (int start = 0; start < length; start++) {
// Second loop - Length of substring
// For each starting point, we try all possible lengths
for (int end = start; end < length; end++) {
// Print substring from index 'start' to 'end'
for (int i = start; i <= end; i++) {
printf("%c", str[i]);
}
printf("\n");
}
}
return 0;
}

Evaluation Scheme:
 Reading String: 2 Marks
 Finding Substring: 6 Marks
o If the approach is correct but contains errors in implementation: 2 Marks
o If the substring search is implemented but has minor logical errors: 4 Marks
o If the substring is correctly found and implemented efficiently: 6 Marks

You might also like