Sample Solution

mmt-001-solved-assignment-2024-ss-8e24e610-06c9-4b43-84f6-a5bf6ef5ab5c

mmt-001-solved-assignment-2024-ss-8e24e610-06c9-4b43-84f6-a5bf6ef5ab5c

MMT-001 Solved Assignment 2024 SS
  1. Write the output of the following C C C\mathrm{C}C codes, with proper justification for each.
    i)
    main()
    {
    int a = 2 , b = 4 , c = 5 a = 2 , b = 4 , c = 5 a=2,b=4,c=5\mathrm{a}=2, \mathrm{~b}=4, \mathrm{c}=5a=2, b=4,c=5;
    a = b + c a = b + c a=b+c\mathrm{a}=\mathrm{b}+\mathrm{c}a=b+c;
    b = a + c b = a + c b=a+c\mathrm{b}=\mathrm{a}+\mathrm{c}b=a+c;
    c = a b c = a b c=a-b\mathrm{c}=\mathrm{a}-\mathrm{b}c=ab;
    printf ("%d,%d,%d", a, b, c);
    }
Answer:
Let’s go through the code step by step:
  1. Initialization:
    • a = 2
    • b = 4
    • c = 5
  2. First Assignment:
    • a = b + c
    • a = 4 + 5
    • a = 9
  3. Second Assignment:
    • b = a + c
    • b = 9 + 5
    • b = 14
  4. Third Assignment:
    • c = a - b
    • c = 9 - 14
    • c = -5
  5. Output:
    • printf("%d,%d,%d", a, b, c);
    • The output will be: 9,14,-5
So, the output of the given C code is 9,14,-5.
ii)
main()
{
printf("%d", ‘C’ + ‘P’ + ‘r’ + ‘o’ + ‘g’ + ‘r’ + ‘a’ + ‘m’);
}
Answer:
In this C code, the printf function is adding the ASCII values of the characters ‘C’, ‘P’, ‘r’, ‘o’, ‘g’, ‘r’, ‘a’, and ‘m’, and then printing the sum as an integer.
Here are the ASCII values for each character:
  • ‘C’ = 67
  • ‘P’ = 80
  • ‘r’ = 114
  • ‘o’ = 111
  • ‘g’ = 103
  • ‘r’ = 114
  • ‘a’ = 97
  • ‘m’ = 109
Adding these values together:
67 + 80 + 114 + 111 + 103 + 114 + 97 + 109 = 795
So, the output of the given C code is 795.
iii)
main()
{
int i , j = 1 i , j = 1 i,j=1i, j=1i,j=1;
for ( i = 1 ; i <= 10 ; i = i + 2 ) i = 1 ; i <= 10 ; i = i + 2 ) i=1;i<=10;i=i+2)i=1 ; i<=10 ; i=i+2)i=1;i<=10;i=i+2)
{
j = i j j = i j j=i-jj=i-jj=ij;
while( j <= 2 j <= 2 j<=2j<=2j<=2 )
printf("%d ", i + j + + ) i + j + + ) i+j++)i+j++)i+j++);
}
}
Answer:
Let’s go through the code step by step:
  1. Initialization:
    • j = 1
  2. For Loop:
    • The loop runs for i = 1, 3, 5, 7, 9.
  3. Inside the For Loop:
    • For each iteration of the for loop, j is updated as j = i - j.
    • Then, a while loop runs as long as j <= 2, printing i + j and incrementing j by 1 each time.
Let’s break down each iteration of the for loop:
  • First Iteration (i = 1):
    • j = 1 - 1 = 0
    • While loop: j <= 2, so it prints 1 + 0 = 1, then j becomes 1 and prints 1 + 1 = 2, then j becomes 2 and prints 1 + 2 = 3, then j becomes 3 and the loop stops.
  • Second Iteration (i = 3):
    • j = 3 - 2 = 1
    • While loop: j <= 2, so it prints 3 + 1 = 4, then j becomes 2 and prints 3 + 2 = 5, then j becomes 3 and the loop stops.
  • Third Iteration (i = 5):
    • j = 5 - 1 = 4
    • While loop: j > 2, so it doesn’t print anything.
  • Fourth Iteration (i = 7):
    • j = 7 - 4 = 3
    • While loop: j > 2, so it doesn’t print anything.
  • Fifth Iteration (i = 9):
    • j = 9 - 3 = 6
    • While loop: j > 2, so it doesn’t print anything.
So, the output of the given C code is 1 2 3 4 5.
iv) main()
{
int a = 10 , b = 20 , c = 30 a = 10 , b = 20 , c = 30 a=10,b=20,c=30\mathrm{a}=10, \mathrm{~b}=20, \mathrm{c}=30a=10, b=20,c=30;
int p = 2 p = 2 **p=2* \mathrm{p}=2p=2;
a = c / p a = c / p a=c//**p\mathrm{a}=\mathrm{c} / * \mathrm{p}a=c/p;
b = c b = c b=c\mathrm{b}=\mathrm{c}b=c;
printf("a = % d , b = % d " , a , b ) % d , b = % d " , a , b ) %d,b=%d”,a,b)\% \mathrm{~d}, \mathrm{~b}=\% \mathrm{~d} “, \mathrm{a}, \mathrm{b})% d, b=% d,a,b);
}
Answer:
There’s an issue in this code. The line int *p = 2; is trying to assign an integer value to a pointer, which is not correct in C. Pointers should be assigned the address of a variable or memory allocated using functions like malloc.
If the intention was to make p a pointer to an integer with the value 2, it should be done like this:
int val = 2;
int *p = &val;
Or, if the intention was to use p as a normal integer variable (not a pointer) with the value 2, it should be declared as:
int p = 2;
Assuming the second case (that p is meant to be a normal integer variable), the corrected code would look like this:
main()
{
    int a = 10, b = 20, c = 30;
    int p = 2;
    a = c / p;
    b = c;
    printf("a = %d, b = %d", a, b);
}
In this corrected code:
  • a is assigned the value of c / p, which is 30 / 2 = 15.
  • b is assigned the value of c, which is 30.
So, the output of the corrected code would be:
a = 15, b = 30
v) func (int x x xxx )
{
static int a = 0 a = 0 a=0a=0a=0;
a + = x a + = x a+=x\mathrm{a}+=\mathrm{x}a+=x;
return(a);
}
main()
{
int p p ppp;
for ( p = 1 ; p <= 5 ; + + p ) ( p = 1 ; p <= 5 ; + + p ) (p=1;p<=5;++p)(p=1 ; p<=5 ;++p)(p=1;p<=5;++p)
printf("%d", fun(p));
}
Answer:
There’s a minor issue in the code: the function name in the printf statement is fun, but the function is defined as func. These names should match. Assuming the function name is intended to be fun, here’s the corrected code:
#include <stdio.h>

int fun(int x)
{
    static int a = 0;
    a += x;
    return a;
}

int main()
{
    int p;
    for (p = 1; p <= 5; ++p)
        printf("%d ", fun(p));
    return 0;
}
Let’s go through the code step by step:
  1. Function fun: This function takes an integer x as an argument and has a static integer variable a. The static variable a retains its value between function calls. In each call, x is added to a, and the updated value of a is returned.
  2. Main Function:
    • A for loop runs from p = 1 to p = 5.
    • In each iteration, the function fun is called with the current value of p, and the returned value is printed.
Here’s what happens in each iteration of the loop:
  • First Iteration (p = 1): a = 0 + 1 = 1, prints 1.
  • Second Iteration (p = 2): a = 1 + 2 = 3, prints 3.
  • Third Iteration (p = 3): a = 3 + 3 = 6, prints 6.
  • Fourth Iteration (p = 4): a = 6 + 4 = 10, prints 10.
  • Fifth Iteration (p = 5): a = 10 + 5 = 15, prints 15.
So, the output of the given C code is 1 3 6 10 15.
  1. (a) Explain the use of the functions getchar(), putchar(), gets() and puts() functions, with a suitable program.
