Trace table for an array
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: Trace table for an array

  1. #1
    Join Date
    Feb 2014
    Posts
    6

    Trace table for an array

    So far I realize that when it enters the while loop, track will be 0, marr[track] will be marr[3]. is this correct or will 23+3 be the correct understanding. Does this mean it will be a infinite loop since track is never incremented. it will always be track<7

    int marr[6]={23,67,12,9,33,7};
    int track =0;
    int ans = 2;

    while(track<7)
    {
    marr[track] = marr[track] + 3;
    if(marr[track]<20)
    ans = marr[track] / ans;
    }
    Last edited by andraesatchell; February 6th, 2014 at 01:01 PM.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,524

    Re: Trace table for an array

    Quote Originally Posted by andraesatchell View Post
    ... then the If statement will be marr[6]<20? which is false because there is no index 6. Is this process the correct way?


    Question
    int marr[6]={23,67,12,9,33,7};
    You declare the array marr[6];
    which means that it contains six elements having indexes from 0 to 5. marr[6] is outside the allocated buffer!
    Victor Nijegorodov

  3. #3
    Join Date
    Feb 2014
    Posts
    6

    Re: Trace table for an array

    Thank you for the clarification

    marr[track] = marr[track] + 3; enters the while loop, track will be 0, marr[track] will be marr[3]

    is this part correct. is it I should add 3 +23 or 0+3

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,524

    Re: Trace table for an array

    Quote Originally Posted by andraesatchell View Post
    Code:
    int marr[6]={23,67,12,9,33,7};
    int track =0;
    int ans = 2;
    
    while(track<7)
    {
       marr[track] = marr[track] + 3;
       if(marr[track]<20)
          ans = marr[track] / ans;
    }
    Quote Originally Posted by andraesatchell View Post
    when it enters the while loop, track will be 0, marr[track] will be marr[3].
    No.
    when it enters the while loop, track will be 0, marr[0] will be 23 + 3 = 26.
    Victor Nijegorodov

  5. #5
    Join Date
    Feb 2014
    Posts
    6

    Re: Trace table for an array

    understand. so track will always be 0 since there is no incrementation in the code, so it will always be adding 3 to marr [0]

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,524

    Re: Trace table for an array

    Exactly!
    Victor Nijegorodov

  7. #7
    Join Date
    Feb 2014
    Posts
    6

    Re: Trace table for an array

    Thanks again. Your assistance was greatly appreciated.

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,524

    Re: Trace table for an array

    Quote Originally Posted by andraesatchell View Post
    Resolved
    You must not remove nor edit your OP after you have resolved the problem!
    A lot of beginner could search for the problems similar to yours and after reading the thread very easy solve their ones. But now the whole thread you have started does not make any sense!
    Victor Nijegorodov

  9. #9
    Join Date
    Feb 2014
    Posts
    6

    Re: Trace table for an array

    My apology for doing same. I have made the amendment. Thats a valid point you made.thanks again for everything

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center