This Blog is protected by DMCA.com

MCS -11, December, 2020, QUESTION AND ANSWER


1. (a) Write an algorithm and draw a corresponding flowchart to find the largest number among 3 numbers given as input. (Mark 10)




 Solution-

Flowchart algorithm for the largest number among three numbers-

1.  Start

2.  Input A, B, and C

3.  If (A>B) and (A>C) then print “A is greater”.

Else if (B>A) and (B>C) then print “B is greater”.

Else print “C is greater”.

4.  Stop


Flowchart



(b) Explain the conditional operator in C with the help of an example. Compare it with if...else statement.(Mark 5)




THE CONDITIONAL OPERATOR IN C 

The conditional operator works on three operands, so it is also known as the ternary operator. It is represented by two symbols, i.e., '?' and ':'. 

If the first operand condition is true, the second operand represents the value entire conditional expression, if is false then the third operand represents the value.



The syntax is as follows:

(condition)? (expression1): (expression2);

If condition is true, expression1 is evaluated else expression2 is evaluated.


Let us see the following examples:

(i) x= (y<20) ? 9: 10;

This means, if (y<20), then x=9 else x=10;






(c) Explain the following user-defined data types : (Mark 5)


(i) typedef
(ii) enum

Enumeration – It helps assign names to integer constants in the program. The keyword 'enum' is used. It is used to increase the readability of the code. 

Typedef – Typedef defines a new name for an existing data type. It does not create a new data class.





(d) What is the difference between a High- level language and a Low-level language?
Why C language is referred to as Middle- level language?

Read 2.2


 High-level language 

1. The language whose design is governed by the circuitry and the structure of the machine is known as the Machine language. This language is difficult to learn and use.

2. These languages have been designed to give better machine efficiency, i.e. faster program execution.

Low-level language

These languages are particularly oriented towards describing the procedures for solving the problem in a concise, precise and unambiguous manner. Every high level language follows a precise set of rules. They are developed to allow application programs to be run on a variety of computers. These languages are machine- independent.










(e) Write a program using pointers, to swap the values of 2 variables.   5




// C program to swap two numbers using pointers
#include <stdio.h>
int main() {
    int a, b, temp;
    int *ptr1, *ptr2;
    printf("Enter the value of a and b: ");
    scanf("%d %d", &a, &b);
    printf("\nBefore swapping a = %d and b = %d", a, b);
    // Assign the memory address of a and b to *ptr1 and *ptr2
    ptr1 = &a;
    ptr2 = &b;
    // Swap the values a and b
    temp = *ptr1;
    *ptr1 = *ptr2;
    *ptr2 = temp;
    printf("\nAfter swapping a = %d and b = %d", a, b);
    return 0;
}






(f) Write a program using file-handling concept, to read and count the no. of characters in a .dot file. 10




Solution

#include<stdio.h>
#include<stdlib.h>
int main()

{

    FILE *fp;
    int count=0;
    char ch;
    fp=fopen("abc.txt","r");
    if (fp==NULL)
    {
        printf("Unable to open a file for reading");
    }
    else
    {
        while((ch=fgetc(fp))!=EOF)

        {
            printf("%c",ch);
            count++;
        }

        printf("\nTotal character are : %d", count );
    }

    return 0;
}






2. (a) Explain type-cast and size-of operators in C with an example to each.  5





TYPE CASTING

TYPE CASTING is nothing but the new way of converting a data type of one variable to some other datatype.

TYPE CASTING is of 2 types:

IMPLICIT TYPECASTING:
 It is done by the compiler and there is no loss of information.

Example

Let x is 8 bit no and y is 16 bit. 
Now Perform addition(x + y) 



EXPLICIT TYPECASTING: 
It is done by the programmer and there will be a loss of information.

EXAMPLE

float x=3.435;
int y = (int) x;











(b) Write a C program to sort the given array of integers in ascending order, using any of the sorting algorithm. Explain the logic. 10



#include<stdio.h>
int main()
{
   int a[5], i, j, temp;

    printf("Enter Array Elements");
    for (i=0;i<5;i++)
    {
        scanf("%d",&a[i]);
    }
     for (i=0;i<5;i++)
     {
         for(j=i+1;j<5;j++)
         {
             if (a[i]>a[j])
             {
                 temp=a[i];
                 a[i]=a[j];
                 a[j]=temp;
             }
         }
     }
                printf("Array Elements :");
                  for (i=0;i<5;i++)
         {
             printf("%d",a[i]);
     }
     return 0;
}





(c) Define a preprocessor in C. How is it implemented? Explain with the help of an example.







**Define a preprocessor in C

Solution

