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

  

               An Introduction To Programming Through C++


IIT Bombay.



Week 8- Programming Assignment 2:


The partial program given below reads in a digit given in letters (e.g. "seven") and is expected to print it as a single number (e.g. 7 in this case). You are to write the important function in this: 'find'. This takes as arguments a two dimensional array in the ith row of which is stored the text name of digit i, number of 'names' in the array i.e. value 10 & a char array that we call 'target'- this would contain the  literal/name of the digit input to the program. If the string stored in target equals the string stored in the ith row of the two dimensional array, then find should return i.  If the string in target is not stored in any row, find should return -1.
Your function find can use the function compare discussed in the lecture. The function compare as well as the line "include <simplecpp>" are stored in the top of the text box already.  The main() program is likewise already stored in the bottom part of the text box.You have to write only the find function. Please note that the two dimensional array that find uses - the one defined in the main() function, stores the 'names' of digits in lower case only. For your reference, the function compare & the main() program are also given below.

#include <simplecpp>
char compare(const char *a, const char *b)
{
  int i=0;
  while(true){
    if(a[i]=='\0' && b[i]=='\0')
      return '=';
    if(a[i]<b[i]) return '<';
    if(a[i]>b[i]) return '>';
    i++;
  }
}

// function find appears here
int main(){
  char digits[10][20] = {"zero", "one", "two", "three", "four",
                         "five", "six", "seven", "eight", "nine"};
  char target[20];
  cin.getline(target,20);
  cout <<find(digits, 10, target)<<endl;
}

Sample Test Cases
Input Output
Test Case 1
nine
9
Test Case 2
ninety
-1
WRITE/COPY THESE CODE DIRECTLY IN NPTEL TERMINAL AND COMPILE AND RUN THEN SUBMIT.

PROGRAM:
#include <iostream>
using namespace std;
int find(char digits[][20], int sizes, char target[])
{
   for(int i =0; i < sizes ; ++i)
  {
    if(compare(digits[i],target) == '=')
     {
        return i;
     }
  }
  return -1;
}


WEEK-8 :PROGRAMMING ASSIGNMENT 1 AND 3  CLICK ON BELOW LINK.
<<CLICK HERE FOR  WEEK 7:PROGRAMMING -01>>
<<CLICK HERE FOR  WEEK 7: PROGRAMMING-03>> 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.

3 comments:

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

Copyright (c) 2020 Custom Programs All Right Reserved

Pages