Problem Solving Through Programming In C Week 12

Session: JULY-DEC 2023

Course Name: Problem Solving Through Programming In C

Course Link: Click Here

These are Problem Solving Through Programming In C Assignment 12 Answers


Q1. Which of the following are themselves a collection of different data types?
a) String
b) Array
c) Character
d) Structure

Answer: d) Structure


Q2. Which of the following comments about the usage structures is true?
a) Storage class can be assigned to individual member
b) Individual members can be initialized within a structure type declaration
c) The scope of the member name is confined to the particular structure, within which it is defined
d) None of the above

Answer: c) The scope of the member name is confined to the particular structure, within which it is defined


Q3. What is actually passed if you pass a structure variable to a function?
a) Copy of structure variable
b) Reference of structure variable
c) Starting address of structure variable
d) Ending address of structure variable

Answer: a) Copy of structure variable


These are Problem Solving Through Programming In C Assignment 12 Answers


Q4. Which function is used to write a string to a file?
a) fputs()
b) fprintf()
c) fwrite()
d) All of the above

Answer: d) All of the above


Q5. Find the output of the following program
a) abcd
b) bcd
c) cd
d) abcdfgh

Answer: b) bcd


Q6. The program will allocate……. bytes to ptr. Assume sizeof(int) = 4.
a) 8
b) 16
c) 4
d) 32

Answer: a) 8


These are Problem Solving Through Programming In C Assignment 12 Answers


Q7. What is the output of the following C code? Assume that the address of x is 2000 (in decimal) and an integer requires four bytes of memory.
a) 2036, 2036, 2036
b) 2012, 4, 2204
c) 2036, 10, 10
d) 2012, 4, 6

Answer: a) 2036, 2036, 2036


Q8. Can a structure contain a pointer to its own type?
a) Yes
b) No
c) Only as an array
d) Only if the structure is anonymous

Answer: a) Yes


Q9. What is the output of the following code snippet?
a) 1
b) 2
c) 3
d) 4

See also  Problem Solving Through Programming In C Assignment 0

Answer: d) 4


These are Problem Solving Through Programming In C Assignment 12 Answers


Q10. What is the output of the following C program?
a) True
b) False
c) No output
d) Compilation error

Answer: b) False


Assignment

Question 1
Write a program in C to find the factorial of a given number using pointers.

Solution:

void findFact(int n, long int *f)
{
  int ids;
  *f =1;
  for(ids=1;ids <= n;ids++)
    *f=*f*ids;
}

Question 2
Write a C program to print the Record of the Student Merit wise. Here a structure variable is defined which contains student rollno, name and score.

Solution:

struct student tempo;
int j;
for(i=0;i < n-1;i++)
{
  for(j=0;j < n-1;j++)
  {
    if(s[j].score < s[j+1].score)
    {
      tempo=s[j];
      s[j]=s[j+1];
      s[j+1]=tempo;
    }
  }
}

These are Problem Solving Through Programming In C Assignment 11 Answers


Question 3
Write a C program to store n elements using Dynamic Memory Allocation – calloc() and find the Largest element

Solution:

element = (float*) calloc(n, sizeof(float));
if(element == NULL)
{
  printf("Error!!! memory not allocated.");
  exit(0);
}
int ide;
for(ide = 0; ide <  n; ++ide)
{
  scanf("%f", element + ide);
}
for(ide = 1; ide < n; ++ide)
{
  if(*element <  *(element + ide))
    *element = *(element + ide);
}
printf("Largest element = %.2f\n", *element);
return 0;
}

Question 4
Write a C program to find the sum of two 1D integer arrays ‘A’  and ‘B’ of same size and store the result in another array ‘C’, where the size of the array and the elements of the array are taken as input.
In the Test case the input is given as follows
5
10
20
30
40
50
1
2
3
4
5
So the output will be displayed as
Result is
11
22
33
44
55

Solution:

int *a1,*b,*c;
a1 = (int *) malloc(n*sizeof(int));
b = (int *) malloc(n*sizeof(int));
c = (int *) malloc(n*sizeof(int));
for(i=0;i < n;i++)
{
  scanf("%d",a1+i);
}
for(i=0;i < n;i++)
{
  scanf("%d",b+i);
}
for(i=0;i < n;i++)
{
  *(c+i) = *(a1+i) + *(b+i);
}

These are Problem Solving Through Programming In C Assignment 12 Answers

More Weeks of Problem Solving Through Programming In C: Click here

More Nptel Courses: Click here


Session: JAN-APR 2023

Course Name: Problem Solving Through Programming In C

Course Link: Click Here

These are Problem Solving Through Programming In C Assignment 12 Answers


