다음 코딩을 배열로 표현하기..
달달항사탕
질문 제목 : 배열함수로 바꿔서 표현하기질문 요약 :아래 코딩을 배열을 사용하여 코딩해주셨으면 합니다질문 내용 :
#include stdio.h
typedef struct
{
int useagespace; // 사용 공간
int remainspace; // 남은 공간
}memory;
void firstfit(memory m[], int n, int requestspace)
{
int i;
for(i = 0; i n; i++)
{
if(m[i].remainspace = requestspace)
{
m[i].useagespace += requestspace;
m[i].remainspace -= requestspace;
printf(%d번째 공간에 배치하였습니다.\n, i+1);
return;
}
}
printf(요청작업을 공간에 배치할수 없습니다.\n);
}
void bestfit(memory m[], int n, int requestspace)
{
int i;
int bestindex = -1;
for(i = 0; i n; i++)
{
if(bestindex == -1)
{
if(m[i].remainspace = requestspace)
bestindex = i;
}
else
{
if(m[i].remainspace = requestspace)
{
if(m[i].remainspace m[bestindex].remainspace)
bestindex = i;
}
}
}
if(bestindex == -1)
printf(요청작업을 공간에 배치할수 없습니다.\n);
else
{
m[bestindex].useagespace += requestspace;
m[bestindex].remainspace -= requestspace;
printf(%d번째 공간에 배치하였습니다.\n, bestindex+1);
}
}
void worstfit(memory m[], int n, int requestspace)
{
int i;
int worstindex = 0;
for(i = 1; i n; i++)
{
if(m[i].remainspace m[worstindex].remainspace)
worstindex = i;
}
if(m[worstindex].remainspace = requestspace)
{
m[worstindex].useagespace += requestspace;
m[worstindex].remainspace -= requestspace;
printf(%d번째 공간에 배치하였습니다.\n, worstindex+1);
}
else
printf(요청작업을 공간에 배치할수 없습니다.\n);
}
void print(memory m[], int n)
{
int i ;
for(i = 0; i n; i++)
{
printf(%d번째 : %2dk 공백 %2dk 사용중\n,i+1, m[i].remainspace, m[i].useagespace);
}
}
void main()
{
int requestspace;
memory m[7] =
{
{ 0, 17 },
{ 10, 0 },
{ 0, 7 },
{ 4, 0 },
{ 0, 15 },
{ 20, 0 },
{ 0, 50 },
}; // 7개의 구역으로 나누었습니다.
print(m, 7);
printf(기억장치 공간 입력 : );
scanf(%d, &requestspace);
firstfit(m, 7, requestspace);
//worstfit(m, 7, requestspace);
//bestfit(m, 7, requestspace);
print(m, 7);
}
위 코딩을 배열을 사용하여 코딩은 안되는건가요?
예를들어 배열에 있는 수(17, 7, 15, 50)중에서 어떤 정수12를 입력하면
50을 출력하여 이것은worstfit 이라는 출력이 나오게하고
또 12를 입력하면 15가 출력되어 bestfit 이라는 출력이 나오게하고,
12를 입력하면 배열의 맨 처음 값인 17이 출력되어 firstfit 이 출력되게 하려고합니다
코딩좀 부탁드릴게요
memory 함수는 포함하지 않으셔도 됩니다