Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. What's wrong.... [modified]

What's wrong.... [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
15 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Astricks

    The below code sorts a matrix diagonally. Does it, but at the end it's throwing an "illegal" may be a bad access error. Why ?int main(int argc, char* argv[]) { int A[5][5];int i;int j; int temp; int c,flag; }
    input : 1-1-1 2-2-2 3-3-3 output: 1-2-3 1-2-3 1-2-3 -- modified at 6:02 Friday 12th January, 2007

    *

    N Offline
    N Offline
    neilsolent
    wrote on last edited by
    #6

    In these lines: if (j-1 > 0 && i+1 <=5 ) { if (A[i][j] < A[i+1][j-]) "i" can be as large as 4. Which means elements A[5][j] are being accessed - but the declaration was int A[5][5] - i.e. the maximum element is A[4][j] So array out of bounds ..

    cheers, Neil

    1 Reply Last reply
    0
    • CPalliniC CPallini

      you're wasting 11 integers...:):-D:)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

      E Offline
      E Offline
      Eytukan
      wrote on last edited by
      #7

      in a way it's future proof ;P :rolleyes:. Just kidding!


      Code-Frog:So if this is Pumpkinhead. Time for him to run and hide. It's an interesting thought really.

      1 Reply Last reply
      0
      • E Eytukan

        int A[5][5];int A[6][6];


        Code-Frog:So if this is Pumpkinhead. Time for him to run and hide. It's an interesting thought really.

        R Offline
        R Offline
        Rajesh R Subramanian
        wrote on last edited by
        #8

        This will surely work, but the poor guy will never come to know what is wrong with his approach! I think thats the reason for you being voted down. (I gave a 5 to bring u up :-D because you put your time and efforts and shouldn't get voted down)


        Nobody can give you wiser advice than yourself. - Cicero ப்ரம்மா

        E 1 Reply Last reply
        0
        • R Rajesh R Subramanian

          This will surely work, but the poor guy will never come to know what is wrong with his approach! I think thats the reason for you being voted down. (I gave a 5 to bring u up :-D because you put your time and efforts and shouldn't get voted down)


          Nobody can give you wiser advice than yourself. - Cicero ப்ரம்மா

          E Offline
          E Offline
          Eytukan
          wrote on last edited by
          #9

          It was a quick-fix :rolleyes:, But I said that to indicate the error is about array-out-of-bounds. The 1 voted would have missed my point. :sigh:... anyway thanks :)


          Code-Frog:So if this is Pumpkinhead. Time for him to run and hide. It's an interesting thought really.

          J 1 Reply Last reply
          0
          • E Eytukan

            It was a quick-fix :rolleyes:, But I said that to indicate the error is about array-out-of-bounds. The 1 voted would have missed my point. :sigh:... anyway thanks :)


            Code-Frog:So if this is Pumpkinhead. Time for him to run and hide. It's an interesting thought really.

            J Offline
            J Offline
            John R Shaw
            wrote on last edited by
            #10

            I decided to add my 1 to the vote, because 0 was not a choice. You received a 1 vote because you did not provide an answer to the question. If the person asking the question understood your [joke] answer then they would not have needed to ask it in the first place. :sigh: Sorry, but the idea is to help and not make fun of the person asking the question.

            INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra

            E 1 Reply Last reply
            0
            • J John R Shaw

              I decided to add my 1 to the vote, because 0 was not a choice. You received a 1 vote because you did not provide an answer to the question. If the person asking the question understood your [joke] answer then they would not have needed to ask it in the first place. :sigh: Sorry, but the idea is to help and not make fun of the person asking the question.

              INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra

              E Offline
              E Offline
              Eytukan
              wrote on last edited by
              #11

              hmm :(


              Code-Frog:So if this is Pumpkinhead. Time for him to run and hide. It's an interesting thought really.

              H 1 Reply Last reply
              0
              • E Eytukan

                hmm :(


                Code-Frog:So if this is Pumpkinhead. Time for him to run and hide. It's an interesting thought really.

                H Offline
                H Offline
                Hamid Taebi
                wrote on last edited by
                #12

                It seems that on this forum we must only answer to questions;)


                WhiteSky


                1 Reply Last reply
                0
                • P prasad_som

                  You are accessing declared array with invalid index. int A[5][5]; should be accessed only from A[0][0] to A[4][4] where as you are accessing it through A[1][1] to A[5][5]

                  Astricks wrote:

                  for (i = 1;i<=5;i++) {

                  You should modify this to,

                  for (int i = 0 ; i <5 ; i++)

                  at all places you have done mistake.

                  Prasad Notifier using ATL | Operator new[],delete[][^]

                  A Offline
                  A Offline
                  Astricks
                  wrote on last edited by
                  #13

                  thanks !

                  *

                  1 Reply Last reply
                  0
                  • CPalliniC CPallini

                    C/C++ arrays are 0-based, i.e. if you declare:

                    int a[5];

                    then you have the following items:

                    a[0], a[1], a[2], a[3], a[4];

                    on the other hand, a[5], is out-of-bounds. so the correct iteration will be

                    for (i = 0;i<5;i++)
                    a[i]= (whatever);

                    :)

                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

                    A Offline
                    A Offline
                    Astricks
                    wrote on last edited by
                    #14

                    thanks! :)

                    *

                    1 Reply Last reply
                    0
                    • E Eytukan

                      int A[5][5];int A[6][6];


                      Code-Frog:So if this is Pumpkinhead. Time for him to run and hide. It's an interesting thought really.

                      A Offline
                      A Offline
                      Astricks
                      wrote on last edited by
                      #15

                      Thanks :(

                      *

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups