How to get the row size of an 2 dim array in C

rem

Hi there I was trying to make a varation of bucket sort. My Programm is easy because I'm a beginner in C but i don't know how print the 2 dim array when one row was realloc after putting in the numbers. I have tried int lenght= sizeof(buckets[0])/sizeof(buckets[0][0]) but then lenght=1. My alternativ solution in the main and it is not very dynamic therefore can anybody tell me how to get the size of the row in a 2 dim array?? If I would add a number in the first bucket the bucket will be too small so i realloc the bucket but how can i print it now? Sorry for the bad english :)

#include <stdlib.h>
#include <stdio.h>
#define MAXZAHL 50

int compare(const void * a, const void * b){// compare for the qsort
   return(*(int*)a - *(int*)b);
}

int** init_bucket(int l,int teile,int teilgr){// to init the buckets
    int **bucket;
    bucket=(int**)malloc(teile*sizeof(int));
    for (int i=0;i<teile;i++){
        bucket[i]=(int *)malloc(teilgr*sizeof(int));
    }
    for (int i=0;i<teile;i++){
        for (int j=0;j<teilgr;j++){
            bucket[i][j]=0;
            printf("%3i",bucket[i][j]);
        }
        printf("\n");
    }
    return bucket;
}
void bucket_int(int**bucket,int bucketsize,int nextVal,int n){// to write the   array in the buckets
     int start=bucketsize;
     for (int i=0;i<n;i++){
         if(nextVal< start && bucket[0][i]==0){
             bucket[0][i]=nextVal;
             break;
         }
         if(nextVal< start && bucket[0][i]!=0&& i==n-1){
             bucket[0][n]=(int)realloc(bucket,sizeof(int));
             ++i;
             bucket[0][i]=nextVal;
             break;
         }
         if((nextVal<(start*2)&& nextVal>=(start))&&(bucket[1][i]==0)){
              bucket[1][i]=nextVal;
              break;
         }
         if((nextVal<(start*3)&&nextVal>=(start*2))&&(bucket[2][i]==0)){
              bucket[2][i]=nextVal;
              break;
         }
         if((nextVal<(start*4)&&nextVal>=(start*3))&&(bucket[3][i]==0)){
              bucket[3][i]=nextVal;
              break;
         }
         if((nextVal<(start*5)&&nextVal>=(start*4))&&(bucket[4][i]==0)){
              bucket[4][i]=nextVal;
              break;
         }
    }
 }

int main(){
    int arr[]={3,26,2,10,33,45,20,15,11,9,34,40,19,16,4,5,26,49,1,0,6,8,7,3};
    int len=sizeof(arr)/sizeof(int);//
    int teile=5;
    int teilgr=MAXZAHL/5;
    int **buckets;
    buckets=init_bucket(len,teile,teilgr);
    for (int i=0;i<len;i++){//write the numbers in the buckets
        bucket_int(buckets,teilgr,arr[i],len);
    }
    printf("\n");
    //print Buckets
    for (int i=0;i<teile;i++){
        for (int j=0;j<teilgr;j++){// is there a way to say j<sizeof(buckets)??
            printf("%3i",buckets[i][j]);
        }
        printf("\n");
    }
    //sorting the buckets with qsort
    for (int i=0;i<teile;i++){
        for (int j=0;j<teilgr;j++){
            qsort(buckets[i],teilgr,sizeof(int**),compare);
        }
    }
    printf("\n");
    //print Buckets
    for (int i=0;i<teile;i++){
        for (int j=0;j<teilgr;j++){
            printf("%3i",buckets[i][j]);
        }
        printf("\n");
    }
    printf("\n");
    int f=0;
    //Putting the buckets back to the array
    for (int i=0;i<teile;i++){
        for (int j=0;j<teilgr;j++){
            if (buckets[i][j]!=0){
                arr[f]=buckets[i][j];
                f++;
            }
        }
    }
    printf("The sorted Array:\n");
    for (int i=0;i<len-1;i++){
        printf("%i ",arr[i]);
    }
    printf("\n");
    return EXIT_SUCCESS;
}
alk

how to get the size of the row

Read it from where you stored it, when you still knew it.

