Homework help?

This is a discussion on Homework help? within the C++ Programming forums, part of the General Programming Boards category; So the beginning numbers are laid out like this: 8 0 3 6 3 1 3 1 0 7 2 ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    1

    Homework help?

    So the beginning numbers are laid out like this:
    8 0 3 6 3
    1 3 1 0 7
    2 7 9 4 5
    Then I have to print it out to look like:
    3 6 3 0 8
    7 0 1 3 1
    5 4 9 7 2.
    This is what I have so far, and i know its not right because i keep getting an error that the program has quit working.
    Code:
    void
    copyB(const int *fromPtr, int rows, int columns, int to[][10])
    {
        int r;
        int c;
    
        for (r =  columns -1; r < rows; r--) {
            for (c = 0; c < columns; c++)
                to[r][c] = *fromPtr++;
        }
    }
    This is not my major I technically dont even need this class anymore since i switched majors but i dont want to drop it now, so any tips are appreciated. I have to run it against my professors test driver, but i just can get this one to work right.
    Thanks for any help!

  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,526
    You don't need to copy it.
    Just swap the n'th element of each row with the (columns -n)'th element.(...iterating n over 0 to columns )
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    Well with the example you showed the function should not do anything. If columns is 5 and rows is 3, then r would = 4. When you enter the for loop, r is not less than rows.

  4. #4
    Registered User Amin sma's Avatar
    Join Date
    Mar 2012
    Posts
    5
    easy! This code works....
    Code:
    #include<iostream.h>
    
    
    void main()
    { int c[3][5];
    for(int i=0;i<3;i++)
    for(int j=0;j<5;j++)
    cin>>c[i][j];
    
    
    for(i=0;i<3;i++)
    {for(int j=4;j>=0;j--)
    cout<<c[i][j];
    cout<<endl;}
    }

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    @ Amin sma
    Note that we're here to help people figure out the answer for themselves.
    Not show off by dumping the complete answer at the first opportunity.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    Which, mind you, doesn't even compile on standard-compliant compilers!
    Last edited by GReaper; 03-29-2012 at 12:31 PM.
    Devoted my life to programming...

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    That solution is wrong in at least three different ways. For starters it should not use a two-dimensional array when a plain 1D array is sufficient.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User Amin sma's Avatar
    Join Date
    Mar 2012
    Posts
    5
    ok

  9. #9
    Registered User
    Join Date
    Mar 2012
    Posts
    110
    Quote Originally Posted by iMalc View Post
    That solution is wrong in at least three different ways. For starters it should not use a two-dimensional array when a plain 1D array is sufficient.
    True, but his array is to[c][r]. You know how it is teachers are always allowed do things like that. ;-)

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by überfuzz View Post
    True, but his array is to[c][r]. You know how it is teachers are always allowed do things like that. ;-)
    I'm assuming that the decision of the function prototype is his own, and so could be whatever. Afterall, it's doing such a trivial task (reversing an array) that I would not even create a function for it.
    Just read in each line and then print it out backwards. Amin got that part right, albeit with missing formatting.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #11
    Registered User
    Join Date
    Mar 2012
    Posts
    110
    Quote Originally Posted by iMalc View Post
    Afterall, it's doing such a trivial task (reversing an array) that I would not even create a function for it.
    It could pass as an exercise in the array part in some course. Ah well...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help about homework
    By agathery in forum C Programming
    Replies: 27
    Last Post: 05-19-2010, 10:17 PM
  2. I'll do your homework for you....
    By Salem in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 04-13-2006, 04:14 PM
  3. Homework Help
    By arstacey in forum C++ Programming
    Replies: 15
    Last Post: 12-04-2005, 02:54 AM
  4. Homework Help Using While
    By Trekkie in forum C++ Programming
    Replies: 9
    Last Post: 11-07-2005, 10:25 PM
  5. Homework Help!!!!!
    By bcianfrocca in forum C++ Programming
    Replies: 20
    Last Post: 09-12-2004, 08:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21