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


  

               An Introduction To Programming Through C++


IIT Bombay.



Week 10- Programming Assignment 1:



You are required to maintain number of patients infected by corona per country. You are given data which needs to be updated as and when new data is received.

The input line has two parts
1) string: Country, EXIT is specify end of input.
3) int: count

At the end print count per country (sorted by country name). Following is the sample input and expected output.
Input:
China 10
USA 5
China -5
USA -5
China -5
USA 5
EXIT

Output:

China:0
USA:5
Public Test Cases:

Public Test Cases: 3 / 3 Passed

Public Test Cases Input Expected Output Actual Output Status
Test Case 1
China 10
USA 5
China -5
USA -5
China -5
USA 5
EXIT

China:0\n
USA:5\n
China:0\n
USA:5\n
Passed

Test Case 2
Italy 5
China 100
China -10
Spain 20
France 7
Italy 25
India 20
Italy -20
India -10
India 5
India -5
EXIT

China:90\n
France:7\n
India:10\n
Italy:10\n
Spain:20\n
China:90\n
France:7\n
India:10\n
Italy:10\n
Spain:20\n
Passed

Test Case 3
China 100
Italy 5
China 120
Spain 20
France 7
Italy 25
France 0
Italy 40
France 2
Italy 0
EXIT

China:220\n
France:9\n
Italy:70\n
Spain:20\n
China:220\n
France:9\n
Italy:70\n
Spain:20\n
Passed


WRITE/COPY THESE CODE DIRECTLY IN NPTEL TERMINAL AND COMPILE AND RUN THEN SUBMIT.

PROGRAM:

#include <iostream>
#include <string.h>
#include <stdio.h>


using namespace std; 

struct a1
{
 char country[100];
 int count;
};
void swap (a1 &x,a1 &y)
{
 a1 temp;
 temp=x;
 x=y;
 y=temp;
}
 
void sortcountry(a1 x[],int n)
{
 for (int i=0;i<n-1;i++)
  for (int j=i+1;j<n;j++)
   if (strcmp(x[i].country,x[j].country)>0)
    swap(x[i],x[j]);
}
 
void display(a1 x[],int n)
{
    string a2;

 for (int i=0;i<n;i++)
 {
     a2=x[i].country;
     if(a2=="EXIT")
         break;
     cout<<x[i].country<<":"<<x[i].count<<endl;    
 }
}

int arr_len(a1 x[])
{
    string a2;int i;
    for (i=0;i<100;i++)
 {
     a2=x[i].country;
     if(a2=="EXIT")
         break;
 }
 return i;
}

int search(a1 a3[],char a4[],int n)
{
    int i,a5;
    for (i=0;i<n;i++)
 {
     if(strcmp(a4,a3[i].country)==0)
     {
         cin>>a5;
         a3[i].count=a3[i].count+a5;
         
         return 1;
     }
 }
 return 0;
}


int main()
{
 a1 a4[100];
 int n=10;
    char a6[100];
 for (int i=0;;i++)
 {
 cin>>a6;
 if(search(a4,a6,i))
 {
     i=i-1;
     continue;
 }
    strcpy(a4[i].country,a6);
 if(strcmp(a6,"EXIT")==0)
  {   break; a4[i].count=0;}
 cin>>a4[i].count;
 
    }
 
 sortcountry(a4,arr_len(a4));
 display(a4,arr_len(a4));
 return 0;
}
 
WEEK-10 :PROGRAMMING ASSIGNMENT 2 AND 3 
CLICK ON BELOW LINK.
<<CLICK HERE FOR  WEEK 10:PROGRAMMING -02>>
<<CLICK HERE FOR  WEEK 10: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.

1 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