Thread: please help me to understand the question

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    39

    please help me to understand the question

    Hi guys,

    Sorry, can u please explain to me what is the sentence in red saying? I'm working on this problem and can do the previous parts until i reached this part. What is int n supposed to be and how is the function supposed to recognise whether the number needs to be swapped when it only takes in addresses?

    This entire question is supposed to arrange all columns in a 3 by 3 matrix in asecending order.

    ie.

    2 -17 11
    -1 11 -7
    0 3 -2

    to

    -1 -17 -7
    0 3 -2
    2 11 11

    Pls help to explain. thanks


    Write a function
    colSort to sort each column of the table in ascending order.

    void colSort(int table[][MAX], int rows, int cols);
    Use the following guide in your implementation.

    Within the colSort function, declare an array of integer pointers int *listPtr[MAX]; . Write a function getColumn that takes in the original table table, the number of rows, the column index cIndex as well as the listPtr array and fill
    the latter with the addresses of all the elements of the column cIndex.

    void getColumn(int table[][MAX], int rows, int cIndex, int *listPtr[]);
    Write a function ptrSort that takes as argument the listPtr array and sorts the column of the original table via the pointer elements of listPtr.

    vovvv void ptrSort(int *listPtr[], int n);

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    3 functions:

    colSort(), getColumn(), and ptrSort(). Instead of sorting the column values directly, the assignment wants your program to sort the values, THROUGH the pointers (and remembering that the pointers point to the values, by dereferencing them with the *).

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    39
    ok...i'll try that again later..thanks...

    i came across this online...can i ask u...if i want to express the following...

    gcd (m,n) = m if n=0
    = gcd (n, m%n) if n>0

    Can this be expressed as gcd= n==0 ? m : m%n

    the last part seems wrong....but i cant tell wads wrong...can u help me to edit it...

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by jollykiddo View Post
    ok...i'll try that again later..thanks...

    i came across this online...can i ask u...if i want to express the following...

    gcd (m,n) = m if n=0 <<===== This is an syntax error, should be if(n==0) in C
    = gcd (n, m%n) if n>0

    Can this be expressed as gcd= n==0 ? m : m%n

    the last part seems wrong....but i cant tell wads wrong...can u help me to edit it...
    You have a function gcd which takes two int's - one is the value that you're searching for the gcd of, and the other is a test int?

    Hard to say much about it without seeing the code.

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    39
    Quote Originally Posted by Adak View Post
    3 functions:

    colSort(), getColumn(), and ptrSort(). Instead of sorting the column values directly, the assignment wants your program to sort the values, THROUGH the pointers (and remembering that the pointers point to the values, by dereferencing them with the *).
    for the previous programmae...i got the listPtr from the the function get column...which i wrote the function as follows...but what's int n? what's input is the function ptrSort expecting?

    Code:
    void getColumn (int table [][3], int rows, int cIndex. int *listPtr[])
    {
            int i=0; 
            for(i=0; i<3; i++)
                listPtr[i]=&(table[i][cIndex])
    }

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I believe n is the number of the column that is to be sorted. Only one column at any time is being sorted.

  7. #7
    Registered User
    Join Date
    Dec 2011
    Posts
    39
    Quote Originally Posted by Adak View Post
    You have a function gcd which takes two int's - one is the value that you're searching for the gcd of, and the other is a test int?

    Hard to say much about it without seeing the code.
    Ok...i'll try the assignment in a while...meanwhile...can u advise whether this is correct...thanks

    Code:
     
      #include <stdio.h> 
        
       void swap (int *X, int *Y);
       int gcd (int m, int n); 
        
       int main ( ) 
       {   
           int GCD=0; 
           int m=0; 
          int n=0; 
          
          scanf("%d",&m); 
          scanf("%d", &n); 
          
          if (n>m) 
              swap(&m,&n); 
          GCD=gcd(m,n); 
          printf("%d", GCD);
       
          return 0; 
      } 
       
      void swap ( int *X, int *Y) 
      {
          int temp;
          temp= *X;
          *X= *Y;
          *Y= temp;
      }
      
      int gcd ( int m, int n)
      {
          return (n==0)? m : gcd(n,m%n) ;
      }
    are there any instances when this code will fail?

    so this operator '? :' is to replace if else?

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by jollykiddo View Post
    so this operator '? :' is to replace if else?
    Sort of. ?: is called the "ternary operator". A lot of languages use it.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please help me to understand this question
    By maatata in forum C Programming
    Replies: 7
    Last Post: 11-28-2011, 06:16 PM
  2. Please help me understand this question on c++
    By darksifer in forum C++ Programming
    Replies: 4
    Last Post: 04-14-2011, 06:34 AM
  3. Question i dont understand
    By muran_pling in forum C++ Programming
    Replies: 4
    Last Post: 06-03-2007, 06:22 AM
  4. I could not understand this question
    By enggabhinandan in forum C Programming
    Replies: 3
    Last Post: 10-22-2006, 05:17 AM
  5. To understand the question....
    By Forever82 in forum C++ Programming
    Replies: 1
    Last Post: 07-20-2003, 10:11 AM