gerrymcd
asked on
end folding algoritmn in c
im try implement end folding with a 10 digit number. this following code is giving very strange results. i need to read the number in as a string in order to find the the first 2 digits and the last 3. i though i could use a for loop and an array but its not working right please help.
#include <stdio.h>
int main()
{
char record_key[10];
char start[2];
char middle[5];
char end[4];
int i,c=0;
printf("\n\nEnter record key ");
gets(record_key);
//get the first 2 digits
for(i=0;i<2;i++)
{
start[i]=record_key[i];
}
start[2]='\0';
//get the middle 5
for(i=3;i<7;i++,c++)
{
middle[c]=record_key[i];
}
middle[5]='\0';
//last 3 digits
for(i=7;i<10;i++)
{
end[i]=record_key[i];
}
printf("start is %s\n",start);
printf("middle is %s\n",middle);
printf("end is %s\n",end);
return 0;
}
#include <stdio.h>
int main()
{
char record_key[10];
char start[2];
char middle[5];
char end[4];
int i,c=0;
printf("\n\nEnter record key ");
gets(record_key);
//get the first 2 digits
for(i=0;i<2;i++)
{
start[i]=record_key[i];
}
start[2]='\0';
//get the middle 5
for(i=3;i<7;i++,c++)
{
middle[c]=record_key[i];
}
middle[5]='\0';
//last 3 digits
for(i=7;i<10;i++)
{
end[i]=record_key[i];
}
printf("start is %s\n",start);
printf("middle is %s\n",middle);
printf("end is %s\n",end);
return 0;
}
middle[4] is also not set, and middle[5] is invalid since you only delclare middle[5], which allocates the 5 elements middle[0],middle[1],middle [2],middle [3],middle [4]
likewise start[2] is invalid
likewise start[2] is invalid
ASKER
ok ill look into it and come back
include <stdio.h>
int main()
{
char record_key[10];
char start[3];
char middle[6];
char end[4];
int i,c=0;
printf("\n\nEnter record key ");
gets(record_key);
//get the first 2 digits
for(i=0;i<2;i++)
{
start[i]=record_key[i];
}
start[2]='\0';
//get the middle 5
for(i=3;i<7;i++,c++)
{
middle[c]=record_key[i];
}
middle[5]='\0';
//last 3 digits
c = 0;
for(i=7;i<10;i++,c++)
{
end[c]=record_key[i];
}
end[4] = '\0';
printf("start is %s\n",start);
printf("middle is %s\n",middle);
printf("end is %s\n",end);
return 0;
}
int main()
{
char record_key[10];
char start[3];
char middle[6];
char end[4];
int i,c=0;
printf("\n\nEnter record key ");
gets(record_key);
//get the first 2 digits
for(i=0;i<2;i++)
{
start[i]=record_key[i];
}
start[2]='\0';
//get the middle 5
for(i=3;i<7;i++,c++)
{
middle[c]=record_key[i];
}
middle[5]='\0';
//last 3 digits
c = 0;
for(i=7;i<10;i++,c++)
{
end[c]=record_key[i];
}
end[4] = '\0';
printf("start is %s\n",start);
printf("middle is %s\n",middle);
printf("end is %s\n",end);
return 0;
}
ASKER
i have made the program implement the end folding algoritmn but even though it gives annser the correct anser it crashes in Turbo c 3.0 i hvae trried it in v c 6.0 and it works no prob can some help me out i keeps crashing after it prints out the anser. its a dos crash "sayting that this program has called an illegal instruction to be carried out"
/*
code to implement the end folding algorithm
*/
#include <stdio.h>
#include<string.h>
#include<stdlib.h>
long int End_Folding(const char record_key[]);
int main()
{
char record_key[10];
printf("\n\nEnter record key ");
gets(record_key);
End_Folding(record_key);
return 0;
}
long int End_Folding(const char record_key[])
{
char start[2];
char middle[5];
char end[4];
char start_end[10];
int i,c=0;
long int first_loc_avail=16541;
long int keys=30000;
long int middle_int,startend_int;
long int startend_mid=0;
long remainder,address_location ;
//get the first 2 digits
for(i=0;i<2;i++)
{
start[i]=record_key[i];
}
start[2]='\0';
//get the middle 5
c=0;
for(i=2;i<7;i++,c++)
{
middle[c]=record_key[i];
}
middle[5]='\0';
c=0;
//last 3 digits
for(i=7;i<10;i++,c++)
{
end[c]=record_key[i];
}
start_end[0]=start[0];
start_end[1]=start[1];
start_end[2]=end[0];
start_end[3]=end[1];
start_end[4]=end[2];
start_end[5]='\0';
printf("start is %s\n",start);
printf("middle is %s\n",middle);
printf("end is %s\n",end);
printf("start end is %s\n",start_end);
middle_int=atol(middle);
startend_int=atol(start_en d);
//printf("middle int %ld\n",middle_int);
startend_mid=middle_int+st artend_int ;
//printf("start end mid is %ld\n",startend_mid);
remainder=startend_mid%key s;
//printf("remainder is %d\n",remainder);
address_location=first_loc _avail+rem ainder;
printf("Adress location is %ld\n",address_location);
return 0;
}
/*
code to implement the end folding algorithm
*/
#include <stdio.h>
#include<string.h>
#include<stdlib.h>
long int End_Folding(const char record_key[]);
int main()
{
char record_key[10];
printf("\n\nEnter record key ");
gets(record_key);
End_Folding(record_key);
return 0;
}
long int End_Folding(const char record_key[])
{
char start[2];
char middle[5];
char end[4];
char start_end[10];
int i,c=0;
long int first_loc_avail=16541;
long int keys=30000;
long int middle_int,startend_int;
long int startend_mid=0;
long remainder,address_location
//get the first 2 digits
for(i=0;i<2;i++)
{
start[i]=record_key[i];
}
start[2]='\0';
//get the middle 5
c=0;
for(i=2;i<7;i++,c++)
{
middle[c]=record_key[i];
}
middle[5]='\0';
c=0;
//last 3 digits
for(i=7;i<10;i++,c++)
{
end[c]=record_key[i];
}
start_end[0]=start[0];
start_end[1]=start[1];
start_end[2]=end[0];
start_end[3]=end[1];
start_end[4]=end[2];
start_end[5]='\0';
printf("start is %s\n",start);
printf("middle is %s\n",middle);
printf("end is %s\n",end);
printf("start end is %s\n",start_end);
middle_int=atol(middle);
startend_int=atol(start_en
//printf("middle int %ld\n",middle_int);
startend_mid=middle_int+st
//printf("start end mid is %ld\n",startend_mid);
remainder=startend_mid%key
//printf("remainder is %d\n",remainder);
address_location=first_loc
printf("Adress location is %ld\n",address_location);
return 0;
}
ASKER
LDC
sorry ur answer is spot on but i needmore help. ill give u the points if can sort the next prob i got see above commnet by me.
sorry ur answer is spot on but i needmore help. ill give u the points if can sort the next prob i got see above commnet by me.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks man thats great.
also, end[0] is never set