Q1. Which of the following are themselves a collection of different data types?
a) String
b) Array
c) Character
d) Structure

Answer: d) Structure

See also  Problem Solving Through Programming In C Week 4

Q2. Can we declare function inside structure of C Programming?
a) Yes
b) No
c) Compiler dependant
d) Its possible, but causes runtime errors.

Answer: b) No


These are Problem Solving Through Programming In C Assignment 12 Answers


Q3. What is the output of the following C program? #include struct p { }; int x; char y : int main() { } struct p p1[] = {1,21,69,42,64}; struct p ptrl=pl; int x = (sizeof(p1)/4); if ((x == sizeof(int) + 2sizeof(char))) printf(“True”); else printf(“False”); return 0;}
a. True
b. False
c. No output
d. Compilation error

Answer: a. True


Q4. Which of the following statements is true about the equality of two structure variables?
a) Two structure variables are equal if all their members are equal
b) Two structure variables are equal if they have the same address
c) Two structure variables cannot be compared for equality
d) None of the above

Answer: a) Two structure variables are equal if all their members are equal


These are Problem Solving Through Programming In C Assignment 12 Answers


Q5. What will be output? #include int main() { } struct insti { int x = 2; char ins[] = “IIT”; }; struct insti s1; printf(“%d”,s1.ins); printf(“%d”, s1.x); return 0;}
a) IIT
b) 2
c) IIT 2
d) Compilation error

Answer: d) Compilation error


Q6. What is the size of the following structure in bytes? struct example { char c; int i; double d; };
a) 7
b) 12
c) 16
d) 20

Answer: c) 16


These are Problem Solving Through Programming In C Assignment 12 Answers


Q7. What will be output?
a) 5 10 10
b) 10 5 15
c) 10 5 10
d) 10 15 5

Answer: c) 10 5 10


Q8. What is the output of the following C code? Assume that the address of x is 2000 (in decimal) and an integer requires four bytes of memory
a) 2036 2036 2036
b) 2012 4 2204
c) 2036 10 10
d) 2012 4 6

Answer: a) 2036 2036 2036


These are Problem Solving Through Programming In C Assignment 12 Answers


Q9. What is the output of the following code?
a) Hello, world!
b) Hello!
c) world
d) world!

Answer: d) world!

See also  Problem Solving Through Programming in C | Week 1

Q10. Which of the following is a disadvantage of the Trapezoidal Rule?
a) It is computationally expensive
b) It is only accurate for certain types of functions
c) It can produce inaccurate results for functions with rapidly changing curvature
d) It requires a large number of subintervals to achieve high accuracy

Answer: c) It can produce inaccurate results for functions with rapidly changing curvature


These are Problem Solving Through Programming In C Assignment 12 Answers


Problem Solving Through Programming In C Programming Assignment

Question 1

Write a program in C to find the factorial of a given number using pointers.

Solution:

void findFact(int n, long int *f)
		{
        int i;

       *f =1;
       for(i=1;i<=n;i++)
       *f=*f*i;
       }

Question 2

Write a C program to print the Record of the Student Merit wise. Here a structure variable is defined which contains student rollno, name and score.

Solution:

struct student temp;
int j;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1;j++)
{
if(s[j].score<s[j+1].score)
{
temp=s[j];
s[j]=s[j+1];
s[j+1]=temp;
}
}
}

These are Problem Solving Through Programming In C Assignment 12 Answers


Question 3

Write a C program to store n elements using Dynamic Memory Allocation – calloc() and find the Largest element.

Solution:

element = (float*) calloc(n, sizeof(float));

    if(element == NULL)
    {
        printf("Error!!! memory not allocated.");
        exit(0);
    }

    // Stores the number entered by the user.
    int i;
    for(i = 0; i < n; ++i)
    {
        scanf("%f", element + i);
    }

 // find the largest
    for(i = 1; i < n; ++i)
    {
       if(*element < *(element + i))
         *element = *(element + i);
    }


    printf("Largest element = %.2f\n", *element);

    return 0;
}

These are Problem Solving Through Programming In C Assignment 12 Answers


Question 4

Write a C program to add two distance given as input in feet and inches.

Solution:

sum.feet=d1.feet+d2.feet;
    sum.inch=d1.inch+d2.inch;

    if (sum.inch>=12)
    {
        sum.inch=sum.inch-12;
        ++sum.feet;
    }

These are Problem Solving Through Programming In C Assignment 12 Answers

More Weeks of Problem Solving Through Programming In C: Click Here

More Nptel courses: https://progiez.com/nptel


These are Problem Solving Through Programming In C Assignment 12 Answers