0
Follow
0
View

The main function needs the data from the subfunction. What should I write

gala42 注册会员
2023-01-25 23:06

The main code of the subject has been modified as follows. Please see the comments for your reference:

#include
#include
#define MaxSize 20
typedef int ElemType;
typedef struct
{
    int data[MaxSize];
    int length;
}SqList;
void InitList(SqList *&L)
{
    L=(SqList *)malloc(sizeof(SqList));
    L->length=0;
}
void CreatList(SqList *&L,int a[],int n)
{
    int i;
    //L=(SqList *)malloc(sizeof(SqList)); 修改
    for(i=0;idata[i]=a[i];
    }
    L->length=i;
}
void DispList(SqList *L)
{
    int i=0;
    for(i=0;ilength;i++)
    {
        printf("%d ",L->data[i]);
    }
}
int cz(SqList *L)
{
    int i=0;          // i=1 修改
    int max=0;        //,k=0;修改
    while(ilength)
    {
       if(L->data[i]>L->data[max])
       {
            max = i; //修改
            //k=i+1;
       }
       i++;    
    }
    return  max; //return(max,k); 修改
}
int main()
{
    int a[5],max; //修改
    SqList *L;
    InitList(L);
    printf("请输入五个元素");
    for(int i=0;i<5;i++)
    {
        scanf("%d",&a[i]);
    }
    CreatList(L,a,5);
    printf("请输出:");
    DispList(L);

    max = cz(L); //修改
    printf("\nMax:L->data[%d] = %d", max, L->data[max]);
    //printf("\n%d,%d",cz(L)); 修改

    return 0;
}

deny0508 注册会员
2023-01-25 23:06

Add a struct and modify the subfunction


typedef struct
{
    int maxvalue;
    int k;
}CZreturn;

CZreturn cz(SqList *L)
{
    int i=1;
    int max=L->data[0],k=0;
    while(ilength)
    {
       if(L->data[i]>max)
       {
            max=L->data[i];
            k=i+1;
       }
       i++;    
    }
    CZreturn tmp;
    tmp.maxvalue=max;
    tmp.k=k;
    return tmp;
}

Then modify the main function as follows:
CZreturn tmpvalue=cz(L);
printf("\n%d,%d",tmpvalue.maxvalue,tmpvalue.k);

sharonrain 注册会员
2023-01-25 23:06

Suggestion: In the last line of printf of the main function, change the statement that calls the child function cz to :

printf("\n%d,%d", max, k);

where max and k are the maximum value and subscript returned in the subfunction cz, respectively.

But you need to change the cz function to return a single structure so that you can use a structure to return multiple values.
Modified code:

#include
#include
#define MaxSize 20
typedef int ElemType;
typedef struct
{
    int data[MaxSize];
    int length;
}SqList;

struct Results {
    int max;
    int k;
};

void InitList(SqList *&L)
{
    L=(SqList *)malloc(sizeof(SqList));
    L->length=0;
}
void CreatList(SqList *&L,int a[],int n)
{
    int i;
    L=(SqList *)malloc(sizeof(SqList));
    for(i=0;idata[i]=a[i];
    }
    L->length=i;
}
void DispList(SqList *L)
{
    int i=0;
    for(i=0;ilength;i++)
    {
        printf("%d ",L->data[i]);
    }
}

Results cz(SqList *L)
{
    Results results = {0, 0};
    for (int i = 1; i < L->length; i++) {
        if (L->data[i] > results.max) {
            results.max = L->data[i];
            results.k = i + 1;
        }
    }
    return results;
}

int main()
{
    int a[5];
    SqList *L;
    InitList(L);
    printf("请输入五个元素");
    for(int i=0;i<5;i++)
    {
        scanf("%d",&a[i]);
    }
    CreatList(L,a,5);
    printf("请输出:");
    DispList(L);
    Results res = cz(L);
    printf("\n%d,%d", res.max, res.k);
}


Useful please adopt!

About the Author

Question Info

Publish Time
2023-01-25 23:06
Update Time
2023-01-25 23:06