PDA

View Full Version : y this while loop cannot run for the second time?


venice
06-26-04, 11:26 AM
//display the ascending and descending value

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define size 10
void asc(int*);
void dec(int*);
void swap(int*,int*);

void main(void)
{
int input[size],a;
int *ascend,*descend,x,y,m;
char dbuffer [9];
char tbuffer [9];

ascend=(int*)calloc(10,sizeof(int));
descend=(int*)calloc(10,sizeof(int));

_strdate( dbuffer );
printf( "The current date is %s \n", dbuffer );
_strtime( tbuffer );
printf( "The current time is %s \n", tbuffer );
printf("\n");


printf("Press 1 to enter or 0 to exit --->");
scanf("%d",&y);
do{
if(y==1)
{
printf("\t\t\tLoading");
for(x=0;x<6;x++)
{
_sleep(300);
printf(".");
}
system("cls");
}
else if(y==0){
system("cls");
printf("\n\t\t\tThanks for using this system!!\n\n\t\t\tHAVE A NICE DAY!!\n\n\n");
return;
}
else
{
system("cls");
printf("\n\t\t\tWrong entry!!\n\n\t\t\tPlease enter again!!\n\n\n");
return;
}

_strdate( dbuffer );
printf( "The current date is %s \n", dbuffer );
_strtime( tbuffer );
printf( "The current time is %s \n", tbuffer );
printf("\n");

printf("\n\t\t=====================================\n");
printf("\n\t\t\t WELCOME TO OUR\n\n\t\t\tCALCULATING SYSTEM!!\n");
printf("\n\t\t=====================================\n\n");

for(a=0;a<size;a++)
{
printf("\t\t Please key in value no.%d>",a+1);
scanf("%d",&input[a]);
*(ascend+a)=input[a];
*(descend+a)=input[a];

}
printf("\n\n");
printf("\t\tProcessing");
for(x=0;x<6;x++)
{
_sleep(200);
printf(".");
}
system("cls");


_strdate( dbuffer );
printf( "The current date is %s \n", dbuffer );
_strtime( tbuffer );
printf( "The current time is %s \n", tbuffer );

asc(ascend);
dec(descend);

printf("\n\n\t ================================================\n");
printf("\n\t\t\tRESULT AS SHOWN AS BELOW\n");
printf("\n\t ================================================\n");
printf("\t\tAscending\tOriginal\tDescending\n");

for(a=0;a<size;a++)
{
printf("\t\t%d\t\t%d\t\t%d\n",*(ascend+a),input[a],*(descend+((size-1)-a)));
}

printf("\n\n\n\t");

free(ascend);
free(descend);

printf("Press 2 to go to new calculation or any keys to exit>");
scanf("%d",&m);
}while(m==2);
system("cls");
printf("\n\t\t\tThanks for using this system!!\n\n\t\t\tHAVE A NICE DAY!!\n\n\n");

}
void asc(int *ascend)
{
int i,j,smallest;

for(i=0;i<size-1;i++)
{
smallest=i;

for(j=i+1;j<size;j++)
if(ascend[j]<ascend[smallest])
smallest=j;
swap(ascend+i,ascend+smallest);
}
}
void dec(int *descend)
{
int i,j,smallest;

for(i=0;i<size-1;i++)
{
smallest=i;

for(j=i+1;j<size;j++)
if(descend[j]<descend[smallest])
smallest=j;
swap(descend+i,descend+smallest);
}
}

void swap(int *x,int *y)
{
int temp;

temp=*x;
*x=*y;
*y=temp;
}

MrChimp
06-28-04, 10:58 AM
You appear to have a ';' in the wrong place :P

gopinath_alr
07-06-04, 08:55 AM
hi,
ur program is absolutely right.
u haven't misplaced any ';' but u misplaced the follwing code

ascend=(int*)calloc(10,sizeof(int));
descend=(int*)calloc(10,sizeof(int));

These statements should be inside while loop (at the beginning of while loop).

actually, u r deallocating memory for these variables 'ascend' and 'descend' at the end of while loop and u have to get back the memory at the beginning of the while loop. Or else u'l be getting some memory leak.