In C you cannot derive from a pointer to how much memory it points.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How to get the content of a row of a Numpy array?

분류에서Dev

C : How to make the size of an array dynamic?

분류에서Dev

How to increment the size of Dynamic Array (C++) without knowing the original array size?

분류에서Dev

How to create a two dimensional array of given size in C++

분류에서Dev

길이가 1 인 차원 중 하나가있는 Numpy 1-dim array vs 2-dim array

분류에서Dev

recurrent neural network ValueError : Found array with dim 3. 예상치 <= 2

분류에서Dev

C++ creating a 2D array using the size of a given vector, in a memory-safe manner

분류에서Dev

how to get the difference of two points in a 2D array

분류에서Dev

C - print in Stream array byte with variable size

분류에서Dev

Why the size of pointer to array is always 8 in C?

분류에서Dev

printing row from 2D array

분류에서Dev

How to implement an array in vbscript with a variable size

분류에서Dev

How to call a function with a reference to an unknown size array?

분류에서Dev

C++ dynamically increase array size for object-array?

분류에서Dev

How to get image in specified size given in parameter?

분류에서Dev

How to get file size in hex format?

분류에서Dev

2 Dim 배열 할당

분류에서Dev

Methods to sample a 2-dim curve efficiently

분류에서Dev

How to find contigious elements in a 2D array using C

분류에서Dev

C # 숫자의 오른쪽에 2dim 배열을 표시하는 방법

분류에서Dev

MongoDB and Spring Data. Get size of array in entity

분류에서Dev

numpy 배열을 (# dim1, # dim2, # channel)에서 (#channel, # dim1, # dim2)로 변경하는 방법

분류에서Dev

How to get excel row numbers to skip the header

분류에서Dev

How to get an alert when there are three images in a row

분류에서Dev

How to get next row column by javascript or jquery

분류에서Dev

How can find size of each Row in Apache spark sql dataframe and discrad the rows having size more than a threshold size in Kilobyte

분류에서Dev

scala Spark get the top words in each row of Array

분류에서Dev

How to get array of array value count?

분류에서Dev

3 Dim을 2 Dim으로 변환하고 Ajust Class?

Related 관련 기사

  1. 1

    How to get the content of a row of a Numpy array?

  2. 2

    C : How to make the size of an array dynamic?

  3. 3

    How to increment the size of Dynamic Array (C++) without knowing the original array size?

  4. 4

    How to create a two dimensional array of given size in C++

  5. 5

    길이가 1 인 차원 중 하나가있는 Numpy 1-dim array vs 2-dim array

  6. 6

    recurrent neural network ValueError : Found array with dim 3. 예상치 <= 2

  7. 7

    C++ creating a 2D array using the size of a given vector, in a memory-safe manner

  8. 8

    how to get the difference of two points in a 2D array

  9. 9

    C - print in Stream array byte with variable size

  10. 10

    Why the size of pointer to array is always 8 in C?

  11. 11

    printing row from 2D array

  12. 12

    How to implement an array in vbscript with a variable size

  13. 13

    How to call a function with a reference to an unknown size array?

  14. 14

    C++ dynamically increase array size for object-array?

  15. 15

    How to get image in specified size given in parameter?

  16. 16

    How to get file size in hex format?

  17. 17

    2 Dim 배열 할당

  18. 18

    Methods to sample a 2-dim curve efficiently

  19. 19

    How to find contigious elements in a 2D array using C

  20. 20

    C # 숫자의 오른쪽에 2dim 배열을 표시하는 방법

  21. 21

    MongoDB and Spring Data. Get size of array in entity

  22. 22

    numpy 배열을 (# dim1, # dim2, # channel)에서 (#channel, # dim1, # dim2)로 변경하는 방법

  23. 23

    How to get excel row numbers to skip the header

  24. 24

    How to get an alert when there are three images in a row

  25. 25

    How to get next row column by javascript or jquery

  26. 26

    How can find size of each Row in Apache spark sql dataframe and discrad the rows having size more than a threshold size in Kilobyte

  27. 27

    scala Spark get the top words in each row of Array

  28. 28

    How to get array of array value count?

  29. 29

    3 Dim을 2 Dim으로 변환하고 Ajust Class?

뜨겁다태그

보관