Problem Solving Through Programming In C NPTEL Assignment 6

                     

Course Name: Problem Solving Through Programming In C NPTEL

Link of course: Click Here

These are answers for Problem Solving Through Programming In C Assignment 6


Q1. What is the right way to initialise an array in C?
a) int ar{}= {1,2, 5,6,9}
b) int ar[5]= {1,2, 5,6,9}
c) int ar {5} = {1,2, 5,6,9}
d) int ar()= {1,2, 5,6,9}

Answer: b


Q2. An integer array of dimension 10 is declared in a C program. The memory location of the first byte of the array is 1000. What will be the location of the 9th element of the array? (Assume integer takes 4 bytes of memory and the element stored at 1000 is identified as 1st element)
a) 1028
b) 1032
c) 1024
d) 1036

Answer: b


Q3. What will be the output after execution of the program?

Answer: a


These are answers for Problem Solving Through Programming In C Assignment 6


Q4. Which of the statements is/are correct?
a) An array may contain more than one element
b) All elements of array have to be of same data type
c) The size of array has to be declared upfront
d) All of the above

Answer: d


Q5. What actually gets passed when you pass an array as an argument to a function
a) Value of elements in array
b) First element of the array
c) Base address of the array
d) Address of the last element of array

Answer: c


Q6. Find the output of the following C program

Answer: c


These are answers for Problem Solving Through Programming In C Assignment 6


Q7. What will be the output?

Answer: c


Q8. An array of the void data type
a) can store any data-type
b) only stores element of similar data type to first element
c) acquires the data type with the highest precision in it
d) It is not possible have an array of void data type

Answer: d


Q9. What will be the output?

Answer: 20


These are answers for Problem Solving Through Programming In C Assignment 6


Q10. How many ‘a’ will be printed when the following code is executed?

Answer: 6


Programming Assignment Answers


These are answers for Problem Solving Through Programming In C Assignment 6


Question 1
Write a C Program to find Largest Element of an Integer Array. 
Here the number of elements in the array ‘n’ and the elements of the array is read from the test data. 
Use the printf statement given below to print the largest element.
printf(“Largest element = %d”, largest);

Solution:

#include < stdio.h>
int main()
{
    int i, n, largest;
    int arr[100];

    scanf("%d", &n);

    for(i = 0; i< n; ++i)
    {
        scanf("%d", &arr[i]);
    }

    largest = arr[0];
    for(i = 1; i < n; ++i)
    {
        if(largest < arr[i])
        largest = arr[i];
    }
    printf("Largest element = %d", largest);
    return 0;
}

These are answers for Problem Solving Through Programming In C Assignment 6


Question 2
Write a C Program to print the array elements in reverse order (Not reverse sorted order. Just the last element will become first element, second last element will become second element and so on) Here the size of the array, ‘n’ and the array elements is accepted from the test case data. The last part i.e. printing the array is also written. 
You have to complete the program so that it prints in the reverse order.

Solution:

#include< stdio.h>
int main()
{
    int arr[20], i, n;

    scanf("%d", &n);

    for(i = 0; i < n; i++)
        scanf("%d", &arr[i]);

    int j, temp;
    j = i - 1;   // last Element of the array
    i = 0;       // first element of the array

    while(i < j)
    {
      temp = arr[i];
      arr[i] = arr[j];
      arr[j] = temp;
      i++;
      j--;
    }

   for(i = 0; i < n; i++)
   {
        printf("%d\n", arr[i]); // For printing the array elements
   }
   return(0);
}

These are answers for Problem Solving Through Programming In C Assignment 6


Question 3
Write a C program to read Two One Dimensional Arrays of same data type (integer type) and merge them into another One Dimensional Array of same type.

Solution:

#include < stdio.h>
int main()
{
    int arr1[20], arr2[20], array_new[40], n1, n2, size, i;

    scanf("%d", &n1);
    for(i = 0; i < n1; i++)
        scanf("%d", &arr1[i]);

    scanf("%d", &n2);

    for(i = 0; i < n2; i++)
        scanf("%d", &arr2[i]);
    int j;
    for(i=0;i< n1;++i)
        array_new[i]=arr1[i];

    size =  n1 + n2;

    for(i=0, j=n1; j< size && i< n2; ++i, ++j)
        array_new[j] = arr2[i];

    //Printing after merging
    for(i = 0; i< size; i++)
    {
        printf("%d\n", array_new[i]);
    }
}

These are answers for Problem Solving Through Programming In C Assignment 6


Question 4
Write a C Program to delete duplicate elements from an array of integers.

Solution:

#include< stdio.h>
int main()
{
    int array[50], i, size;

    scanf("%d", &size);

    for(i = 0; i< size; i++)
        scanf("%d", &array[i]);
    int j, k;
    for(i = 0; i < size; i++) {
        for(j = i + 1; j < size;) {
            if(array[j] == array[i]) {
                for(k = j; k < size; k++) {
                   array[k] = array[k + 1];
                }
                size--;
            }
            else
                j++;
        }
    }
    for(i = 0; i< size; i++)
    {
        printf("%d\n", array[i]);
    }
}

These are answers for Problem Solving Through Programming In C Assignment 6

All weeks of Problem Solving Through Programming In C: https://progies.in/answers/nptel/problem-solving-through-programming-in-c

More NPTEL Solution: https://progies.in/answers/nptel


* The material and content uploaded on this website are for general information and reference purposes only. Please do it by your own first.


These are answers for Problem Solving Through Programming In C Assignment 6