[ create a new paste ] login | about

Link: http://codepad.org/fbIGEN8w    [ raw code | fork ]

C, pasted on Dec 7:
/*
	・正の値をキーボードから入力。-の値の場合停止。 
	・入力された値をヒープ配列(入力された値の並びをヒープに)に格納。配列の大きさはランダムにしておく。 
	そして降順に並びかえて画面に出力 
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
static void swap(int *a,int *b)
{
	int temp;
	temp = *a;
	*a = *b;
	*b = temp;
}
static void BubbleSort(int * temp,int size)
{
	int i,j;
	for(i = 0; i < size; i++)
		for(j = i; j<size;j++){
			if(temp[i] <= temp[j])
				swap(&temp[i],&temp[j]);
		}

}
static void show(int * temp, int size)
{
	int i;
	for(i = 0; i < size; i++)
		printf("%d \n",temp[i]);
}
int main()
{
	char buff[256] = {'\0'};
	int temp;
	int i = 0;
	int *r1 =malloc(sizeof(int)); 
	while(fgets(buff,256,stdin)){
		temp = atoi(buff);
		if(temp < 0)
			break;
		if(i == 0){
			r1[i] = temp;
			i++;
		}else if(i > 0){
			r1 = realloc(r1,(sizeof(int))*(i+1));
			r1[i] = temp;
			i++;
		}
	};

	BubbleSort(r1,i);
	show(r1, i);
	free(r1);
	return 0;
}


Output:
No errors or program output.


Create a new paste based on this one


Comments: