#help with the "transpose matrix" question

16 messages · Page 1 of 1 (latest)

queen crest
#

I tried to print a matrix for home work in a transpose type of way but without pronting the first element of each array (printing place [1] and forward...).
but when i rint it the output shows me that at at the end of every col theres another number thats not suppose to be there (chatgpt says its "garbage") so can someone please help me fix my program in any way cuz i tried for 2 hours and havent figiured it out yet.

-btw one of the things i cant use is indexs in loops and such like int i/j so i have to use only pointers.

#include <stdio.h>

void transpose(int **arr){
  int lenrows = 0;
  int **temp = arr;
  while(*temp != NULL){
    lenrows = lenrows + 1; //ספרתי את שורות הפויינטר ALL
    temp = temp + 1;
  }
  
  int maxCol = 0; //לספירת העמודות
  int **rc = arr;//fst pointer
  while (*rc != NULL) {
    int *colpointer = *rc;   // מצביע לתחילת השורה
    int cur = *colpointer;  // הערך הראשון מציין את האורך של השורה

    if (cur > maxCol){
        maxCol = cur;
    }
    
    rc++;
  }
  
  int col = 0;
  while(col < maxCol){
    int **row = arr;
    while(*row != NULL){
      int *currow = *row;
      int size = *currow;
      
      if (col < size) {
          printf("%-6d", *(currow + col + 1));
        }else{
          printf("      ");
        }
        row = row +1;
    }
    printf("\n");
    col = col +1;
  }
}

int main(){
int A[]={5,-5,14,5,2};
int B[]={3,6,11};
int C[]={4,1,-3,4};
int D[]={6,2,7,1,8,2};
int E[]={2,15};
int F[]={3,4,-2};
int *All[]={A,B,C,D,E,F,NULL};//מערך מצביעים למערכים שבהם יש מספרים

printf("transpose answer: \n");
transpose(All);
}

(i added the og qustion if annyone wanna read)

strange girderBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

soft blade
#

BTW, an "indexless" for loop (depending on how you define "index" IG) could bec int arr[LEN] = {/* whatever */}; for (int* arrPtr = arr; arrPtr < arr + LEN; arrPtr++) { // etc } where at every step of the loop, *arrPtr would be equivalent to arr[i] in a "normal" for loop

soft blade
acoustic tapir
queen crest
acoustic tapir
queen crest
#
      for(int **ptr = arr; *ptr < arr; ptr++){
      }
queen crest
acoustic tapir
soft blade
# queen crest ```C for(int **ptr = arr; *ptr < arr; ptr++){ } ```

ptr < arr + arrLen, where arrLen is the length of the array as an integer, in this case. you want to compare pointers of equal types when you can. in this case, both arr and ptr are of type int**; by dereferencing ptr like that, you're now comparing an int** to an int*. it's probably legal code (in that it will compile), but it won't do what you want it to, because you're comparing an actual pointer with the thing it points to

actually, if this is for the null-terminated array, you'd want to have ptr or ptr != NULL as your condition

strange girderBOT
#

This question is being automatically marked as stale.
If your question has been answered, type !solved.
If your question is not answered feel free to bump the post or re-ask.
Take a look at !howto ask for tips on improving your question.

queen crest
#

!solved