Ask Question

Write a program that gets a list of integers from input, and outputs non-negative integers in ascending order (lowest to highest). Ex: If the input is: 10 - 7 4 39 - 6 12 2 the output is: 2 4 10 12 39

+4
Answers (1)
  1. 15 August, 01:54
    0
    Following are the code to this question:

    #include / /defining header file

    using namespace std;

    int main () / /defining main method

    {

    int a[]={10,-7,4,39,-6,12,2}; //defining single deminition array and assign value

    int i, x, j, t; / /defining integer variable

    cout<<"Before sorting value: ";

    for (i=0; i<7; i++) / /using loop to print value

    {

    cout<
    }

    cout<
    for (i=0; i<7; i++) / /defining loop to sort value

    {

    for (j=i+1; j<7; j++) / /count array value

    {

    if (a[i]>a[j]) / /defining condition to inter change value

    {

    //performing swapping

    t=a[i]; / /integer variable t assign array value

    a[i]=a[j]; //swapp value

    a[j]=t; //assign value in array

    }

    }

    }

    for (i=0; i<7; i++) / /defining loop to print value

    {

    if (a[i]>=0) / /defining condition to check positive value

    {

    cout<
    }

    }

    return 0;

    }

    Output:

    Before sorting value: 10 - 7 4 39 - 6 12 2

    After sorting value: 2 4 10 12 39

    Explanation:

    Following are the description to the above code:

    In the above program code, Inside the main method, an array a[] is declared that assign some value, and another integer variable "i, j, x, and t" is declared, in which variable "i and j" are used in the loop, and "x, t" is used to sort value. In the next step, three main for loop is declared, in which the first loop is used to print array value. In the second loop, inside another loop is used that sorts array values. In the last loop, a condition is defined, that check the positive value in the array and print its values.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Write a program that gets a list of integers from input, and outputs non-negative integers in ascending order (lowest to highest). Ex: If ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers