Thread: Delete Rows And Colums Of Array

  1. #1
    Registered User
    Join Date
    Jan 2015
    Posts
    23

    Post Delete Rows And Colums Of Array

    Hello
    I want to write a program a[m][n];
    and then scans for a,b
    then delete a row and b column
    thank You

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Hello, I want to collect my nice programmer salary while you write the actual code for me, for free. Thank you.

    That sounds ridiculous, right? Yes, it does. Taking credit while others do the work is ridiculous. Read our homework policy: Announcements - General Programming Boards. Then, post back with a reasonable attempt at solving this yourself, and ask specific questions about where you're stuck. Then we will help you.

  3. #3
    Registered User
    Join Date
    Jan 2015
    Posts
    23
    that is not a homework sir its just a question of my exam which i could not solve it !
    Thank You
    please Help me Solving This Question Thank You ...

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by thedardwhie View Post
    that is not a homework sir its just a question of my exam
    Perhaps the term homework sounds too specific to you. Let me make it clearer:

    If your question involves someone else doing your school work for you, in any form, whether it's homework, in-class work, or quiz/exam questions, you will not receive the help you seek.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  5. #5
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    We'll give you hints though, my hint is the malloc.h header

  6. #6
    Registered User
    Join Date
    Jan 2015
    Posts
    23
    let me make it clearer for you ! i said i cant but you say you cant ask it and learn it and please be more polite

  7. #7
    Registered User
    Join Date
    Jan 2015
    Posts
    23
    no i don,t want to use malloc.h header

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Elkvis is right. The point is, we are here to help you learn. Doing the work for you will not help you learn. It will only teach you how to copy other's work. Would you learn how to be a good woodworker by asking people to build cabinets and shelves for you? No. You must learn to use all the tools, create the right joints for the boards, etc. It's okay to have somebody teach you and help you though. To walk you through it. Similarly, you will not become a good programmer by having others write code for you.

    If you can't do it all right now, that's fine. We're not asking you for a complete working solution (if you could do that, you wouldn't need our help). We're just asking that you make an honest attempt to do it yourself. Write as much as you can on your own. If there are parts you have difficulty with, ask for specific help on those parts, and we will help you.

    If you are really completely lost on working with arrays, to the point that you can't even declare or populate one, then I suggest you return to your class notes and textbook, and some online tutorials to brush up.

    Here is some free code to get you started
    Code:
    #include <stdlib.h>
    
    int main(void)
    {
        /* your code goes here */
        return EXIT_SUCCESS;
    }

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    If you were going to do it (not with a computer), how would you do it? What steps would you use to remove an item from an array of real objects, based on the row and column that someone gives you?

    Write down the steps you would take. Be as specific and verbose as possible. Then translate those steps into C code.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  10. #10
    Registered User
    Join Date
    Jan 2015
    Posts
    23
    i have problem in shifting the array numbers and my program do not work !

  11. #11
    Registered User
    Join Date
    Jan 2015
    Posts
    23
    And I dont want to write program just with simple codes not with any codes that wrote up

  12. #12
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Is it the "delete" part that is throwing you off?

    You don't actually delete the row or column in a 2D array; you just move the latter part of the data by one element (and, optionally, clear the last row/column). In C, with an array declared as a[rows][columns], each row is consecutive in memory. This means that if you "delete" a column, you must move the latter part of each row. If you "delete" a row, you can do it all at once. The memmove() function works well for moving parts of an array around.



    I personally don't use such 2D arrays in practice at all, because I need to be able to dynamically resize and allocate them. The structure I use to describe and manage them typically looks like
    Code:
    typedef struct dataref  dataref;
    
    typedef struct {
        int rows;
        int cols;
        long rowstride;
        long colstride;
        TYPE *element;
        dataref *owner;
    } matrix;
    
    struct dataref {
        struct dataref *next;
        long refcount;
        size_t size;
        TYPE data[];
    };
    where the dataref type is where the actual elements are stored, and each matrix just references the data. This allows multiple views to the same data, and I also use it to track the uses of the data (refcount) so that I don't need to remember which matrix "owns" the data; as long as I properly discard each matrix I use, the data gets freed when it is no longer needed.

    While the above allows vector and sub-matrix views and even transposes and reflections on the data without modifying it, it does not allow cutting a row or a column out of it. However, if I did need that kind of an interface, I might use something like
    Code:
    typedef struct {
        int rows;
        int cols;
        long *row;
        long *col;
        TYPE *element;
        dataref *owner;
    } matrix;
    Whereas the former version uses constant numbers of elements in the one-dimensional element array to go one element "right" (colstep) or "down" (rowstep), this latter one has offsets for each column and each row stored in separate arrays. This way you can pick any row and/or column order, and skip (and even duplicate) ones if you want to.

    The point is that even in this form I wouldn't actually "delete" anything; I'd just skip it.

    If I wanted to, I could create a copy, a duplicate, that didn't have the rows and/or columns skipped, and then free the original.

    It is very important to understand the concepts, and design your solution, rather than cobble together the code via trial and error without really understanding why and how it works.

  13. #13
    Registered User
    Join Date
    Jan 2015
    Posts
    23
    Sorry but i want code with out any struct or any thing else i want this code just with simple things like :loops
    Last edited by thedardwhie; 02-05-2015 at 02:03 PM.

  14. #14
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You're not going to just get some code, without making a genuine effort to write the code yourself. In case you're still having trouble with the general concept, here is some more help.

    Just to clarify, you have a 2-d array, declared like so
    Code:
    #define NUM_ROWS 5
    #define NUM_COLS 5
    int array[NUM_ROWS][NUM_COLS];
    So imagine you have an array like so, deleting the specified row and column
    Code:
     1  2  3  4  5
     6  7  8  9 10
    11 12 13 14 15  <-- "delete" this row (row 2, remember arrays start at 0 in C)
    16 17 18 19 20
    21 22 23 24 25
       ^
       |
    delete this column (column 1, remember arrays start at 0 in C)
    Which would result in something like
    Code:
     1  3  4  5 ??
     6  8  9 10 ??
    16 18 19 20 ??
    21 23 24 25 ??
    ?? ?? ?? ?? ??
    Where the ?? designates data we no longer care about. We started with a 5x5 array, deleted a row and column, and now have a 4x4 array.

    So, if you notice, all we did is basically move, or copy, data from columns to the right of the deleted column, one column to the left. Similarly, we moved/copied data from rows below the deleted row, one row up. We can just overwrite the data in the row/column to delete, and it gives us the desired array.

    You don't have to do this all at once. You can first delete the desired row, then the desired column, or you can do it the other way:
    Code:
     1  2  3  4  5                        1  2  3  4  5                           1  3  4  5
     6  7  8  9 10                        6  7  8  9 10                           6  8  9 10
    11 12 13 14 15  -- delete row 2 -->  16 17 18 19 20  -- delete column 1 -->  16 18 19 20
    16 17 18 19 20                       21 22 23 24 25                          21 23 24 25
    21 22 23 24 25
    
    
     1  2  3  4  5                           1  3  4  5                        1  3  4  5
     6  7  8  9 10                           6  8  9 10                        6  8  9 10
    11 12 13 14 15  -- delete column 1 -->  11 13 14 15  -- delete row 2 -->  16 18 19 20
    16 17 18 19 20                          16 18 19 20                       21 23 24 25
    21 22 23 24 25                          21 23 24 25

  15. #15
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    simple enough, for example:
    Code:
    int colCount = 10;
    int rowCount = 10;
    int **rows = mkRows( &rowCount, &colCount ); // initialise with 10 pointers and 10 elements per array pointed to
    rmRow( rows, &rowCount, 3 ); // free row then move pointers up 1
    rows[9] = mkRow( &colCount );
    rows = appendRow( rows, &rowCount ); // loop though looking for empty pointer and use realloc if reach end of loop
    rows = insertRow( rows, &rowCount, 6 ); // move pointers down 1 then use malloc for new array
    I'll leave implementing the actual code to you though
    EditLate post

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HELP 2d array sort rows by its sum value
    By Fromar123 in forum C++ Programming
    Replies: 2
    Last Post: 12-02-2012, 06:50 AM
  2. Swapping rows in a 2D array
    By bassist11 in forum C Programming
    Replies: 5
    Last Post: 03-11-2010, 12:04 PM
  3. displaying with rows and colums
    By tio1225 in forum C++ Programming
    Replies: 1
    Last Post: 09-23-2005, 05:07 PM
  4. sum rows & columns in array
    By ronenk in forum C Programming
    Replies: 7
    Last Post: 06-20-2004, 04:16 AM
  5. Rows and colums
    By Aluvas in forum C++ Programming
    Replies: 3
    Last Post: 11-14-2002, 10:18 PM

Tags for this Thread