Wednesday, October 14, 2020

NPTEL INTRODUCTION TO PROGRAMMING IN C WEEK 4 ANSWERS (JUL-DEC 2020)

 Question 1:

#include<stdio.h>


int main()

{

int arr1[100], arr2[100];

int n1,n2;

int check = 1;

int i;

  int k;

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]);


for(i = 0; i < n1; i++)//sorting arr1.

{

for(int j = i; j < n1; j++)

{

if(arr1[j]>arr1[i])

{

int temp = arr1[i];

arr1[i] = arr1[j];

arr1[j] = temp;

}

}

}


for (int k = 0; k < n1; k++)//comparing arr1 and arr2.

{

check = 1;

for(i = 0; i < n2; i++)

{

if(arr1[k] == arr2[i])

{

check = 0;

break;

}

}

if(check == 1)

{

printf("%d",arr1[k] );

break;

}


}

if(check == 0)

printf("0");

return 0;


Question 2:

#include<stdio.h>


int main()

{

int arr[100];

int n1,n2;

int counter = 0;

int i;

scanf("%d",&n1);

for( i = 0; i < n1; i++)

scanf("%d",&arr[i]);



for(i = 0; i < n1; i++)

{

for(int j = i; j < n1; j++)

{

if(arr[j]>arr[i])

{

int temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

}

}

}


for( i = 0; i < n1; i++)

{

if(arr[i] != arr[i+1])

counter++;

}

printf("%d",counter );


return 0;


}

Question 3:

#include <stdio.h>

#include <stdlib.h>

#include <string.h>



char* replaceWord(const char* s, const char* oldW,

                  const char* newW)

{

    char* result;

    int i, cnt = 0;

    int newWlen = strlen(newW);

    int oldWlen = strlen(oldW);



    for (i = 0; s[i] != '\0'; i++) {

        if (strstr(&s[i], oldW) == &s[i]) {

            cnt++;



            i += oldWlen - 1;

        }

    }



    result = (char*)malloc(i + cnt * (newWlen - oldWlen) + 1);


    i = 0;

    while (*s) {


        if (strstr(s, oldW) == s) {

            strcpy(&result[i], newW);

            i += newWlen;

            s += oldWlen;

        }

        else

            result[i++] = *s++;

    }


    result[i] = '\0';

    return result;

}



int main()

{

    char str[100];

    char c[2];

    char d[10];


    char* result = NULL;


    scanf("%s",str);

    scanf("%s",c);

    scanf("%s",d);


    result = replaceWord(str, c, d);

    printf("%s", result);


    free(result);

    return 0;

}


Tuesday, October 13, 2020

NPTEL PROGRAMMING IN C++ ASSIGNMENT WEEK 4 ANSWERS 2020(JUL-DEC)

 Question 1 :

#include <iostream>

using namespace std;


class B;             // LINE-1


class A {

    int a_ = 0;

public:

    A(int x) : a_(x) { }

    int addB(B&);

    int subtractB(B&);

};


class B {

    int b_ = 5;

public:

    friend class A;         // LINE-2

};


Question 2:

Complex operator *(const Complex &c) { // LINE-1

        int x, y;

        x = re*(c.re) - im*(c.im);            // LINE-2

        y = im*(c.re) + (c.im)*re;            // LINE-3

        Complex t1(x, y);

        return t1;

    }


Question 3:

        if(data == m1.data) m.data = 1;    // LINE-1

            return m;

    }

};


void fun(myClass m) { // LINE-2


Question 4:

#include <iostream>

using namespace std;


class myClass {

    int data;

    static myClass *t;        // LINE-1 Complete the declaration

    myClass(int x) : data(x) { }

public:

    static myClass *create(int x) {    // LINE-2 Mention return type of the function

        if (!t)

            t = new myClass(x);       // LINE-3 Allocate memory towards object t

            return t;

    }



Saturday, October 10, 2020

NPTEL PROGRAMMING IN JAVA WEEK 4 ANSWERS (JUL-DEC 2020)

 

QUESTION 1:


import java.io.*;
import java.util.*;
import static java.lang.System.*;

QUESTION 2:


// Create an object of Calendar class. 
java.util.Calendar current ;

// Use getInstance() method to initialize the Calendar object.
current = java.util.Calendar.getInstance();

// Initialize the int variable year with the current year
year = current.get(current.YEAR);

QUESTION 3:


interface ExtraLarge{
String extra = "This is extra-large";
void display();
}

class Large {
    public void Print() {
        System.out.println("This is large");
    }
}

class Medium extends Large {
    public void Print() {
       super.Print();  
        System.out.println("This is medium");
    }
}
class Small extends Medium {
    public void Print() {
        super.Print();  
        System.out.println("This is small");
    }
  }


class Question43 implements ExtraLarge{
    public static void main(String[] args) {
        Small s = new Small();
        s.Print();
  Question43 q = new Question43();
  q.display();
    }
  public void display(){
    System.out.println(extra);
  }
}


QUESTION 4:


// Call show() of First interface.
First.super.show();
// Call show() of Second interface.
Second.super.show();

QUESTION 5:


// Interface ShapeX is created
interface ShapeX {
 public String base = "This is Shape1";
 public void display1();
}

// Interface ShapeY is created which extends ShapeX
interface ShapeY extends ShapeX {
 public String base = "This is Shape2";
 public void display2();
}

// Class ShapeG is created which implements ShapeY
class ShapeG implements ShapeY {
 public String base = "This is Shape3";
 //Overriding method in ShapeX interface
 public void display1() {
  System.out.println("Circle: " + ShapeX.base);
 }
 // Override method in ShapeY interface
  public void display2(){
  System.out.println("Circle: " + ShapeY.base);}
}

Tuesday, October 6, 2020

NPTEL PROGRAMMING IN C++ ASSIGNMENT WEEK 3 ANSWERS 2020(JUL-DEC) || CODE LINK IN THE COMMENTS

 Question 1:

 triangle(int b, int h) : _base(&b), _height(&h) { } // Line-1


    // LINE-1: Complete Constructor definition


    ~triangle() {

        _base = 0; // Line-2

        _height = 0;// LINE-2: Complete destructor to delete both data pointers


    }

    double area();

};


double triangle::area() {  // LINE-3: Complete function header

Question 2:

 mutable int _sem;        // LINE-1

public:

    Student(int roll, string name, int sem)

        : _roll(roll), _name(name), _sem(sem) { }


    void promote() const { // LINE-2

        _sem++;

    }


    void display() const{ // LINE-3

Question 3:

#include <iostream>

#include <string>

using namespace std;


class Complex {

    const int _r, _i;


public:

    Complex() : _r(0), _i(0){ }                       // LINE-1


    Complex(int r) : _r(r),_i(0) { }                  // LINE-2


    Complex(int r, int i) : _r(r), _i(i) { }           //          


Question 4 :

integer objA;  // LINE-1: Invoke Default Constructor


    integer objB(val);  // LINE-2: Invoke Parameterized Constructor


    integer objC = objB;  // LINE-3: Invoke Copy Constructor

Monday, October 5, 2020

NPTEL PROGRAMMING IN JAVA WEEK 3 ANSWERS (JUL-DEC 2020)


QUESTION 1:

if (n<=1) 
            return (1-n); 
        return fib(n - 1) + fib(n - 2); 

QUESTION 2:

class Point{
   double x,y;
  
   public double distance(Point p1, Point p2)
                {
                  
                  double root=(p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y);
                  root=Math.sqrt(root);
                  System.out.print(root);
                  return root;
                }
}

QUESTION 3:


//Create a derived class constructor which can call the one parametrized constructor of the base class
double height;
Test1(double l, double h){
  super(l);
  this.length=l;
  this.height=h;
}

//Create a derived class constructor which can call the two parametrized constructor of the base class
Test1(double l, double b, double h){
  super(l,b);
  this.length=l;
  this.breadth=b;
  this.height=h;
}
  
  

//Override the method calculate() in the derived class to find the volume of a shape instead of finding the area of a shape

double calculate(){
  
  return length*breadth*height;}

QUESTION 4:


QuestionScope a= new QuestionScope();
System.out.println(a.sum(n1,n2));
System.out.print(QuestionScope.multiply(n1,n2));

QUESTION 5:


static void swap(Question obj)
        {
          int temp;
          temp=obj.e1;
          obj.e1=obj.e2;
          obj.e2=temp;
        }

Wednesday, September 30, 2020

NPTEL INTRODUCTION TO PROGRAMMING IN C WEEK 3 ANSWERS (JUL-DEC 2020)

 Question 1:

#include<stdio.h>


int find_odd(int k)

{

  int num;

  int counter = 0;

  int check = 0;

  

  while(num != -1)

  {

    scanf("%d",&num);

    if(num%2!=0)

    {

      counter++;

      if(counter == k)

      {

        printf("%d",num);

        check = 1;

      }

    }

    

       

  }

   if((counter != k) && (check == 0))

      printf("-1");


  return 0;

}



int main()

{

  int k;

  scanf("%d",&k);

  find_odd(k);

  return 0;

}

Question 2:

#include<stdio.h>


int main()

{

  float num1,num2;

  float m_avg;

  

  scanf("%f",&num1);

  

  while(num1 != -1)

  {

    num2 = num1;

    scanf("%f",&num1);

      if(num1!=-1)

        { m_avg = (num1+num2)/2;

    printf("%0.1f ",m_avg);

        }

    

  }

  return 0;

}

    

Question 3:


#include<stdio.h>


int main()

{

  int n;

  scanf("%d",&n);

  int fac = 1;

  

  for(int i = 1; i<n; i++)

  {

    fac = fac*i; 

    if(fac <= n)

     {

        printf("%d ",fac);

      }

    else 

      break;

  }

  return 0;

  

}


Thursday, September 24, 2020

NPTEL PROGRAMMING IN JAVA WEEK 2 ANSWERS (JUL-DEC 2020)

 Question 1:

// Create an object of class Student


// Call 'print()' method of class Student 


// Create an object of class School


// Call 'print()' method of class School


Student obj = new Student();


obj.print();


School obj_1 = new School();


obj_1.print();


 Question 2:

// Create an object of class Printer


// Call 'print()' methods for desired output


Printer obj = new Printer();


obj.print("Hi! I am class STUDENT");


obj.print();


 Question 3:

// Define a method named 'studentMethod()' in class Question


// Call the method named 'print()' in class Question


void studentMethod(){

  

  Question23 obj = new Question23();

  print(obj);

}



 Question 4:

class Answer{

Answer(){

System.out.println("You got nothing.");

}

Answer(int marks, String type){

      this();

System.out.print("You got "+marks+" for an "+type);

}

}


 Question 5:

//Declare variable with name 'nptel', 'space' and 'java' and proper datatype.


//Initialize the variables with proper input


String nptel = "NPTEL", space = " ", java = "JAVA";