Answer:
The functions getchar(), putchar(), gets(), and puts() are standard input/output functions in C used for character and string handling.
  1. getchar(): This function is used to read a single character from the standard input (usually the keyboard). It doesn’t take any arguments and returns the read character. If there’s an error or end of file is reached, it returns EOF.
  2. putchar(): This function is used to write a single character to the standard output (usually the console). It takes a character as an argument and returns the written character. If there’s an error, it returns EOF.
  3. gets(): This function is used to read a string from the standard input into a buffer until a newline character is encountered or end of file is reached. It’s considered unsafe because it can lead to buffer overflow, so it’s better to use fgets() instead.
  4. puts(): This function is used to write a string to the standard output followed by a newline character. It takes a string as an argument and returns a non-negative number if successful, or EOF if there’s an error.
Here’s a simple program demonstrating the use of these functions:
#include <stdio.h>

int main() {
    char c;
    char str[100];

    printf("Enter a character: ");
    c = getchar(); // Read a character

    printf("You entered: ");
    putchar(c); // Write the character
    printf("\n");

    printf("Enter a string: ");
    gets(str); // Read a string (unsafe, consider using fgets instead)

    printf("You entered: ");
    puts(str); // Write the string with a newline

    return 0;
}
In this program:
  • getchar() is used to read a single character from the user.
  • putchar() is used to display the entered character.
  • gets() is used to read a string from the user (though it’s unsafe and not recommended).
  • puts() is used to display the entered string with a newline at the end.
Note: In modern C programming, it’s recommended to use fgets() instead of gets() for reading strings to avoid buffer overflow issues.
(b) Arrange the following operators in descending order of their priority. If any two operators have the same priority, then specify their associativity.
+ = , % , + , , (unary), ! = , , + + + = , % , + , , (unary), ! = , , + + +=,quad%,quad+,quad**,quad**” (unary), “quad!=,quad-,quad+++=, \quad \%, \quad+, \quad *, \quad * \text { (unary), } \quad !=, \quad-, \quad+++=,%,+,, (unary), !=,,++
Answer:
In C, operators have a defined precedence (priority) and associativity. Here’s the list of the given operators in descending order of their priority, along with their associativity:
  1. * (unary): Unary operators have the highest precedence among the listed operators. Associativity: Right to left.
  2. ++ (unary): Unary increment operator has the same precedence as the unary * operator. Associativity: Right to left.
  3. * (binary): Binary multiplication operator. Associativity: Left to right.
  4. %: Remainder operator has the same precedence as the binary * operator. Associativity: Left to right.
  5. +: Binary addition operator. Associativity: Left to right.
  6. -: Binary subtraction operator has the same precedence as the binary + operator. Associativity: Left to right.
  7. !=: Inequality operator. Associativity: Left to right.
  8. +=: Addition assignment operator has the lowest precedence among the listed operators. Associativity: Right to left.
So, the descending order of priority with associativity is:
  • * (unary), ++ (unary) [Right to left]
  • * (binary), % [Left to right]
  • +, - (binary) [Left to right]
  • != [Left to right]
  • += [Right to left]
(c) Write a loop that examines each character in a character type array called text, and determines how many of the characters are letters, how many are digits, how many are whitespace characters, and how many are other characters.
Answer:
We can use a loop to iterate through each character in the array and use the functions isalpha(), isdigit(), and isspace() from the ctype.h header file to classify each character. Here’s an example in C:
#include <stdio.h>
#include <ctype.h>

int main() {
    char text[] = "Hello, World! 12345 \t\n";
    int letters = 0, digits = 0, whitespace = 0, others = 0;

    for (int i = 0; text[i] != '\0'; i++) {
        if (isalpha(text[i])) {
            letters++;
        } else if (isdigit(text[i])) {
            digits++;
        } else if (isspace(text[i])) {
            whitespace++;
        } else {
            others++;
        }
    }

    printf("Letters: %d\n", letters);
    printf("Digits: %d\n", digits);
    printf("Whitespace characters: %d\n", whitespace);
    printf("Other characters: %d\n", others);

    return 0;
}
In this program:
  • isalpha() checks if a character is an alphabetic letter.
  • isdigit() checks if a character is a digit.
  • isspace() checks if a character is a whitespace character (space, tab, newline, etc.).
  • The loop continues until it encounters the null terminator '\0' at the end of the string.
The counts of each type of character are printed at the end.
Verified Answer
5/5
Scroll to Top
Scroll to Top