Can anyone give me a good description of an array?

This is a discussion on Can anyone give me a good description of an array? within the C Programming forums, part of the General Programming Boards category; alright i dont get some part of this program I already posted from another topic about another problem i had ...

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    17

    Can anyone give me a good description of an array?

    alright i dont get some part of this program
    I already posted from another topic about another problem i had but now i have a new one .

    Code:
    #include <stdio.h>
    
    main()
    {
          int c, i, nwhite, nother;
          int ndigit[10];
          
          nwhite = nother = 0;
          for(i = 0; i < 10; ++i)
               ndigit[i] = 0;
               
          while((c = getchar()) != EOF)
                if(c >= '0' && c <= '9')
                   ++ndigit[c-'0'];
                else if (c == ' ' || c == '\n' || c == '\t')
                         ++nwhite;
                else
                      ++nother;
                      
          printf("digits =");
          for (i = 0; i < 10; ++i)
               printf(" %d", ndigit[i]);
          printf(", white space = %d, other = %d\n", nwhite, nother);
    
          getchar();
    }
    alright 1st of all, in the 1st for loop if ndigit has already been declared 0s for the subscript values, how it needs another for loop to print all the values of it. I took the 2nd for loop out and it would only print 1 value. but should the 1st for loop take care of that?

    Another thing, since i has been used in the loops to stand for the values in ndigit, how come when i take out the ndigit[i] with only i, it doesn't set the values to 0 but 0123456789?

  2. #2
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    Your question isnt clear.

  3. #3
    Sys.os_type="Unix";;
    Join Date
    Aug 2005
    Posts
    52
    Code:
    #include <stdio.h>
    
    main()
    {
          int c, i, nwhite, nother;
          int ndigit[10];  //array of ten integers
          
          nwhite = nother = 0;
    //This for loop is setting each element in the array to a 0
          for(i = 0; i < 10; ++i)
               ndigit[i] = 0;  
    //Basically it's setting each element ndigit[0] ndigit[1]...ndigit[9]
               
          while((c = getchar()) != EOF)
                if(c >= '0' && c <= '9')  //if c is a digit
                   ++ndigit[c-'0'];  //add 1 to the appropriate element
                else if (c == '  ' || c == '\n' || c == '\t')
                         ++nwhite;
                else
                      ++nother;
                      
          printf("digits =");
          for (i = 0; i < 10; ++i)
               printf(" %d", ndigit[i]); //prints out each element of ndigit
    //which by the code above will be a count of occurences of 0-9
          printf(", white space = %d, other = %d\n", nwhite, nother);
          getchar();
    }

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    17
    alright that answers the 1st part of my question but how come in the printf part i have to put ndigit[i] and not just with "i"? I tried that and not all values of ndigit is set to 0 but shouldnt "i" contain all the values of ndigit??

  5. #5
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    Lets forget c is a char lets imagine its an integer and '0' is 0 and 9 is '9'.

    for example, lets say c = 1,
    in here ++ndigit[c-0(1 is the answer here)]; this is equivalent to ++ndigit[1] or ndigit[1] = ndigit + 1;

    If you enter character '9' then itll increment 9th index in ndigit[] array. Now you have 1 occurence of 9. So that code increments how many times a number has occured in the input.

    the i variable only acts as a counter.

    so if you wanna print how many occurence does 1 has. you just print the value of ndigit[1] for 9 print the value of ndigit[9].

  6. #6
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    i is simply a convenient variable used in a loop counter. K&R could have easily used loop_counter_variable instead of i, but that is kind of ugly, and besides using i, or j, or k as a loop counter variable is generally considered to be an idiom in the C language. Consider the following:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    
            int i;
            int j;
            int array1[10];
            int array2[10];
    
            /* intitialise arrays */
    
            for (i = 0; i < 10; ++i) {
                    array1[i] = 0;
            }
    
            /* Now in array1, each element is set to zero.
             * This is done with the loop above.  Using the
             * for loop has the same effect as this operation:
             * 
             * array1[0] = 0;
             * array1[1] = 0;
             * array1[2] = 0;
             * ...
             * array1[9] = 0;
             *
             * You see, when you write array1[i] = 0; 
             * the variable i is replaced by whatever
             * value is is holding at the time.  So if
             * i == 5, then writing array1[i] = 0; would
             * be the same as writing array1[5] = 0;
             */
    
            for (j = 0; j < 10; ++j) {
                    array2[j] = j;
            }
    
            /* For array2, we are not setting the individual elements
             * of the array to 0 this time, but instead, they are being
             * set to whatever the current value of the variable j.
             * So j will start at zero, and increment to 1, then 2, and so
             * on up to 9.  At each increment, the corresponding element 
             * will be set to that number:
             * 
             * If j == 5, array2[5] = 5;
             * If j == 8, array2[8] = 8;
             */
    
            for (i = 0; i < 10; ++i) {
                    printf("array1[%d] = %d\tarray2[%d] = %d\n", i, array1[i],
                           i, array2[i]);
            }
    
            return 0;
    }
    My output:

    array1[0] = 0 array2[0] = 0
    array1[1] = 0 array2[1] = 1
    array1[2] = 0 array2[2] = 2
    array1[3] = 0 array2[3] = 3
    array1[4] = 0 array2[4] = 4
    array1[5] = 0 array2[5] = 5
    array1[6] = 0 array2[6] = 6
    array1[7] = 0 array2[7] = 7
    array1[8] = 0 array2[8] = 8
    array1[9] = 0 array2[9] = 9


    ~/
    Last edited by kermit; 09-22-2005 at 04:40 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating a menu that reads input via an array?
    By Nalif in forum C Programming
    Replies: 6
    Last Post: 09-29-2006, 10:21 PM
  2. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  3. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-12-2004, 12:15 AM
  4. Creating 2D arrays on heap
    By sundeeptuteja in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2002, 12:44 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 09:31 AM

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