Question

In: Computer Science

Write up to 10 lines to each code explain the logic used behind these code pls!!!!!...

Write up to 10 lines to each code explain the logic used behind these code pls!!!!!

(10 lines for each please)

1.

#include<iostream>

using namespace std;

int main()

{

       int id, reverse[20], count = 0, min = 0, max = 0;

       cout << "\nStudent ID Number is \n";

       cin >> id;

       while (id != 0)

       {

             reverse[count] = id % 10;

             if (count == 0)

             {

                    min = reverse[count];

                    max = min;

             }

             else

             {

                    if (min > reverse[count]) min = reverse[count];

                    if (max < reverse[count]) max = reverse[count];

             }

             count++;

             id = id / 10;

       }

       cout << "Reverse Number is \n";

       for (int i = 0; i < count; i++)

             cout << reverse[i];

       cout << "\nLargest Number is \n" << max;

       cout << "\nSmallest Number is \n" << min;

}

......................................

2.

#include <iostream>

using namespace std;

int main()

{

    //printing the initial table structure of the output

    cout<<"\t\t\t\t\t2019 Cricket WorldCup\n";

    cout<<"Countries\t\tMatches Played Matches Won Matches Lost Draw/Rain Total Points\n";

    cout<<"---------\t\t-------------- ----------- ------------ --------- ------------\n";

   

    //Declaring variables

    //Extra spaces are included in the names to align the data in the output

    char team[][20] = {"India      ","New Zealand","Australia","West Indies","South Africa","England   "};

   

    //Declaring and initializing the given data

    int match_won[] = {3,2,0,1,1,1};

    int match_lost[] = {0,0,3,2,0,3};

    int draw_rain[] = {1,1,0,1,1,0};

   

    //Declaring arrays for storing the calculated values

    int match_played[6];

    int total_point[6];

    int total[] = {0,0,0,0,0};

   

    //for loop to find the values in missing columns

    for(int i=0 ; i<6 ; i++ ) {

   

        match_played[i] = match_won[i] + match_lost[i] + draw_rain[i];

        total_point[i] = (2 * match_won[i]) + (-1*match_lost[i]) + (1 * draw_rain[i]);

        total[0] = total[0] + match_played[i];

        total[1] = total[1] + match_won[i];

        total[2] = total[2] + match_lost[i];

        total[3] = total[3] + draw_rain[i];

        total[4] = total[4] + total_point[i];

    }

   

    //printing the calculated values and rest of the table

    for(int i=0 ; i<6 ; i++) {

        cout<<team[i]<<"\t\t\t"<<match_played[i]<<"\t\t"<<match_won[i]<<"\t"<<match_lost[i]<<"\t\t"<<draw_rain[i]<<"\t\t"<<total_point[i]<<"\n";

    }

    cout<<"Total      "<<"\t\t\t"<<total[0]<<"\t\t"<<total[1]<<"\t"<<total[2]<<"\t\t"<<total[3]<<"\t\t"<<total[4]<<"\n";

  

   return 0;

}

Solutions

Expert Solution

1.

For the first program, the main functionality of the program is to take a number as input could be anything, but here we are taking a student ID as input.

We then take one number at a time starting from the last number.

For each number, we also check if the number is the minimum among all numbers or maximum among all numbers.

To start with, we first take input as an integer for variable id. We initialize 4 more variables.

reverse- an array of maximum size 20 which would store integers. The maximum size is 20, this does not indicate the size of our integer id. If id has numbers greater than 20, our program would throw an error.

count-It is used to keep track on which array cell we last stored a number in.

min=Used to store the minimum number

max=Used to store the maximum number.

Now lets take a random number, this would help you understand the program better. So let id=10982826

so id!=0, we store the last digit in reverse[0]=6. You can check 10982826%10 will give you 6, or the last number.

Next we check if count==0, yes, so min=reverse[0]=6 and max=6.

we now remove the last number from id(id=id/10), so id is now 1098282 and count++ makes count=1

Again we check id!=0, we store the present last digit in reverse[1]=2. We then check if count==0, no its not,

So we check if 6>2? yes it is so, min=2

we check if 6<2? no, so max stays as 6.

again we will perform id=id/10 and our new id=109828, and count=2.

We now keep performing the same steps until finally our id=0 after which we end the loop and we print all the details.

In the end, we would be printing, 62828901 which is the reverse of the number we started with.

We would also print max=9 and min=2

2.

For this question, we are going to print a tabular form of a match, the table would indicate the teams that have played the world cup. the matches they have won, the matches they have lost, the matches that ended up in a tie, the total points they have earned.

Apart from that we will also print the total of all these information.

So, to start with, we have 6 teams, the names of these teams are stored in char team array.

just below this we have an array of integers:

match_won: this is an array used to indicate the number of matches won by each team. so the first cell represents number of matches won by india, 2nd cell represents match won by New Zealand and so on.

This representation would be same for all the 3 arrays specified above. The 1st cell for india, 2nd for newzealand, 3rd for australia and so on.

So, the second array match_lost represents the matches lost by each team. The representation specified above follows.

3rd array draw_rain represents the matches that drew due to rain.

Now, we again have 3 other arrays,

1. match played: This will again follow the above representation. This will indicate the number of matches played by each team. We can calculate this by adding the matches won, the matches lost and the matches that drew due to rain.

