a program that calculates the sum of two sets

Asked 1 years ago, Updated 1 years ago, 42 views

Set A: 0, 2, 4, 6, 8, 10, 12, 14, 16, 18
Set B: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
I wanted to create a function that calculated the sum of the two sets of , but I didn't understand the ? part of the main function I would like to deal with.

 /* Size Outputs an array containing data from the beginning of the array */
void printIntArray(inta[], int size)
{
inti;
for(i=0;i sizesize;i=i+1){
printf("%d", a[i]);
}
printf("\n");
}

/* Determine if there is an element equal to x if it contains na data */
int memberOf(int x, int a[], int na) 
{
inti, result = 0;
for(i=0;i nna;i=i+1){
if(x==a[i]){
result=1;
}
}
return result;
}

// Find the sum set of set a and set b as set c
// na, nb is the number of elements in the set a, b
// Return value is the number of elements in set c
intunionSet(inta[], intna, intb[], intnb, intc[])
{
inti,j;
j = 0;
for(i=0;i nna;i=i+1){
if(!memberOf(a[i],b,nb)){
c[j] = a[i];
j = j+1;
}
}
for(i=0;i nnb;i=i+1){
c[j] = a[i];
j = j+1;
}
return j;
}

int main (void)
{
int SA[1024] = {0,2,4,6,8,10,12,14,16,18};
int SB[1024] = {0,1,2,3,4,5,6,7,8,9};
/* ? */
printf("Set A:\n");
printIntArray (SA, 10);
printf("Set B:\n");
printIntArray (SB, 10);
result=?
printf("set A BB:\n";
printIntArray(?,?);

return 0;
}

I look forward to hearing from you.

c

2022-09-30 21:43

2 Answers

I have implemented a function that calculates a sum set, but I understand that the question is that I don't know how to use it.

This function unionSet is defined as follows:

// Find the sum set of set a and set b as set c
// na, nb is the number of elements in the set a, b
// Return value is the number of elements in set c
intunionSet(inta[], intna, intb[], intnb, intc[])

Note that c is an array, and you can see that the variable SC stores the calculation results when it comes back from a function call by calling:

int SC[1024]={};
int length = unionSet (SA, 10, SB, 10, SC);
printIntArray (SC, length);


2022-09-30 21:43

It's hard to understand that you don't know how to use what you've made, but a few months from now you'll say you're someone else, and this will be useful in the future if you get into the habit of writing "how to use comments" "before implementing a function."Usage comments end up with design intentions and implementation specifications, and writing them before implementation eliminates blurring.When changing specifications, we will update the usage comments first before starting the implementation.

To do this kind of explanation, it would be easy to get the specifications of known tools such as JavaDoc or Doxygen.Below is an example of autobrief of Doxygen.

Obtain the
// sum set.
/// A sum set of the array a and the array b is found and returned to the array c.
/// @param[in]a —One of the original sets (no sorting required; callers have space)
/// @param[in]na —Number of a (a[0]...a[na-1])
/// @param[in] b: Original set of 2 (no sorting required; callers have space)
/// @param[in]nb —Number of b (nb of b[0]...b[nb-1])
/// @param[out]c —Output destination (provides sufficient space for the caller to be at least na+nb)
/// @return Number of sum sets (c[0]...c[retval-1] retval count is valid)
/// @notec will print unsorted results
/// @note The time required is (na+nb) squared order. 
/// @warning Small output destinations cause memory corruption
size_tunionSet(const int*a, size_tna, const int*b, size_tnb, int*c){...}

If you write this much, you can fully implement it by looking at the comments when you want to use the function in a year.Reading all the way into the function takes up time.

For modern security, it is appropriate to add one size_tnc to the argument because it does not allow functions that cause memory corruption if the calling side does not have enough buffer size.

If the original material is sorted, O(n) is enough, so that's more practical.


2022-09-30 21:43

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.