게시판 즐겨찾기
편집
드래그 앤 드롭으로
즐겨찾기 아이콘 위치 수정이 가능합니다.
C언어 고수님들 이거 맞는지 확인좀..(스압주의)
게시물ID : computer_89012짧은주소 복사하기
작성자 : tutuh
추천 : 0
조회수 : 403회
댓글수 : 6개
등록시간 : 2013/05/21 18:52:29

미국 사는 유학생인대요, 프로그래밍 공부하다가 막혀서요, 이게 틀린지 맞는지 확인이 안되서 질문해요


Write the insertion using pointer arithmetic. The data to be sorted are to be read from a file. The array is to be dynamically allocated in the heap after reading the file to determine the number of elements While reading the data to determine the size of array you will require, print them 10 integers to a line. Use the test data shown below

838 758 113 515 51 627 10 419 212 86

749 767 84 60 225 543 89 183 137 566

966 978 495 311 367 54 31 145 882 736

524 505 394 102 851 67 754 653 561 96

628 188 85 143 967 406 165 403 562 834

353 920 444 803 962 318 422 327 457 945

479 983 751 894 670 259 248 757 629 306

606 990 738 516 414 262 116 825 181 134

343 22 233 536 760 979 71 201 336 61

The data will be sorted as they are read into the array. Do not fill the array and then sort the data. After the array has been sorted, print the data again using the same format you used for the unsorted data.

#include <stdio.h> 

#include <stdlib.h>


#define MEM_ERROR printf("Not enough memory!\n")

#define MAX_ARY_SIZE 300



FILE *getFile(void);

int integerCount( FILE * loadData );

void reset( FILE * fPointer);

int *readInteger ( FILE * fPointer, int cnt, int *pAry );

void insertionSort ( int *list , int size);

void printResult (int *pAry, int size );


int main(void)

{

//Local Declarations

  FILE * fp;

  int count;

  int Ary[MAX_ARY_SIZE];


//Statements

  fp = getFile();//File opened

  count = integerCount( fp );//number of integers counted

  reset( fp );//file pointer rewinded

  readInteger ( fp, count, Ary);//read Integers, populate array after sorted in the heap

  printResult (Ary, count);//Sorted data printed


  fclose(fp);// file closed

  return 0;

}//main


FILE *getFile(void)

{

//Local Declaration

FILE *filePointer;


// Statements

filePointer = fopen("input.txt","r");//file opened


if(filePointer == NULL)

printf("Cannot open the file!\n");


return filePointer;

}//getFile

int integerCount( FILE *loadData )

{

//Local Declartions

int cnt=0;

int fend;

int *list;


//Statements

list = (int *) malloc (MAX_ARY_SIZE * sizeof(int));

while(1)//infinite loop

{

fend=fscanf(loadData,"%d", list+cnt);

if(fend==EOF) break;//stops when it is end of file

cnt++;//counting integers

}

realloc ( list, cnt * sizeof(int));

printResult( list, cnt );//printing result


free(list);

return cnt;

}//integerCount


void reset( FILE *fPointer)

{

rewind(fPointer);//file pointer rewinded

}


int *readInteger( FILE *fPointer, int cnt, int *pAry)

{

//Local Declartions

int *list;


//Statements

list= (int *) malloc ( cnt * sizeof(int));

if(list==NULL)

{

MEM_ERROR;

exit(100);

}

for(int i=0; i<cnt; i++){

fscanf(fPointer,"%d", (list+i));

}

insertionSort( list, cnt);//Sorting data stored in heap



for(int i=0; i<cnt; i++){

*(pAry+i)= *(list+i);}//populating array with sorted data

free(list);

return pAry;

}


void insertionSort ( int *list , int size )

{

int temp;

int walk;

bool located;

for(int current = 1; current < size; current++)

{

located = false;

temp =* ( list + current );

for( walk = current - 1 ; walk >= 0 && !located;)

if(temp < *(list+walk))

{

*(list+ walk + 1) = *( list + walk );

walk--;

}

else

located = true;

*(list + walk + 1) = temp;

}

return ;

}


void printResult( int *pAry, int count)

{

for( int i=0; i< count; i++)

{

if( i%10 == 0)

printf("\n");

printf("%d\t", *( pAry + i ) );

}

}

전체 추천리스트 보기
새로운 댓글이 없습니다.
새로운 댓글 확인하기
글쓰기
◀뒤로가기
PC버전
맨위로▲
공지 운영 자료창고 청소년보호