WEEK:8 PROGRAMMING ASSIGNMENT 3 SOLUTION (Jan 2020-Apr2020) An Introduction to Programming through C++ NPTEL 2020

  

               An Introduction To Programming Through C++


IIT Bombay.

If any one need test series of online exam mcqs can comment.

Week 8- Programming Assignment 3:


Here is a simple way to represent sets of integers: we store the elements in an array. Suppose we further decide that the elements must be stored in increasing order. Write a function which takes two sets represented in this manner and prints the elements in the intersection of the two in ascending order, one per line.You are to only write the function so that it can be used with the main program below, which has already been entered. For the main() program, you can assume that the elements entered by the user are always in ascending order. Moreover, a set cannot contain any repeated elements.
int main(){
  int s1[20], s2[20], n1, n2;

  //Assume that both n1 & n2 are <= 20.
  cin >> n1;
  for(int i=0; i<n1; i++) cin >>s1[i];
  cin >> n2;
  for(int i=0; i<n2; i++) cin >>s2[i];

  // assuming n1,n2 <= 20 and elements given in increasing order
  printIntersection(s1,n1,s2,n2);
}

Sample Test Cases
Input Output
Test Case 1
7 1 2 3 4 5 6 7
5 2 7 91 92 1007 
2
7
Test Case 2
1 1
5 9 91 92 93 94
WRITE/COPY THESE CODE DIRECTLY IN NPTEL TERMINAL AND COMPILE AND RUN THEN SUBMIT.

PROGRAM:
using namespace std;

void printIntersection(int *s1, int n1, int *s2, int n2)
{
   int i = 0;
   int j = 0;
   while( (i < n1) && (j < n2) )
   {
       if(s1[i] == s2[j])
        {
         cout<< s1[i] <<endl;
         i++; j++;          
        }  
        else if(s1[i] < s2[j])
         {
            i++;  
          }
         else
         {
           j++; 
         } 
   }   
}


WEEK-8 :PROGRAMMING ASSIGNMENT 1 AND 2 
CLICK ON BELOW LINK.
<<CLICK HERE FOR  WEEK 8:PROGRAMMING -01>>
<<CLICK HERE FOR  WEEK 8: PROGRAMMING-02>> FOLLOW OUR WEBSITE FROM THE BUTTON PROVIDED TO THE BOTTOM OF PAGE TO GET SOON ANSWERS OF PROGRAMS.
Disclaimer: Here you can find all nptel assignment solutions related to CS stream.These may help you for your assignment.The answers are only for verification.You cannot copy these directly from the post(code of conduct of NPTEL).These is not 100%  correct solutions.

No comments:

Post a Comment

If you have any problems related to solutions or any concept please let me know.

Copyright (c) 2020 Custom Programs All Right Reserved

Pages