十万火急!请大家帮帮忙!
Q1. Write a function with the header
int removeDuplicates(char names[ ][31], int age[ ], int *n);
that removes all duplicate records from a table
(the table being the array of strings called: "names").
The table consists of *n records and each record consists
of a null-terminated char array (a string) containing a
persons first name and an int containing their age.
*n holds the number of records in the table when
removeDuplicates is called and the number of records
in the updated table upon return from removeDuplicates.
A duplicate record is a record that has the same
name as another record, but not necessarily the
same age. When removing a duplicate record,
your function keeps the record with the lower
index and discards the record with the higher index.
Removing a record will involve shifting all of the array names
that appear after a duplicate one position "up" the array
(this includes the same for the "age" array).
For example:
The "names" array will initially look like:
(before the duplicate "Homer" record is found:
"Homer", "Marge", "Homer", "Bart", "Marge", "Lisa"
And like this:
"Homer", "Marge", "Bart", "Marge", "Lisa"
after the array names have been shifted "up" 1 position.
Your function returns the total number of records removed.
NOTE: temporary array declarations like char temp[6][31] are not permitted!
Each name in the array of names is unique upon
return from removeDuplicates.
Example:
Before call to removeDuplicates
name[ ][] age[ ] *n
-------------------------------
Homer 45 6
Marge 22
Homer 42
Bart 14
Marge 21
Lisa 8
After call to removeDuplicates
name[ ][] age[ ] *n
----------------------
Homer 45 4
Marge 22
Bart 14
Lisa 8
*/
int removeDuplicates(char [ ][31], int [ ], int *);
int main( ) {
int n = 6, rv;
char names[6][31] = { "Homer", "Marge", "Homer", "Bart", "Marge", "Lisa" };
int ages[6] = { 45, 22, 42, 14, 21, 8 };
rv = removeDuplicates(names, ages, &n);
for(i=0; i<n; i++) {
printf("%s %d\n", names[i], ages[i]);
}
printf("total records removed: %d\n", rv);
return 0;
}
#include <stdio.h>
#include <string.h>
int removeDuplicates(char names[ ][31], int age[ ], int *n) {
int i, j, rv;
char temp[31];
for(i=0; i<*n; i++) {
strcpy(temp, names[i]);
for(j=i+1; j<*n; j++) {
rv = strcmp(temp, names[j]);
if(rv == 0) {
//接下去怎么写,我就不会了... 大家帮我想想吧, 谢谢了,这个作业明天就交了...
}
}
}
return ?
}
[解决办法]
jf
[解决办法]
跟一帖,也来接分
[解决办法]
用链表删除会简单点