In: Computer Science
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;
}
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 :)