*C preprocessor comes under action before the actual compilation process
* C preprocessor is not a part of the c compiler
*It is a text substitution tool
*All preprocessor commands begin with a hash symbol (#).






3. (a) Write a C program using structures, to find the total, average, and grade for 5 students. 10



#include <stdio.h>

#define NUM_STUDENTS 5

// Define a structure for student
struct Student {
    char name[50];
    int marks[5]; // Assuming 5 subjects
    float total;
    float average;
    char grade;
};

// Function to calculate total marks
float calculateTotal(int marks[], int numSubjects) {
    float total = 0;
    for (int i = 0; i < numSubjects; i++) {
        total += marks[i];
    }
    return total;
}

// Function to calculate average marks
float calculateAverage(float total, int numSubjects) {
    return total / numSubjects;
}

// Function to calculate grade
char calculateGrade(float average) {
    if (average >= 90)
        return 'A';
    else if (average >= 80)
        return 'B';
    else if (average >= 70)
        return 'C';
    else if (average >= 60)
        return 'D';
    else
        return 'F';
}

int main() {
    struct Student students[NUM_STUDENTS];

    // Input details for each student
    for (int i = 0; i < NUM_STUDENTS; i++) {
        printf("Enter details for student %d:\n", i+1);
        printf("Name: ");
        scanf("%s", students[i].name);
        printf("Enter marks for 5 subjects:\n");
        for (int j = 0; j < 5; j++) {
            printf("Subject %d: ", j+1);
            scanf("%d", &students[i].marks[j]);
        }
        // Calculate total and average
        students[i].total = calculateTotal(students[i].marks, 5);
        students[i].average = calculateAverage(students[i].total, 5);
        // Calculate grade
        students[i].grade = calculateGrade(students[i].average);
    }

    // Output details for each student
    printf("\n\n");
    printf("Details for 5 students:\n");
    for (int i = 0; i < NUM_STUDENTS; i++) {
        printf("Student %d:\n", i+1);
        printf("Name: %s\n", students[i].name);
        printf("Total Marks: %.2f\n", students[i].total);
        printf("Average Marks: %.2f\n", students[i].average);
        printf("Grade: %c\n", students[i].grade);
        printf("\n");
    }

    return 0;
}





(b) Write a program to : 10

(i) Find the length of a string.


To find the length of a string in C, you can use the strlen function which is available in the <string.h> library. Here's an example:


#include <stdio.h>
#include <string.h>

int main() {
    char str[100]; // Declare a character array to store the string
    printf("Enter a string: ");
    scanf("%s", str); // Read the string from the user

    int length = strlen(str); // Calculate the length of the string
    printf("Length of the string: %d\n", length);

    return 0;
}
In this example, strlen is used to find the length of the input string stored in the variable str. Remember that strlen doesn't count the null character '\0' at the end of the string.





(ii) Print the reverse of the string.

#include <stdio.h>
#include <string.h>

int main() {
    char str[100];
    int length, i;

    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);

    length = strlen(str);

    printf("Reverse of the string: ");
    for (i = length - 1; i >= 0; i--) {
        printf("%c", str[i]);
    }

    return 0;
}
This program takes a string as input, then iterates through the characters of the string in reverse order and prints them out one by one, effectively printing the reverse of the input string.










4. (a) Write a simple menu program, using switch statement to select an operator on
two 3 × 3 matrices : 10





(i) Addition of two matrices


Solution

#include<stdio.h>
void main()
{
    int A[3][3],B[3][3],C[3][3],i,j;
    printf("Enter 9 numbers of first Matrix: ");
    for(i=0;i<=2;i++)
    for(j=0;j<=2;j++)
    scanf("%d",&A[i][j]);
    printf("Enter 9 numbers of second Matrix: ");

    for(i=0;i<=2;i++)
    for(j=0;j<=2;j++)
    scanf("%d",&B[i][j]);


    printf("Sum of the matrix are \n");

    for(i=0;i<=2;i++)
    {
      for(j=0;j<=2;j++)
      {
          C[i][j]=A[i][j]+B[i][j];

          printf("%d  ",C[i][j]);
      }
      printf("\n\n");
    }
}




(ii) Subtraction of 2 matrices

Solution

#include<stdio.h>
void main()
{
    int A[3][3],B[3][3],C[3][3],i,j;
    printf("Enter 9 numbers of first Matrix: ");
    for(i=0;i<=2;i++)
    for(j=0;j<=2;j++)
    scanf("%d",&A[i][j]);
    printf("Enter 9 numbers of second Matrix: ");

    for(i=0;i<=2;i++)
    for(j=0;j<=2;j++)
    scanf("%d",&B[i][j]);


    printf("Subtraction of the matrix are \n");

    for(i=0;i<=2;i++)
    {
      for(j=0;j<=2;j++)
      {
          C[i][j]=A[i][j]-B[i][j];

          printf("%d  ",C[i][j]);
      }
      printf("\n\n");
    }
}










(b) Write a C program to display the following pattern using “for” loop : 10

5
5 4
5 4 3
5 4 3 2
5 4 3 2 1


Solution


#include<stdio.h>
int main()
{
    int i;
    for(i=5;i>4;i--)
    {
        printf("%d ",i);
    }
    printf("\n");


    for(i=5;i>3;i--)
    {
        printf("%d ",i);
    }
    printf("\n");


    for(i=5;i>2;i--)
    {
        printf("%d ",i);
    }
    printf("\n");


    for(i=5;i>1;i--)
    {
        printf("%d ",i);
    }
    printf("\n");


    for(i=5;i>0;i--)
    {
        printf("%d ",i);
    }
    printf("\n");


    return 0;
}










5. Write short notes on the following with the help of an example mentioning their use in the
programs : 10 × 2 = 20

(a) getch( )

book-298

Solution

The simplest of the console I/O functions are getch(), which reads character from the keyboard, and putch(), which prints a character to the screen.

For Example ,

char ch;
ch=getch();
putch(ch);



(b) void( )

book 147


void Data Type: (for empty set of values and non-returning functions). The void type specifies an empty set of values. It is used as the return type for functions that do not return a value.




(c) gets( )

book 299

The pre-defined C method gets() is utilised to read a string or text line. Additionally, save the input to a string variable that has clear parameters. As soon as it meets a newline character, the function stops reading. After using scanf(), compare the results.


(d) + + (increment operator)
(e) – – (decrement operator)
(f) % operator
(g) break statement
(h) # define
(i) fseek( )
(j) Goto statement






Comments

College Admission

Best Career Option After Class 12 Course List

slider1

Blog E-commerce Auto Slider
Product 1
Product 4
Product 4
Product 4
Product 4
Product 4
Product 4

IIM Calcutta

 

IIM Calcutta

 

Apply For Admission


SUBSCRIBE

This Blog is protected by DMCA.com

Admission

SUBSCRIBE

OlderPost

Contact Form

Name

Email *

Message *

Share