Thread: Need help in writing a code (assignment)

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    5

    Need help in writing a code (assignment)

    I need to write a code (in C99). The programe receives an input number (integer) 'n' and then a matrix sized n*n (matrix[n][n]), all numbers are integers. We're supposed to define the matrix's size with malloc.
    Now the program needs to sort the lines of the matrix (the number in each line), in a single function (!) this way:
    even line index (0,2,4,6...): from small to big.
    odds line index (1,3,5...): from big to small.
    and then print it.
    *note: first line is indexed 0, second line is 1, etc.

    I was thinking to sort it with bubblesort function with the following if:
    if(i%2==1)
    do odds sorting.
    else
    do even sorting.
    when i is the index of the row.

    my problem is defining the malloc and how do I send the matrix to sorting.
    please help

    If needed I will attach my current (not so good (completly awful)) code and functions as well as an example of what the prog. supposed to do.

    *sorry for grammer.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,730
    Quote Originally Posted by Akruze View Post
    If needed I will attach my current (not so good (completly awful)) code and functions as well as an example of what the prog. supposed to do.
    Do that, plz. Don't forget to use code tags.
    Devoted my life to programming...

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,114
    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.

  4. #4
    Registered User
    Join Date
    Jan 2014
    Posts
    5
    Quote Originally Posted by GReaper View Post
    Do that, plz. Don't forget to use code tags.
    Need help in writing a code (assignment)-untitled-png
    Code:
    void swap(int *p, int *q){
        int temp=*p;
        *p=*q;
        *q=temp;
    }
    
    
    int bubble(int a[][], int m)
    {
        int swapped=0;
        for (int i=0; i<m; i++)
        {
            if ((i%2)==0)
            {
                for (int left=0; left<m-1; left++)
                {
                    int right=left+1;
                    if (a[i][left]>a[i][right])
                    {
                        swap (a+left, a+right);
                        swapped++;
                    }
                }
            }
            if ((i%2)==1)
            {
                for (int left=0; left<m-1; left++)
                {
                    int right=left+1;
                    if (a[i][left]<a[i][right])
                    {
                        swap (a+left, a+right);
                        swapped++;
                    }
                }
            }
    
    
        }
        return swapped;
    }
    
    
    void bubblesort (int a[][], int n)
    {
        for (int last=n-1; last>0; last--)
        {
            if (bubble (a, last+1)==0)
            break;
        }
    }
    
    
    
    
    
    
    
    
    int main()
    {
        int n=0;
        scanf(" %d", &n);
        int matrix[n][n];
        for (int i=0; i<n; i++)
        {
            for (int j=0; j<n; j++)
            {
                scanf(" %d", &matrix[i][j]);
            }
        }
        bubblesort(matrix, n);
        for (int i=0; i<n; i++)
        {
            for (int j=0; j<n; j++)
            {
                printf("%d ", matrix[i][j]);
            }
        }
    
    
    
    
    
    
      return 0;
    }
    I think my problem is how I send the matrix to the func. I think I need to seperate it into one dimensinal arrays and sending each individualy to sorting by the row index.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The one dimensional arrays you need, are already there - waiting for you. In C, a two D array has 2 (maybe more) one dimensional arrays, inside it. One for each row.

    With a[n][n], a[0], is one one dimension array (the first row). a[1] is another, etc.

    This looks like a problem from SPOJ.

  6. #6
    Registered User
    Join Date
    Jan 2014
    Posts
    5
    Quote Originally Posted by Adak View Post
    The one dimensional arrays you need, are already there - waiting for you. In C, a two D array has 2 (maybe more) one dimensional arrays, inside it. One for each row.

    With a[n][n], a[0], is one one dimension array (the first row). a[1] is another, etc.

    This looks like a problem from SPOJ.
    I've modified my code:
    Code:
    #include <stdio.h>#include <stdlib.h>
    
    
    void bubble(int m, int a[m], int i)
    {
        int c=0;
        if(m<=1) return;
        bubble(m-1, a, i);
        if ((i%2)==0)
        {
            for (int left=0; left<m-1; left++)
            {
                int right=left+1;
                if (a[left]>a[right])
                {
                    c=a[left];
                    a[left]=a[right];
                    a[right]=c;
                }
            }
        }
        else
        {
            for (int left=0; left<m-1; left++)
            {
                int right=left+1;
                if (a[left]<a[right])
                {
                    c=a[left];
                    a[left]=a[right];
                    a[right]=c;
                }
            }
        }
    
    
    
    
    }
    
    
    int main()
    {
        int n=0;
        scanf(" %d", &n);
        int matrix[n][n];
        for (int i=0; i<n; i++)
        {
            for (int j=0; j<n; j++)
            {
                scanf(" %d", &matrix[i][j]);
            }
        }
        for (int i=0;i<n;i++)
        {
        bubble(n, matrix+i*n,i);
        }
        for (int i=0; i<n; i++)
        {
            for (int j=0; j<n; j++)
            {
                printf("%d ", matrix[i][j]);
            }
            printf("\n");
        }
    
    
    
    
    
    
      return 0;
    }
    now it says:
    Code:
    In function 'main':|53|error: passing argument 2 of 'bubble' from incompatible pointer type|
    |4|note: expected 'int *' but argument is of type 'int (*)[(unsigned int)(n)]'|
    why?
    I believe that how I send my array does send it as a one dimensional array to my func.
    Or am I mistaken?

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Nope. matrix is an array of arrays (which can decay to a pointer-to-array, as it has done here). A pointer-to-array is not the same as an array. matrix[0] is a one-D array. matrix[i] is a one-D array. matrix is not, and matrix+i*n is not only not an array, it probably doesn't point to valid memory.

  8. #8
    Registered User
    Join Date
    Jan 2014
    Posts
    5
    Quote Originally Posted by tabstop View Post
    Nope. matrix is an array of arrays (which can decay to a pointer-to-array, as it has done here). A pointer-to-array is not the same as an array. matrix[0] is a one-D array. matrix[i] is a one-D array. matrix is not, and matrix+i*n is not only not an array, it probably doesn't point to valid memory.
    So can I send different rows to my func as it is now with this?:
    Code:
    for (int i=0;i<n;i++)
    {
      bubble(n, matrix[i], i)
    }
    considering matrix is a 2D array?

  9. #9
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Yes you can send rows to the bubble() function like that. Note that you pass matrix[i] which is an entire row. Therefore matrix[i] must be a pointer to an allocated area that holds a row's worth of integers.

    You haven't defined the matrix with malloc yet - that's the first thing.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You don't need to use malloc, at all. This is a static array.

    Your sort code is the first thing - it's a Substitution sort, not a Bubble sort. While these two sort algorithms are very close to each other, here's the difference: the Bubble sort ALWAYS compares adjacent values, as it sorts. The Substitution sort STARTS comparing just one adjacent value, and then compares only non-adjacent values, as it proceeds to sort.

    Your call to Bubble sort should be:
    Code:
        for (int i=0;i<n;i++)
        {
           bubble(n,i,matrix[i]);   //i goes first, before matrix[i]
        }
    The function's first line should be:
    Code:
    void bubble(int n, int i, int a[i])
    If int i, is not listed before a[i], you should receive an error or warning, from your compiler.

    This is a correct Substitution sort:
    Code:
            for (left=0; left<n-1; left++)   //Substitution sort
            {
                
                for(right=left+1;right<n;right++) //starts with 1 comparison to 
                if (a[left]>a[right])             //the adjacent value
                {
                    c=a[left];                    //then begins comparing non
                    a[left]=a[right];             //adjacent values
                    a[right]=c;
                }
            }
    and this is a correct Bubble sort:
    Code:
         do {
           swapped = 0;
           for(left =0; left < n-1; left++) {  //Bubble sort
             if(a[left] < a[left + 1 ]) {      //always compares adjacent values
                c=a[left];
                a[left]=a[left+1];
                a[left+1]=c;
               
                swapped = 1;
             }
           } //end for
           --n;                               //a reasonable optimization
         }while(swapped);               //for a small quantity sorter, only.
    Both of these sorting algorithms are good for small quantities, only. Don't even THINK about using them for a large quantity sort - they fall right on their faces.

  11. #11
    Registered User
    Join Date
    Jan 2014
    Posts
    5
    Quote Originally Posted by nonoob View Post
    Yes you can send rows to the bubble() function like that. Note that you pass matrix[i] which is an entire row. Therefore matrix[i] must be a pointer to an allocated area that holds a row's worth of integers.

    You haven't defined the matrix with malloc yet - that's the first thing.
    Quote Originally Posted by Adak View Post
    You don't need to use malloc, at all. This is a static array.

    Your sort code is the first thing - it's a Substitution sort, not a Bubble sort. While these two sort algorithms are very close to each other, here's the difference: the Bubble sort ALWAYS compares adjacent values, as it sorts. The Substitution sort STARTS comparing just one adjacent value, and then compares only non-adjacent values, as it proceeds to sort.

    Your call to Bubble sort should be:
    Code:
        for (int i=0;i<n;i++)
        {
           bubble(n,i,matrix[i]);   //i goes first, before matrix[i]
        }
    The function's first line should be:
    Code:
    void bubble(int n, int i, int a[i])
    If int i, is not listed before a[i], you should receive an error or warning, from your compiler.

    This is a correct Substitution sort:
    Code:
            for (left=0; left<n-1; left++)   //Substitution sort
            {
                
                for(right=left+1;right<n;right++) //starts with 1 comparison to 
                if (a[left]>a[right])             //the adjacent value
                {
                    c=a[left];                    //then begins comparing non
                    a[left]=a[right];             //adjacent values
                    a[right]=c;
                }
            }
    and this is a correct Bubble sort:
    Code:
         do {
           swapped = 0;
           for(left =0; left < n-1; left++) {  //Bubble sort
             if(a[left] < a[left + 1 ]) {      //always compares adjacent values
                c=a[left];
                a[left]=a[left+1];
                a[left+1]=c;
               
                swapped = 1;
             }
           } //end for
           --n;                               //a reasonable optimization
         }while(swapped);               //for a small quantity sorter, only.
    Both of these sorting algorithms are good for small quantities, only. Don't even THINK about using them for a large quantity sort - they fall right on their faces.
    Thank you both so much!!!! It seems I didn't really understood few things with func. and sorting, you cleared some of my misunderstandings, thank you!!!
    BTW, so I won't have to bother this forum with stupid questions so much, can you recommand me on a site (or anything of the sort) that can help me improve in C? A site that has theoretical material (this part is not really critical as this->) and assigments (pref. with solutions)?

  12. #12
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Quote Originally Posted by Adak View Post
    You don't need to use malloc, at all. This is a static array.
    Read the requirement. Although in its current form the array is static, it will need to be changed to comply with what was asked for.

  13. #13
    Registered User
    Join Date
    Dec 2013
    Posts
    26
    Hello mate, I am new too. Here, this site is actually the best place I have found go to their tutorials C Tutorial - Learn C - Cprogramming.com. beyond that, I recommend reading the Kernighan and Richie book "The C programming language" they wrote the language, so it's definitely worth a read. Then I discovered this site for things to program, simple iterative mathematical problems designed to be programed and they fit nicely with each other: Project Euler they are all mathematical problems which work well with C.

    Regards

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by nonoob View Post
    Read the requirement. Although in its current form the array is static, it will need to be changed to comply with what was asked for.
    I was helping fix the sorting function problem. You were answering about the malloc'ing problem. I saw no reason to add the malloc'ing code into the example of the sorting code - you were there.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 29
    Last Post: 07-19-2013, 06:14 AM
  2. First Computer Program Assignment using c code?
    By blisman20 in forum C Programming
    Replies: 4
    Last Post: 09-11-2012, 08:26 PM
  3. Changing writing code to reading code
    By binks in forum C Programming
    Replies: 8
    Last Post: 06-12-2012, 09:41 AM
  4. writing good code is like writing an artistic expression
    By renzokuken01 in forum C Programming
    Replies: 5
    Last Post: 02-03-2011, 08:48 PM
  5. C++ code for school assignment
    By Pupo in forum C++ Programming
    Replies: 3
    Last Post: 03-09-2010, 08:11 AM