C Programming project related help Hello, I\'ve got 2 methods to do as the follo
ID: 3562638 • Letter: C
Question
C Programming project related help
Hello, I've got 2 methods to do as the following
//
// It returns in *name and *value the name and value that correspond to
// the ith entry. It will return 1 if successful, or 0 otherwise.
//
int rtable_get_ith(RESIZABLE_TABLE * table, int ith, char ** name, void ** value) {
return 0;
}
//
// It removes the ith entry from the table. The entries after the entry removed are
// moved downwards to use the empty space. Also the name/value strings are freed.
//
int rtable_remove_ith(RESIZABLE_TABLE * table, int ith) {
return 0;
}
For example, if I have a table like -
1) Mat 298 Vine Street
2) Jeff 998 Vine Street
3) Sun 234 Vine Street
Using the methods should return the appropriate answer. (table has 2 properties, name which is the name of the person and value which is address.
(This is the way I am doing it currently) -
int rtable_get_ith(RESIZABLE_TABLE * table, int ith, char ** name, void ** value) {
if((strcmp(table->array[ith].name, &name) == 0) && (strcmp(table->array[ith].value, &value) ==
0)) {
return 1;
}
return 0;
}
Explanation / Answer
What you are doing for rtable_get_ith is right, the function is
int rtable_get_ith(RESIZABLE_TABLE * table, int ith, char ** name, void ** value)
{
if((strcmp(table->array[ith].name, &name) == 0) && (strcmp(table->array[ith].value, &value) ==
0))
{
return 1;
}
return 0;
}
int rtable_remove_ith(RESIZABLE_TABLE * table, int ith)
{
free(table->array[ith].name);
free(table->array[ith].value);
//All entries before i are moved downwards
int j;
for(j=0;j<i;j++)
{
//Shifting downwards
table->array[ith-j].name = table->arrya[ith-j-1].name;
table->array[ith-j].value = table->array[ith-j-1].value;
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.