Thread: beginners question

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    5

    beginners question

    Code:
    int i,t;
    int arr[10];
    
    scanf("%d", &t);
        for(i=1;i<=t;i++)
        scanf("%d", &arr[i]);
        for(i=1;i<=t;i++)
        {
           
           
        }
    if i am understanding this right t gets read from input and the first for loop says thats how many elements we are going to use in the array and assign them values...please correct me if i am wrong. Then the second for loop loops through each index of the array right? Assuming that I am correct how do I access the value in each index? I have tried many different ideas but I am always wrong. Any help is greatly appreciated

  2. #2
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    That's right.

    Remember though that array indexes start at 0 (i.e an array declared as int arr[3]; contains the three elements arr[0], arr[1] and arr[2]) so your loops are skipping the first element of the array. Also make sure not to allow input over 10 (well actually 9 in this case because you're starting at 1) so that you don't run over the end of your array!

    You're on the right track and you should keep reading up on arrays.

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    5
    so now that I am looping through the different indexes how do I use the values associated with that particular index at the time? say my first element in the array has 231 and the second element has 42 stored in them...How do I get that value and from the first element manipulate it and continue with the second element 42 and then the third forth fifth and so on? Thanks again for your help
    Last edited by James Charny; 02-08-2013 at 11:04 PM.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    If you can assign those numbers to an index you should already know. arrayName[ index ]

  5. #5
    Registered User
    Join Date
    Feb 2013
    Posts
    5
    Quote Originally Posted by whiteflags View Post
    If you can assign those numbers to an index you should already know. arrayName[ index ]
    i think it is another for loop i need if i am not mistaken thx

  6. #6
    Registered User
    Join Date
    Feb 2013
    Posts
    5
    Nope thought I had what i was looking for until i saw that semicolon after my next for loop.....Still unsure on how to process each element of the array, get its number without any other numbers, manipulate it and move on....still trying though :/

  7. #7
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Quote Originally Posted by James Charny View Post
    Code:
    int i,t;
    int arr[10];
    
    scanf("%d", &t);
        for(i=1;i<=t;i++)
        scanf("%d", &arr[i]);
        for(i=1;i<=t;i++)
        {
             printf( "%i  ", arr[i] ) ;  //print each value
             arr[i] += 2 ;                //add 2 to each value
        }
    Not sure what your confusion is. Perhaps you are trying to access arr the same way it was used in scanf(). If so, then understand that scanf() requires an address to store the input into, so the address-operator (&) was used for each arr element. But you don't use that when you want to access the value stored in the arr element.

  8. #8
    Registered User
    Join Date
    Feb 2013
    Posts
    5
    Quote Originally Posted by Tclausex View Post
    Not sure what your confusion is. Perhaps you are trying to access arr the same way it was used in scanf(). If so, then understand that scanf() requires an address to store the input into, so the address-operator (&) was used for each arr element. But you don't use that when you want to access the value stored in the arr element.
    What if i didnt want to print each value...only just the first one...do somethings to it but not add a value to the element...continue through the loop until I get to the next value. I feel that if I want to get the number from the current index I need another for loop inside of the empty one I have when I first drew up this post
    Last edited by James Charny; 02-09-2013 at 12:29 AM.

  9. #9
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Well, then don't print the value. I was just showing 2 examples of accessing the element. You'll have to be more specific about "do something".

    You don't need a nested loop. 1 dimensional array needs only 1 for() loop. 2 dimensional array needs 2 for() loops, and so on. Assuming you want to access each sequentially.

    Of course, you always use decision branching to skip certain elements, or use some algorithm to calculate an integer value to use as index for non-sequential access.

    But no, to access the element of the current index, you simply use the array name + index in an expression and it will be evaluated for the value bound to that element, i.e. arr[i].

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. beginners question
    By facebook1978200 in forum C Programming
    Replies: 18
    Last Post: 01-14-2013, 04:49 AM
  2. C beginners question
    By incipientmind in forum C Programming
    Replies: 5
    Last Post: 02-25-2008, 02:06 PM
  3. beginners question
    By c++prog in forum C++ Programming
    Replies: 4
    Last Post: 02-01-2008, 04:41 PM
  4. Beginners question
    By jamesstuart in forum C Programming
    Replies: 9
    Last Post: 09-24-2007, 03:24 PM
  5. Beginners OOP question
    By Skusey in forum C++ Programming
    Replies: 2
    Last Post: 11-06-2006, 06:10 AM