Hi.. I am new to Dani web and would like to know the problem in the following code:

#include<iostream.h>
#include<conio.h>
void insertion_sort(int *p);
int main()
{
int a[20],j;
cout<<"\nEnter contents of array:";
for(int k=0;k<20;k++)
{
 cin>>a[k];
}
insertion_sort(a);
return 0;
}
void insertion_sort(int *p)
{
 int h,key;
 for(int i=2;i<20;i++)   //Insert into the sorted array p[1:i-1]
 {
  key= p[i];
  h=i-1;
  while(h>0&&p[h]>p[i])
  {
  p[h+1]=p[h];
  h--;
  p[h+1]=key;
  }
 }
 cout<<"\nSorted array:";
 for(int m=0;m<20;m++)
 {
  cout<<p[m];
  cout<<endl;
 }
}

This runs fine.. but produces no output..

Maybe it does, and maybe it doesn't. The first thing to do is to pause the program after the call to insertion sort and before the return line in main(). Calling cin.get()or cin >> x once or twice usually does the trick. If you can pause the program and there is still no output, then you need to debug the program by using your debugger or by throwing in a bunch of input statements to pause the program after each individual step and output the appropriate variable values to the screen to be sure you reach each step of the program and the output with each step of the program is appropriate.

N.B. These days most compilers prefer you to use iostream and namespace std instead of iostream.h.

Edited 6 Years Ago by Lerner: n/a

You should pause your program so you can see the output before the console window closes. You can do this quite a few ways, but the simplest way I can think of is using the system command. I am not sure if this works cross-platform but it works for Windows.

int main()
{
  int a[20],j;
  cout<<"\nEnter contents of array:";
  for(int k=0;k<20;k++)
  {
    cin>>a[k];
  }
  insertion_sort(a);
  system("pause");
  return 0;
}

Happy coding!

Edited 6 Years Ago by Vexx: n/a

I tried system("pause") as recommended but there was no change.. I have one more problem like this where I think coding is correct but there is no output.. Moreover I found from internet that system("pause") should be used for "C" rather than C++. So any other suggestion would be appreciated.. I am using Turbo C++ as compiler..

Hey Lerner, don't think that I ignored your answer but the problem is that I really don't know how to use cin.get() function.. So if you can tell me how to use it . that would be a great help..

Use cin >> then:

insertion_sort(a);
char ch;
cin >> ch;
return 0;

You can throw another cin >> ch; in there if the one doesn't do it. Otherwise you can replace the declaration of char ch and the call to cin >> with 1 or 2 calls to cin.get(); But you need to do something to something to tell the computer to not go back to the desktop as soon as the call to insertionsort() is completed so you can see the output on the screen, assuming there is something printed to the screen. If neither of those approaches work, then you need to pause during the program to evaluate where things go wrong. The debugger will do that or you can write your own debugging code by placing the same "pausing" code snippets after each action of the program and outputting the value of the variable that was changed by that action or output a statement that "I am now in function x" or whatever so you know the code gets that far before crapping out.

This article has been dead for over six months. Start a new discussion instead.