1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Why am I not getting an output for my problem?

Discussion in 'C++' started by greenhorn_newbie, May 5, 2012.

  1. greenhorn_newbie

    greenhorn_newbie New Member

    Joined:
    May 5, 2012
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    //Hello I'm a newbie in programming I need some help in figuring out what I'm //doing wrong. I am trying to figure out how to output the number of key //comparisons to sort the list and nothing I do is working. The code compiles //fine I'm just having issues with getting the correct output for the Iterations. //I'm using Dev C++. Here is the code:
    Code:
    #include <iostream>
    
    using namespace std;
    
    void insertionSort(int list[ ], int listLength);
    
    int main()
    {
    int list[ ] = {18, 8, 11, 9, 15, 20, 32, 61, 22, 48, 75, 83, 35, 3};
    int i;
    char ans;
    int numIters;
    
    insertionSort(list, 14);
    
    cout <<"After sorting, the list elements are:"
    << endl;
    for (i = 0; i < 14; i++)
    cout << list [i] << " ";
    
    cout << endl;
    
    while (true)                   //:confused:This is where I'm having problems with the output. 
    {
    cout << "Number of Iterations: ";
    cin >> numIters;
    cin.ignore();
    for (int i=0;i<numIters;i++);
    }
    
    cout << "Continue y/n: " << endl;
    cin >> ans;
    if (ans != 'y');
    cin.ignore();
    
    system("PAUSE");
    
    return 0;
    }
    
    void insertionSort(int list[ ], int listLength)
    
    {
    int firstOutOfOrder, location;
    int temp;
    
    for (firstOutOfOrder = 1; firstOutOfOrder < listLength;
    firstOutOfOrder++)
    
    if (list[firstOutOfOrder] < list[firstOutOfOrder - 1])
    {
    temp = list[firstOutOfOrder];
    location = firstOutOfOrder;
    
    do 
    {
    list[location] = list[location - 1];
    location--;
    }
    while (location > 0 && list[location - 1] > temp);
    
    list[location] = temp;
    
    }
    
    }
    
    
    The output that I keep getting is:
    After sorting, the list elements are:
    3 8 9 11 15 18 20 22 32 35 48 61 75 83
    Number of Iterations:
     

Share This Page