Thread: Need to swap row woth column

  1. #1
    Registered User
    Join Date
    Oct 2020
    Posts
    5

    Need to swap row woth column

    I need to swap the row that includes the maximal element with the column that includes the minimal element.
    Here's my code with the mistake:


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main()
    {
      int N;
      int i, j, max, min, rowmax, colmin, temp;
      printf("The size of array: ");
      scanf("%u", &N);
      int **arr = calloc(N, sizeof(int *));
      for (int i = 0; i < N; ++i) {
        arr = calloc(N, sizeof(int));
        for (int j = 0; j < N; ++j) {
          printf("array[%u][%u] = ", i, j);
          scanf("%d", &arr[j]);
        }
      }
      for (int i = 0; i < N; ++i) {
        for (int j = 0; j < N; ++j)
          printf("%d ", arr[j]);
        printf("\n");
      }
      printf("\n");
    
      for (int i = 0; i < N; ++i) {
        for (int j = 0; j < N; ++j) {
          if (arr[j] > max) {
            max = arr[j];
            arr[j] = arr[rowmax][j];
            arr[rowmax][j] = max;
          }
        }
      }
      for (int i = 0; i < N; ++i) {
        for (int j = 0; j < N; ++j) {
          if (arr[j] < min) {
            min = arr[j];
            arr[j] = arr[colmin];
            arr[colmin] = min;
          }
        }
      }
      for (i = 0; i < N; i++) {
        for (j = 0; j < N; j++) {
          temp = arr[j];
          arr[j] = arr[rowmax][colmin];
          arr[rowmax][colmin] = arr[colmin][rowmax];
          arr[colmin][rowmax] = temp;
        }
      }
      for (i = 0; i < N; i++) {
        for (j = 0; j < N; j++)
          printf("%d ", arr[j]);
        printf("\n");
      }
      return 0;
    }
    Last edited by Salem; 10-28-2020 at 09:55 PM. Reason: Removed crayola

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It sounds like you need to do some debugging: put aside the rest of your code for now and just check that your loop to find "the row that includes the maximal element" is correct. When you're satisfied that it works, check that your loop to find "the column that includes the minimal element" is correct. Then check that both are correct when put together. Then check that the swapping is correct separately. Then put them all together and test again.

    You might want to move the code that prints the array into a function that you can call twice. In fact, all these "check this part separately" that I mentioned earlier can be automated if you move those loops into their own functions. You can then write code that constructs different arrays (directly, not using user input), passes them to the functions, and checks that the return value or resulting array is correct. This is the notion of "unit testing", and when you combine them together for testing it might be called "integration testing".

    Since you know that there will be N*N elements, you don't need to do N+1 invocations of calloc. For example, you could call calloc once to allocate all N*N elements, then call it another time to allocate N pointers to point to the elements so as to access them as a 2D array. You should also free what you calloc, although it isn't strictly necessary in this case as the program exits soon after.

    By the way, it would be better to post your code without additional markup as the forum software will add syntax highlighting and line numbering for you as long as you post in code bbcode tags.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > int **arr = calloc(N, sizeof(int *));
    > arr = calloc(N, sizeof(int));
    You have two things called 'arr' going on at the same time.

    > max = arr[j];
    > arr[j] = arr[rowmax][j];
    > arr[rowmax][j] = max;
    You then try to use one of them in two different ways.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 11-11-2018, 07:31 PM
  2. Problem woth new List
    By R0tleSS in forum C Programming
    Replies: 4
    Last Post: 02-05-2018, 12:14 AM
  3. Replies: 6
    Last Post: 12-09-2010, 06:27 PM
  4. Problem woth CLK_TCK
    By ramayana in forum C Programming
    Replies: 5
    Last Post: 11-03-2006, 07:31 PM
  5. Need help woth fractions..
    By snapshooter in forum C Programming
    Replies: 4
    Last Post: 11-17-2004, 06:51 PM

Tags for this Thread