用于实现插入排序的程序。 [英] Program to implement insertion sort.

查看:78
本文介绍了用于实现插入排序的程序。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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;
}

推荐答案

看起来上述状态消息是由您所在的站点运行的测试工具生成的上传它进行验证。



该工具似乎检查您的程序是否生成输出文件,如果没有生成输出文件则会出现某种错误。



所以再看看作业和你的代码的作用:

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:
引用:

你的程序应该把文本文件作为输入...它应该输出一个文本文件...

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.


当您尝试使用无效指针时,或者在某些系统上尝试访问数组时会发生分段错误不存在的元素 - 它取决于编译器检测到的错误。



我会说第一个问题是你忽略了指令:

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

这意味着argv中的值不是数字:它们是输入和输出文件的文件名。您应该使用第一个参数读取输入文件以打开文件,并使用第二个参数将输出写入另一个文件。



一旦您对其进行了排序out,开始学习使用调试器来发现这样的问题:它们让你的生活变得更加轻松,如果你在这样的简单工作中没有得到好处,你就不知道当你做什么时进入更复杂的项目。我无法告诉你如何使用它 - 我不知道你使用的是什么系统 - 但谷歌可以:只搜索IDE /编译器的名称和调试器这个词,你应该得到很多指令告诉你要做的事。

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.


int array[argc]



这不是你应该如何为动态变量分配内存。

阅读动态内存分配在C.



如果您可以运行程序o,您的计算机,请使用调试器查看发生的情况。



建议:学会正确缩进你的代码,这有助于阅读它。


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.

#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;
}





当你不明白你的代码在做什么或为什么它做它做的时候,答案是调试器

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。



调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]



调试器在这里向您展示您的代码在做什么你的任务是与它应该做的事情进行比较。

调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。



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.


这篇关于用于实现插入排序的程序。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