2. total_points: Follows the same representation, this indicates the total points for each team. here they have given 2 points to match won, so if a team has won 3 matches then 3*2=6. -1 for matches lost, so if a team has lost 2 matches then -1*2=-2. and if matches drew then 1 point. so for 4 matches drawn 1*4=4

so suppose for this figurative team, the total points would be 6-2+4=8(just an example, does not represent any of the teams above)

3. total: now total does not follow the above representation. Total is used to store the total of these following fields. I have represented cell number and what total it represents

0th cell: Total matches played by all the teams.

1st cell: Total matches won by all the teams

2nd cell: total matches lost by all the teams.

3rd cell: total matches drawn due to rain for all the teams

4th cell: total points earned by all the teams.

So you see, the total array follows a different representation compared to all other arrays which are used for representation of each country.

in the end you just print all the values out in a tabular form.

If you have any doubt regarding any of the 2 questions and require a clarification do let me know in the comment section and i will answer it. Please, do give a like if you like my answer, would mean a lot. Thank you :)


Related Solutions

Write up to 10 lines to explain the logic used behind this code pls!!!!! *****************************************/ #include...
Write up to 10 lines to explain the logic used behind this code pls!!!!! *****************************************/ #include <iostream> #include <string> #include <iomanip> #include<activaut.h> #include<activdbg.h> using namespace std; int main() {     int MatchesPlayed[6], MatchesWon[6], MatchesLost[6], MatchesDraw[6], MatchesPoints[6], TotalPoints[6];     MatchesWon[0] = 3; MatchesLost[0] = 0; MatchesDraw[0] = 1; //India     MatchesWon[1] = 2; MatchesLost[1] = 0; MatchesDraw[1] = 1; //NZ     MatchesWon[2] = 0; MatchesLost[2] = 3; MatchesDraw[2] = 0; //Australia     MatchesWon[3] = 1; MatchesLost[3] = 2; MatchesDraw[3] = 1;...
Write up to 10 lines to explain the logic used behind this code pls!!!!! #include <iostream>...
Write up to 10 lines to explain the logic used behind this code pls!!!!! #include <iostream> #include <iomanip> #include<cmath> #include<AclUI.h> using namespace std; int main() {     int array[][2] = { {29,60} , {33,80} , {45,90} , {57,52} , {12,44} , {21,78} , {32,64} , {17,59} }; //given values     int Totalstudents = 0,TotalPass = 0, Average = 0;     for (int i = 0; i < 8; i++)      //students enrooled     {          Totalstudents += array[i][0];     }    ...
Write up to 4 or 5 lines to explain the logic used behind those codes. Separately...
Write up to 4 or 5 lines to explain the logic used behind those codes. Separately for each please 1) #include <iostream> #include <string> #include <fstream> #include <vector> #include <sstream> using namespace std; int main() {         ifstream infile("worldpop.txt");         vector<pair<string, int>> population_directory;         string line;         while(getline(infile, line)){                 if(line.size()>0){                         stringstream ss(line);                         string country;                         int population;                         ss>>country;                         ss>>population;                         population_directory.push_back(make_pair(country, population));                 }         }         cout<<"Task 1"<<endl;         cout<<"Names of countries with population>=1000,000,000"<<endl;...
Please Explain each of them in 10 lines: Please !!! If you write it in your...
Please Explain each of them in 10 lines: Please !!! If you write it in your handwriting, please be clear. answer all questions :) § how a changing magnetic field produces an electric field §Inductance and give examples of applications and relevance § the concept of electro magnetic field in self induction, how is it generated thank you,
List and explain the logic behind the “four functions of money.” Then, consider the following: In...
List and explain the logic behind the “four functions of money.” Then, consider the following: In many prisons, cigarettes are used as money. Do cigarettes in prison have the ability to satisfy these four functions? Explain.
write a java program. 10-20 lines of code You are a landscaper, one of your first...
write a java program. 10-20 lines of code You are a landscaper, one of your first tasks is to determine the cost of landscaping a yard. You charge a flat fee of $40 to landscape a yard, and an extra $10 a bag for raking leaves. Not all yards you work on have leaves that need to be raked. Houses without any leaves will be discounted $5. Using your program, use the JOption Pane to ask the homeowner to enter...
1. Explain the logic behind the application purchasing power price theory to explain changes in the...
1. Explain the logic behind the application purchasing power price theory to explain changes in the spot exchange rate. 2. Assume US interest rates are generally above foreign interest rates. a. what does this sideway about the future strength or weakness of the dollar based on the IFE (international Fisher effect)? b. should US investors invest in foreign securities if they believe in the IFE? c. should foreign investors invest in US securities if they believe in the IFE?
State the “accounting equation” and define each of its terms. What is the logic behind this...
State the “accounting equation” and define each of its terms. What is the logic behind this equation? How is the structure of the balance sheet related to this equation? 15-200 words and no plagiarism please.
State the “accounting equation” and define each of its terms. What is the logic behind this...
State the “accounting equation” and define each of its terms. What is the logic behind this equation? (100 words minimum)
Can someone explain the logic behind this question? The addition of 1 g of which of...
Can someone explain the logic behind this question? The addition of 1 g of which of the following compounds to distilled water would cause the least change in pH? a) Cs(OH) b)HCl c) LiH d) LiOH I know that HCL is a strong acid which elimates that since it will affect PH. In class we had to memorize that group I metals with OH are strong bases making which LiOH and CsOH follow. The answer is A. The solution to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT