如何在C

时间:2017-04-23 02:53:37

标签: c arrays

这是我的问题:

我有一个程序,用户必须填写此内容:

  • 一个元素列表的二维数组,我最多可以从中获取一个元素(我们称之为exclusionArr)
  • 一个元素列表的二维数组,我必须至少从中获取一个元素(我们称之为inclusArr)
  • 包含我必须保留的元素的数组(我们称之为keepArr)
  • 包含我无法使用的元素的数组(我们称之为throwArr)

现在,在第一步,我需要检查exclusionArr的任何行是否包含keepArr的元素,如果确实如此,我需要添加该行的每个元素,但是在throwArr中添加该行。

一行不能包含keepArr的多个元素,i' ll创建一个函数来检查它并返回错误(如果是)。 (你最多可以在exclusionArr的行中保留一个项目,因此在同一行上有2个keepArr元素是个问题)

我需要帮助将元素添加到数组中,我似乎无法将这些值存储在我的数组中(更像是我不知道如何,仍然是C的新手)。

这是我迄今为止所做的功能:

void interdiction(int *throwArr[], int *throw_size, int keepArr[], int keep_size, int x, int y, int exceptionArr[x][y]) {
    int i, j, k, count=0;
    while(count<keep_size) {
        for(i=0;i<x;i++) {
            for(j=0;j<y;j++) {
                if (exceptionArr[i][j] == keepArr[count]) {
                    for (k=0;k<y;k++) {
                        if(k!=j) {
                            printf("\nElement %d of exclusion %d inserted in throwArr",exceptionArr[i][k], i);
                            *throw_size+=1;
                            throwArr[*throw_size]=exceptionArr[i][k];
                        }
                        else printf("\nelement to keep found in exclusion %d in position %d", i, j);
                    }
                }
            }
        }
        count++;
    }
}

我希望它能改变我放在函数中的throwArr,以便它在已存在的数组上添加当前行上除keepArr中的元素之外的每个元素。

我不知道它是否相关,但是对于throwArr在初始化时我分配了大量的额外内存,所以我可以在没有空间不足的情况下进行更改,所以我不知道如果我需要在函数中重新分配内存以进行更改。

非常感谢任何帮助!

编辑:这里是完整的代码

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>


void interdiction(int *throwArr, int throw_size, int *keepArr, int keep_size, int x, int y, int exclusionArr[x][y]);
static int compare (void const *a, void const *b);
void noDuplicate( int arr[], int *size );
void xclusion_alloc (int x, int y, int(**aptr)[x][y]);
void xclusion_print (int x, int y, int array[x][y]);
void xclusion_fill (int x, int y, int array[x][y]);

void main(){
    int throw_size, keep_size, rexc, lexc;
    int i, j;
    int  nbObjets=7, *throwArr, *keepArr, (*exclusionArr)[rexc][lexc];

    printf("\nHow many exclusions :");
    scanf("%d", &rexc);
    printf("\nHow many elements in each exclusion :");
    scanf("%d", &lexc);
    xclusion_alloc(rexc,lexc,&exclusionArr);
    xclusion_fill(rexc,lexc,*exclusionArr);
    xclusion_print(rexc,lexc,*exclusionArr);

    printf("\nHow many elements do we have to keep :");
    scanf("%d", &keep_size);
    keepArr=malloc(nbObjets*sizeof(int));
    printf("\nWhat are they :");
    for(i=0;i<keep_size;i++){
        scanf("%d",&keepArr[i]);
    }
    qsort(keepArr, keep_size, sizeof *keepArr, compare);
    noDuplicate(keepArr, &keep_size);

    printf("\nHow many elements we can't use :");
    scanf("%d", &throw_size);
    throwArr=malloc(nbObjets*sizeof(int));
    printf("\nWhat are they :");
    for(i=0;i<throw_size;i++){
        scanf("%d",&throwArr[i]);
    }
    qsort(throwArr, throw_size, sizeof *throwArr, compare);
    noDuplicate(throwArr, &throw_size);

    interdiction(throwArr, throw_size, keepArr, keep_size, rexc, lexc, *exclusionArr);
    printf("\nOur array of elements we can't use : ");
        for (i=0;i<throw_size;i++){
        printf("%d ", throwArr[i]);
        }

}

static int compare (void const *a, void const *b){
   int const *pa = a;
   int const *pb = b;
   return *pa - *pb;
}


void interdiction(int *throwArr, int throw_size, int *keepArr, int keep_size, int x, int y, int exclusionArr[x][y]){
    int i, j, k, count=0;

    while(count<keep_size){
        for(i=0;i<x;i++) {
            for(j=0;j<y;j++) {
                if (exclusionArr[i][j] == keepArr[count]) {     
                for (k=0;k<y;k++){
                    if(k!=j){
                    printf("\nElement %d of exclusion %d inserted in the array of elements we can't use",exclusionArr[i][k], i);
                    throw_size+=1;
                    throwArr[throw_size]=exclusionArr[i][k];            
                    }
                    else printf("\nelement to keep found in exclusion n°%d in position %d", i, j);
                }
                }
            }
        }
    count++;
    }
}

void noDuplicate( int arr[], int *size ) {
    int i=0, j=0;

    for (i = 1; i < *size; i++) {
        if (arr[i] != arr[j]) {
            j++;
            arr[j] = arr[i]; 
        }
   }


    *size = (j + 1);

}

void xclusion_alloc (int x, int y, int(**aptr)[x][y]) {

    *aptr = malloc( sizeof(int[x][y]) ); 
    assert(*aptr != NULL);
}

void xclusion_fill (int x, int y, int array[x][y]) {
    int i, j;

    for(i=0; i<x; i++) {
        for(j=0; j<y; j++) {
            scanf("%d", &array[i][j]);
            }
        }
}

void xclusion_print (int x, int y, int array[x][y]) {
    int i,j;

    for(i=0; i<x; i++) {
        printf("\nExclusion n°%d :", i);
        printf(" { ");
        for(j=0; j<y; j++) {
            printf("%d ", array[i][j]);
        }
        printf("}");
        printf("\n");
    }
}

可悲的是,我得到的输出是这样的:

How many exclusions :3

How many elements in each exclusion :2
5 3
2 7
4 1

Exclusion n░0 : { 5 3 }

Exclusion n░1 : { 2 7 }

Exclusion n░2 : { 4 1 }

How many elements do we have to keep :1

What are they :5

How many elements we can't use :1 2

What are they :
element to keep found in exclusion n░0 in position 0
Element 3 of exclusion 0 inserted in the array of elements we can't use
Our array of elements we can't use : 2

2 个答案:

答案 0 :(得分:0)

我设法通过对我的功能进行以下更改来使其工作:

void interdiction(int **throwArr, int *throw_size, int *keepArr, int keep_size, int x, int y, int exclusionArr[x][y]){
    int i, j, k, count=0;
    while(count<keep_size){
        for(i=0;i<x;i++) {
            for(j=0;j<y;j++) {
                if (exclusionArr[i][j] == keepArr[count]) {                 
                for (k=0;k<y;k++){
                    if(k!=j){
                    printf("\nElement %d of exclusion %d inserted in the array of elements we can't use",exclusionArr[i][k], i);

                    (*throwArr)[*throw_size]=exclusionArr[i][k];
                    *throw_size+=1; 
                    }
                    else printf("\nelement to keep found in exclusion n°%d in position %d", i, j);
                }
                }
            }
        }
    count++;
    }
}

答案 1 :(得分:0)

好的,我认为您需要更改这些行,如下所示。当你测试你的程序时,你也犯了一个错误。

void interdiction(int *throwArr, int throw_size, int *keepArr, int *keep_size, int x, int y, int exclusionArr[x][y]);

interdiction(throwArr, throw_size, keepArr, &keep_size, rexc, lexc, *exclusionArr);

void interdiction(int *throwArr, int throw_size, int *keepArr, int *keep_size, int x, int y, int exclusionArr[x][y]) {
    ...

        throwArr[*throw_size]=exclusionArr[i][k]; // these two lines switched order
        *throw_size+=1;  
    ...
}