Ask Question
30 August, 15:48

Write a complete program that reads 6 numbers and assigns True to variable isAscending if the numbers are in ascending order. Otherwise assign False to it. Display the value of isAscending. Here are three sample runs: Sample Run 1 Enter six numbers: 4 6 8 10 11 12 True Sample Run 2 Enter six numbers: 4 6 3 10 11 12 False Sample Run 3 Enter six numbers: 41 6 3 10 11 12 False Note: Your program must match the sample run exactly.

+4
Answers (1)
  1. 30 August, 16:41
    0
    The programming language is not stated;

    For this, I'll answer your question using c+ + programming language

    The program does not make use of comments (See explanation for further details)

    From the sample run, the input numbers are integer; So, this program assumes that all input will be integer ...

    Program starts here

    #include

    using namespace std;

    int main ()

    {

    cout<<"Enter six numbers: ";

    int Arr[6];

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

    {

    cin>>Arr[i];

    }

    bool flag = true;

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

    {

    for (int j = i; j<6; j++)

    {

    if (Arr[i]>Arr[j])

    {

    flag = false;

    }

    }

    }

    string isAscending;

    if (flag)

    {

    isAscending = "True";

    }

    else

    {

    isAscending = "False";

    }

    cout<
    return 0;

    }

    Explanation:

    cout<<"Enter six numbers: "; - > This line prompts the user for input

    int Arr[6]; - > The input is stored in an array; This line declares the array

    The following for loop iteration is used to iterate through the array

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

    {

    cin>>Arr[i]; - > This line accepts user input

    }

    bool flag = true; - > A boolean variable is initialized to true

    The following nested loop checks if numbers are in ascending order

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

    {

    for (int j = i; j<6; j++)

    {

    if (Arr[i]>Arr[j]) - > This particular if statement checks if a number is greater than the next number

    {

    flag = false; - > If yes, the boolean variable is changed to false

    }

    }

    } - > The nested loop ends here

    string isAscending = "True"; - > Here, isAscending is declared as string

    if (flag) If boolean variable flag is true; i

    {

    isAscending = "True"; "True" is assigned to isAscending

    }

    else - > Otherwise; if boolean variable flag is no longer true; i. e if it's false

    {

    isAscending = "False"; - > "False" is assigned to isAscending

    }

    cout< The corresponding value of isAscending is printed
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “Write a complete program that reads 6 numbers and assigns True to variable isAscending if the numbers are in ascending order. Otherwise ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers