Results 1 to 18 of 18

Thread: Confused about using arrays

  1. #1

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037

    Confused about using arrays

    Does anyone know a good though teaching on how to use arrays with explanations as to how and why everything works?

  2. #2
    Hyperactive Member made_of_asp's Avatar
    Join Date
    Jul 2001
    Location
    123 Fake Street
    Posts
    394
    VS.NET 2003

    Need to email me?

  3. #3

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037
    Thank you very much. I hope I can get all my questions answered.

  4. #4

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037
    int billy [] = {16, 2, 77, 40, 12071};
    int n, result=0;

    int main ()
    {
    for ( n=0 ; n<5 ; n++ )
    {
    result += billy[n];
    }
    cout << result;
    return 0;
    }

    result = 12206

    I was trying to figure out why in the for loop isn't n<5 instead n<=5 because there are 5 elements in the array.

    I am thinking that the computer sees billy[n] as billy[0], billy[1], billy[2] . . .etc. when the loop is done. Am I right?
    Last edited by aewarnick; Jan 5th, 2003 at 03:16 PM.

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    There are 5 elements. The first has the index 0. Therefore the 5th has the index 4.
    If you had <= 5 then the loop would be executed with i=5 the last time, which would be an invalid index.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037
    Is i a standard in C++ for array indexes. In other words, is i a variable that could be anything and work just the same?

    and

    I am sorry to bog you down with so many questions but I have tried to figure this out for over an hour now.

    How does this work:

    #define WIDTH 5
    #define HEIGHT 3

    int jimmy [HEIGHT][WIDTH];
    int n,m;

    int main ()
    {
    for (n=0;n<HEIGHT;n++)
    for (m=0;m<WIDTH;m++)
    {
    jimmy[n][m]=(n+1)*(m+1);
    }
    return 0;
    }

    It is supposed to assign integers to every part of the array in 2d.

    1 2 3 4 5
    2 4 6 8 10
    3 6 9 12 15

    But all I can see it doing is this:

    1 2 3 4 5
    1
    2
    3

    Please, help me understand this.
    Last edited by aewarnick; Jan 5th, 2003 at 04:47 PM.

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Is i a standard in C++ for array indexes. In other words, is i a variable that could be anything and work just the same?
    The data type of i should be int, unsigned int or size_t but the name can be anything. It's just very common to use i (stands for index). If you need more than one index then usually j,k... or i2,i3... is used.
    Or other letters, like a,b..., x,y... or m,n....

    Before I answer the other question a quick tip:
    Wrap the code you post here in [code][/code] tags, it preserves the formatting and makes the code easier to read.
    Now for the question. I've changed the formatting of the code, but it will still run in the same way (actually it should compile to the same machine code).
    Code:
    for (n=0;n<HEIGHT;n++)
    {
      for (m=0;m<WIDTH;m++)
      {
        jimmy[n][m]=(n+1)*(m+1);
      }
    }
    What we have here are nested loops. Let's follow the program flow (and assume both WIDTH and HEIGHT are 2, or I'll get old writing this).
    The first for-loop starts (I will call it nfor, and the other loop mfor). n is set to 0. A check is performed, n is less than HEIGHT.
    The the inner loop, the mfor starts. m is set to 0, which is less than WIDTH.
    The expression evaluates to
    jimmy[0][0] = 1*1;
    m is incremented to 1, which is still less than WIDTH.
    Loop body is:
    jimmy[0][1] = 1*2;
    m is again incremented. 2 is no longer less than WIDTH, so the loop exits.
    The outer loop, however, doesn't exit. n is incremented to 1 (which is less than HEIGHT) and the whole thing starts over. m is reset to 0 and the loop bodies
    jimmy[1][0] = 2*1;
    and
    jimmy[1][1] = 2*2;
    are executed.

    Do you see now how nested loops fill an array completly?

    If you couldn't follow this, then try it with the debugger.
    In Visual C++, go to this codeline (place the caret there):
    for (n=0;n<HEIGHT;n++)
    Press F9 to place a breakpoint.
    A breakpoint tells the debugger to stop execution once the program reaches the line where the breakpoint is set.
    Press F5 to start debugging. VC++ might ask you to compile the program first. Do it.
    Then the program starts and immediatly stops at the breakpoint. You will see a small yelllow arrow pointing to the line that is about to be executed.
    Now you can step through your code step by step. Press F10 to execute one line of code. There should be a watcher window somewhere (if not, you can open it from the Debug menu) where you can see the variable values change.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  8. #8

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037
    I see how it works. I guess I didn't understand nested loops. I do not have a compiler that works right. Dev c++ never compiles for me.

    I was at Borders the other day and I saw a book called c++ in 21 days. It was bundled with a cd that was supposed to be a student vc++ or something like that. Is it worth getting? It was $50.

  9. #9
    Hyperactive Member made_of_asp's Avatar
    Join Date
    Jul 2001
    Location
    123 Fake Street
    Posts
    394
    Sams Teach Youself C++ in 21 days Third Edition is not a very good book, as it looks to me. Too confusing and goes into classes too fast.
    VS.NET 2003

    Need to email me?

  10. #10

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037
    Actually I am specifically asking about the VC++ student edition (I am not sure what is was called). Is it worth getting for $50.

  11. #11
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Not sure. I could get the student version of the complete Visual Studio 6 for $10 at the university.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  12. #12
    Addicted Member jmiller's Avatar
    Join Date
    Jul 2002
    Location
    University of Michigan
    Posts
    238
    Is it just me, or is it pretty much impossible to learn any language from a Sams book in the amount of time that they allot? I've had that Sams book for a couple months now, and I still haven't learned C++.....

  13. #13
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Apparently i, j, and k are traditionally used as loop index variables, since in FORTRAN they were explicitly reserved in the language for that purpose.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  14. #14
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    I wasn't yet born when FORTRAN was used
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  15. #15
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    I've used it Not of my own choice, I hasten to add...
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  16. #16

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037
    Right now I have the C++ how to program book by Deitel and Deitel. It seems to be pretty good, although I still don't think it explains some things in detail enouph.

    This for example:

    int main(){

    const int Size1=10, Size2=11;
    int responses[Size1]={1, 2, 6, 4, 8, 2, 6, 1, 8, 4};
    int frequency[Size2]={0};

    for (int answer=0; answer<Size1;answer++)
    ++frequency[responses[answer]]; //this is what I don't understand.

    for (int rating=1; rating<Size2;rating++)
    cout<< rating << setw(17) << frequency[rating] << endl;

    return 0;}

    Apparently when the program runs it uses ++frequency[responses [answer]] to read the numbers in responses, one at a time using the loop in and places them somewhere in frequency but I have no idea how that works. Please help.
    Last edited by aewarnick; Jan 6th, 2003 at 01:25 PM.

  17. #17
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    It's just the usual array indexing.

    ++frequency[responses[answer]];
    The subexpressions are executed like this:
    1st SE:
    answer
    2nd SE:
    responses[result 1st SE]
    3rd SE:
    frequency[result 2nd SE]
    4th SE:
    ++result 3rd SE

    Let's say it's the first iteration of the for loop. Then answer evaluates to 0.
    2nd SE is therefore
    responses[0]
    which retrieves the first element of the responses array. This happens to be 1.
    3rd SE:
    frequency[1]
    The second element of frequency is accessed...
    4th SE:
    ++frequency[1]
    ... and incremented. This way you can count how often each number occurs in the responses array.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  18. #18

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037
    Ok, no more explanation needed. I got it now. Thank you very much!

Posting Permissions

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



Click Here to Expand Forum to Full Width