Click here to Skip to main content
15,887,936 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Your program should take as input a text file which contains numbers to be sorted and it 	should output a text file containing the sorted sequence and also output at each round of 	insertion sort. Both input and output files should be passed as command line arguments to your program as given in Sample Run.
	
	eg.
		Input file (input.txt)



		Output file (output.txt)
			
23 15 45 19 20 27 31 
23 15 45 19 20 27 31 
15 23 45 19 20 27 31
15 23 45 19 20 27 31
15 19 23 45 20 27 31
15 19 20 23 45 27 31
15 19 20 23 27 45 31
15 19 20 23 27 31 45 

----
ERROR IN MY CODE:
Status Messages:-

Program compiled successfully ...

Your program is not producing any output file, not allowed ...

This is mostly because your program is giving a "Segmentation Fault" for some input test case, test your program with multiple test cases, identify a test case when program gives segmentation fault, debug your program and try uploading again !


What I have tried:

#include<stdio.h>
#include<stdlib.h>
int main(int argc, char *argv[])
{
	int array[argc], c, d, t,i,j;
int n=argc-1;
for (i = 1; i < argc; i++)
	{
		array[i - 1] = atoi(argv[i]);
	}
   for (c = 0; c < n; c++) {
    printf("%d ", array[c]);
  }
 
  for (c = 1 ; c <= n - 1; c++) {
    d = c;
 printf("\n");
    while ( d > 0 && array[d] < array[d-1]) {
      t          = array[d];
      array[d]   = array[d-1];
      array[d-1] = t;
 
      d--;
    }
     for (j = 0; j < n; j++) {
    printf("%d ", array[j]);
  }
  }
 
  return 0;
}
Posted
Updated 3-Apr-17 8:09am
Comments
CHill60 3-Apr-17 11:05am    
Did you follow the instructions given in the status messages?
For what test case does your code cause a segmentation fault?
[no name] 3-Apr-17 11:05am    
debug your program and try uploading again

It looks like the above status message is generated by a test tool run by the site to which you are uploading it for verification.

The tool seems to check if your program generates an output file and assumes some kind of error if no output file is generated.

So have again a look at the assignment and what your code does:
Quote:
Your program should take as input a text file ... and it should output a text file ...
Your code is neither reading from an input file nor writing to an output file. It treats the command line arguments as numbers while it should treat them as file names.
 
Share this answer
 
A Segmentation fault occurs when you try to use an invalid pointer, or on some systems try to access an array element that doesn't exist - it depends on the compiler what errors it detects.

I would say that the first problem is that you are ignoring the instructions:
Quote:
Both input and output files should be passed as command line arguments to your program
This means that the values in argv are not numbers: they are the filenames for the input and output file. You should be reading the input file using the first argument to open the file, and writing the output to a different file using the second argument.

Once you have sorted that out, start learning to use the debugger to find problems like this: they make your life a whole lot easier, and if you don't get good with them on simple jobs like this one, you will have no idea what to do when you get to more complex projects. I can't tell you how to use it - I don't know what system you are using - but Google can: just search for the name of your IDE / compiler and the word "debugger" and you should get a lot of instructions to tell you want to do.
 
Share this answer
 
C++
int array[argc]

This not how you should allocate memory to dynamic variable.
Read about "dynamic memory allocation" in C.

If you can run the program o, your computer, use the debugger to see what happen.

Advice: learn to indent properly your code, it helps to read it.
C++
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char *argv[])
{
	int array[argc], c, d, t,i,j;
	int n=argc-1;
	for (i = 1; i < argc; i++)
	{
		array[i - 1] = atoi(argv[i]);
	}
	for (c = 0; c < n; c++) {
		printf("%d ", array[c]);
	}

	for (c = 1 ; c <= n - 1; c++) {
		d = c;
		printf("\n");
		while ( d > 0 && array[d] < array[d-1]) {
			t          = array[d];
			array[d]   = array[d-1];
			array[d-1] = t;

			d--;
		}
		for (j = 0; j < n; j++) {
			printf("%d ", array[j]);
		}
	}

	return 0;
}


When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900