Question

In: Computer Science

#include <iostream> #include <iomanip> using namespace std; int main() {     int studentid, numberreverse[20], count =...

#include <iostream>

#include <iomanip>

using namespace std;

int main() {

    int studentid, numberreverse[20], count = 0, maximum = 0, minimum = 0;

    cout << "Enter your student ID number: ";

    cin >> studentid;

    cout << "Student ID Number = " << studentid << endl;

    while (studentid != 0)

    {

         numberreverse[count] = studentid % 10;

         if (count == 0)

         {

             minimum = numberreverse[count];

             maximum = minimum;

         }

         else

         {

             if (minimum > numberreverse[count]) minimum = numberreverse[count];

             if (maximum < numberreverse[count]) maximum = numberreverse[count];

         }

         count++;

         studentid = studentid / 10;

    }

    cout << "\nReversed Number is : ";

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

    cout << numberreverse[i];

    cout << "\nLargest Number is :" << maximum;

    cout << "\nSmallest Number is :" << minimum;

    return 0;

}

can u pls Write up to 10 lines to explain the logic used behind this code?

Solutions

Expert Solution

In the above code, firstly a number is inputted from user in console

Then the number is reversed

After that, we have found the largest and the smallest digit out out of the number

Note: Number can be more than 1 digit as well

Output of the code is as below:

Enter your student ID number: 123451

Student ID number: 123451

Reversed Numbe Is:  154321

Largest Digit: 5

Smallest Digit : 1

______________________________

To explain the login shortly, please find below steps:

Step 1: Declare the array and variables with datatype integer , array with length 20

Step 2: Read input from user , and store in studentId variable. Also, display/print the value in studentId variable

Step 3: With the help of while loop , navigate/traverse through each digit of the number

and store the reverse of the digits in the numberreverse array, each digit at one index

Reverse is taken place by divide and modulus method by 10

Taking remainder or last digit of number and storing at starting index of array

Simililary , reducing the number to length , of 1 less than length , removing the last digit

Step 4: With the help of while loop, compare each digits , and with the help of if-else condition within the loop,

compare each digit and find the maximum and minimum of all the digits in number , And store the smallest digit value is variable minimum and largest digit value in variable maximum

Step 5: Print all the values of array numberreverse at each index in console with the help of FOR loop

Step 6: Print the maximum and minimum values in console

Note: The length of number varies depending on the input received from the user

__________________________

For expalining more clearly and widely with code:

Step 1: Firstly studentid, numberreverse[20], count = 0, maximum = 0, minimum = 0; variables are declared with datatype Integer

numberreverse is and integer array with length 20, count, maximum and minimum variables are initialized with value 0.

Step 2: studentId is inputted from user using predefined object in C++ , cin

cin >> studentId

After taking input from user , studentId value is displayed/printed in console via  predefined object in C++ , cout

cout<< studentId

Step 3: Now, while loop is used to iterate through all the digits present in the studentId

Example: studentId is 123 , so loop will iterate/run 3 times , to navigate through each digit 1, 2 ,3 in the number

Syntax: while(condition){}

Loop will continue executing till the condition in while is true , as the condition goes false, control will exit from the loop

while (studentid != 0)

      

Here, till the studentId value is greater than 0 , loop will continue executing/iterating.

As the studentId becomes 0, control will get out of loop.

Now , starling studentId=123 , that means studentId!=0 , condition becomes true, control will move inside the loop

   numberreverse[count] = studentid % 10;

% = modulus operator

studentId will be divided by 10 and the result remainder will be stored numberreverse array at index count

Intially, we have initialized the count =0,

so , numberreverse[0] = 123 % 10 = 3 [here , by dividing , 12 becomes quotient and 3 remains remainder]

At 0th position of array ->numberreverse[0]=3 [store the value].

Moving to next step , if - else statement is used, if count is equal to 0 , then control goes to if statement , otherwsie , else statement

Here, count =0 , control will go inside if statement

if (count == 0) ,

         {

             minimum = numberreverse[count];

             maximum = minimum;

         }

value in array at count index , 0th index , will be stored in minimum variable

Note : minimum and maximum variables , here used are to find the smallest and largest out of each digit in the number

so, minimum will become 3

minimum =3

and, maximum also become 3,

maximum =3

Moving to next statment,

count variable will be incremented by 1

count++;

count =1

And, new value for studentId will be stored as, last digit is traversed for the number

studentId divided by 10 , quotient will become new value of variable studentId

studentid = studentid / 10;

studentId= 123/10 -> 12

studentId =12 [new value]

Step 4: Now , after changing/updating the values , control will again move towards while condition , to check whether condition is true or false

Now ,

while (studentid != 0)

      

Here, till the studentId value is greater than 0 , loop will continue executing/iterating.

As the studentId becomes 0, control will get out of loop.

Now , studentId=12 , that means studentId !=0 , condition becomes true, control will move inside the loop

   numberreverse[count] = studentid % 10;

% = modulus operator

studentId will be divided by 10 and the result remainder will be stored numberreverse array at index count

Now, the count value is incremented and count=1

so , numberreverse[1] = 12 % 10 = 2 [here , by dividing , 1 becomes quotient and 2 remains remainder]

At 1st position of array ->numberreverse[1]=2 [store the value].

Moving to next step , if - else statement is used, if count is equal to 0 , then control goes to if statement , otherwsie , else statement

Here, count =1 , control will go inside else statement

    else

         {

             if (minimum > numberreverse[count])

minimum = numberreverse[count];

Now , for finding minimum , we have compared the previously stored minimum with newly received digit , if the digit stored at 1st index of array is less than the old minimum value ,

minimum value will be updated with the digit stored at 1st index of array

if (minimum > numberreverse[count]) //checked the statement ,digit stored at 1st index of array is less than the old minimum value

If true ,value will be updated , if false, control will move towards next if statement , without changing/updating minimum value

Here, minimum is 3 and numberreverse[1] is 2 , so the value of array index is less than minimum value, control will move inside the if statement and update the value of minimum variable

Now, minimum will become 2

minimum =2

Moving towards next if statment, checked for the maximum value

    if (maximum < numberreverse[count])

Now , for finding maximum , we have compared the previously stored maximum with newly received digit , if the digit stored at 1st index of array is greater than the old maximum value ,

maximum value will be updated with the digit stored at 1st index of array

   if (maximum < numberreverse[count]) //checked the statement ,digit stored at 1st index of array is greater than the old maximum value

If true ,value will be updated , if false, control will move towards next statement outside the if condition , without changing/updating minimum value

Here, maximum is 3 and numberreverse[1] is 2 , so the value of array index is not greater  than maximum value, control will not move inside the if statement , leaving the old maximum value without any updation

Now, maximum will remain 3

maximum =3

Moving to next statment,

count variable will be incremented by 1

count++;

count =2

And, new value for studentId will be stored as, last digit is traversed for the number

studentId divided by 10 , quotient will become new value of variable studentId

studentid = studentid / 10;

studentId= 12/10 -> 1

studentId =1 [new value]

Step 5: Now , after changing/updating the values , control will again move towards while condition , to check whether condition is true or false

Now ,

while (studentid != 0)

      

Here, till the studentId value is greater than 0 , loop will continue executing/iterating.

As the studentId becomes 0, control will get out of loop.

Now , studentId=1 , that means studentId !=0 , condition becomes true, control will move inside the loop

   numberreverse[count] = studentid % 10;

% = modulus operator

studentId will be divided by 10 and the result remainder will be stored numberreverse array at index count

Now, the count value is incremented and count=2

so , numberreverse[1] = 1 % 10 = 1 [here , by dividing , 0 becomes quotient and 1 remains remainder]

At 2nd  position of array ->numberreverse[2]=1 [store the value].

Moving to next step , if - else statement is used, if count is equal to 0 , then control goes to if statement , otherwsie , else statement

Here, count =2 , control will go inside else statement

    else

         {

             if (minimum > numberreverse[count])

minimum = numberreverse[count];

Now , for finding minimum , we have compared the previously stored minimum with newly received digit , if the digit stored at 2nd index of array is less than the old minimum value ,

minimum value will be updated with the digit stored at 2nd index of array

if (minimum > numberreverse[count]) //checked the statement ,digit stored at 2nd index of array is less than the old minimum value

If true ,value will be updated , if false, control will move towards next if statement , without changing/updating minimum value

Here, minimum is 2 and numberreverse[2] is 1 , so the value of array index is less than minimum value, control will move inside the if statement and update the value of minimum variable

Now, minimum will become 1

minimum =1

Moving towards next if statment, checked for the maximum value

    if (maximum < numberreverse[count])

Now , for finding maximum , we have compared the previously stored maximum with newly received digit , if the digit stored at 2nd index of array is greater than the old maximum value ,

maximum value will be updated with the digit stored at 2nd index of array

   if (maximum < numberreverse[count]) //checked the statement ,digit stored at 2nd index of array is greater than the old maximum value

If true ,value will be updated , if false, control will move towards next statement outside the if condition , without changing/updating minimum value

Here, maximum is 3 and numberreverse[2] is 1 , so the value of array index is not greater  than maximum value, control will not move inside the if statement , leaving the old maximum value without any updation

Now, maximum will remain 3

maximum =3

Moving to next statment,

count variable will be incremented by 1

count++;

count =3

And, new value for studentId will be stored as, last digit is traversed for the number

studentId divided by 10 , quotient will become new value of variable studentId

studentid = studentid / 10;

studentId= 1/10 -> 0

studentId =0 [new value]

Now the newly updated array will be;

numberreverse[0]=3

numberreverse[1]=2

numberreverse[2]=1

Step 6: Now , after changing/updating the values , control will again move towards while condition , to check whether condition is true or false

Now ,

while (studentid != 0)

This condition becomes false here , as studentId=0 and condition becomes false

Control will move out/exit of the loop

Step 7:

Print the statement via  predefined object in C++ , cout

   cout << "\nReversed Number is : ";

Step 8:

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

cout << numberreverse[i];

Here , for loop is used ,

Syntax: for(initial value;condition, incrementor)

Startingly , value of variable i is initilaized to 0

condition is checked , whether true or false , i<count , if it the true, control will move inside the loop

If the condition is false, control will exit the loop

here , count is 3 , incremented from previous While loop

0<3 , --->True

array at value i i.e; 0 is printed, numberreverse[0] which is 3 is printed

now , increment the i variable , i will become 1 ; i++

Check the condition ; 1<3   --->True

array at value i i.e; 1 is printed, numberreverse[1] which is 2 is printed

now , increment the i variable , i will become 2  ; i++

Check the condition ; 2<3 --->True

array at value i i.e; 2 is printed, numberreverse[2] which is 1 is printed

now , increment the i variable , i will become 3 ; i++

Check the condition ; 3<3 , condition become false, control moves out of loop to the immediate next statement

The final reversed string is printed as: 321

Step 9:

Print the statement via  predefined object in C++ , cout , for largest number and value of maximum variable will be displayed

    cout << "\nLargest Number is :" << maximum;

Print the statement via  predefined object in C++ , cout , for smallest number and value of minimum variable will be displayed

    cout << "\nSmallest Number is :" << minimum;

Step 10:

return 0 ; return value of main function and exit the code


Related Solutions

#include <iostream> using namespace std; int main() {     int hour;     int min;     for (hour = 1;...
#include <iostream> using namespace std; int main() {     int hour;     int min;     for (hour = 1; hour <= 12; hour++)     {         for (min = 0; min <= 59; min++)         {             cout << hour << ":" << min << "AM" << endl;         }     }       return 0; } 1.      Type in the above program as time.cpp. Add a comment to include your name and date. Compile and run. 2.      What is the bug or logic error in the above program? Add the...
#include <iostream> using namespace std; void count( int begin[], int end[] ) { int index, current[4]...
#include <iostream> using namespace std; void count( int begin[], int end[] ) { int index, current[4] = { begin[0], begin[1], begin[2], begin[3] }; add: goto print; carry: if ( current[index] < end[index] - 1 ) { current[index]++; goto add; } else if ( index > 0 ) { current[index] = begin[index]; index--; goto carry; } else return; print: cout << current[0] << current[1] << current[2] << current[3] << endl; index = 3; goto carry; } int main( ) { int...
write the algorithm for this the code?!. #include<iostream> using namespace std; #include<string.h> int main() { char...
write the algorithm for this the code?!. #include<iostream> using namespace std; #include<string.h> int main() { char plain[50], cipher[50]="", decrypt[50]=""; int subkeys[50], len;       cout<<"Enter the plain text:"<<endl; cin>>plain;    cout<<"Enter the first subkey:"<<endl; cin>>subkeys[0];    _strupr(plain);    len = strlen(plain);    /**********Find the subkeys**************/    for(int i=1; i<len; i++) { if ((plain[i-1]>='A') && (plain[i-1]<='Z')) { subkeys[i] = plain[i-1]-65; } }    /****************ENCRYPTION***************/       for(int i=0; i<len; i++) { if ((plain[i]>='A') && (plain[i]<='Z')) {    cipher[i] = (((plain[i]-65)+subkeys[i])%26)+65; }...
#include <iostream> #include <string> #include <fstream> #include <vector> #include <sstream> using namespace std; int main() {...
#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; for(int i=0;i<population_directory.size();i++){ if(population_directory[i].second>=1000000000){ cout<<population_directory[i].first<<endl; } } cout<<"Names of countries with population<=1000,000"<<endl; for(int i=0;i<population_directory.size();i++){ if(population_directory[i].second<=1000000){ cout<<population_directory[i].first<<endl; } } } can u pls explain the logic behind this code up to 10 lines pls, many thanks
C++ Given Code: #include <iostream> #include <string> using namespace std; int main() { //declare variables to...
C++ Given Code: #include <iostream> #include <string> using namespace std; int main() { //declare variables to store user input bool cont = true; //implement a loop so that it will continue asking until the user provides a positive integer // the following provides ONLY part of the loop body, which you should complete { cout <<"How many words are in your message? \n"; cout <<"Enter value: "; // get user input integer here    cout <<"\nInvalid value. Please Re-enter a...
#include <iostream> #include <string> #include <iomanip> #include <cstdlib> #include "Contact.h" using namespace std; class Contact {...
#include <iostream> #include <string> #include <iomanip> #include <cstdlib> #include "Contact.h" using namespace std; class Contact { public: Contact(string init_name = "", string init_phone = "000-000-0000"); void setName(string name); void setPhone(string phone); string getName()const; string getPhone()const; friend ostream& operator << (ostream& os, const Contact& c); friend bool operator == (const Contact& c1, const Contact& c2); friend bool operator != (const Contact& c1, const Contact& c2); private: string name, phone; }; Contact::Contact(string init_name, string init_phone) { name = init_name; phone = init_phone;...
#include<iostream> #include<cmath> using namespace std; int p = 7; void main() { extern double var ;...
#include<iostream> #include<cmath> using namespace std; int p = 7; void main() { extern double var ; int p = abs(-90); cout << ::p + p - var << endl; system("pause"); } double var = 5.5;
#include <iostream> #include <string> #include <sstream> using namespace std; int main() { string userInput; getline(cin, userInput);...
#include <iostream> #include <string> #include <sstream> using namespace std; int main() { string userInput; getline(cin, userInput); // Declaring base int N = 30; if (userInput.length() > 10) { cout << 0 << endl; } else { int finalTotal = 0; //Iterates through userInput for(int i = 0; i < 10; i++){ char convertedInput = userInput[i]; // ASCII decimal value of each character int asciiDec = int(convertedInput); //Casts char value from input to int value stringstream chr; chr << convertedInput; int...
What is the flowchart for this code. Thank You! #include<iostream> #include<iomanip> #include<string> #include<cmath> using namespace std;...
What is the flowchart for this code. Thank You! #include<iostream> #include<iomanip> #include<string> #include<cmath> using namespace std; float series(float r[], int n) {    float sum = 0;    int i;    for (i = 0; i < n; i++)        sum += r[i];    return sum; } float parallel(float r[], int n) {    float sum = 0;    int i;    for (i = 0; i < n; i++)        sum = sum + (1 / r[i]);...
I want Algorithim of this c++ code #include<iostream> using namespace std; int main() { char repeat...
I want Algorithim of this c++ code #include<iostream> using namespace std; int main() { char repeat = 'y'; for (;repeat == 'y';){ char emplyeename[35]; float basic_Salary,EPF, Dearness_Allow, tax, Net_Salary , emplyee_id; cout << "Enter Basic Salary : "; cin >> basic_Salary; Dearness_Allow = 0.40 * basic_Salary; switch (01) {case 1: if (basic_Salary <= 2,20,00) EPF = 0; case 2: if (basic_Salary > 28000 && basic_Salary <= 60000) EPF = 0.08*basic_Salary; case 3: if (basic_Salary > 60000 && basic_Salary <= 200000)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT