Results 1 to 5 of 5

Thread: Print elements in array

  1. #1
    Junior Member
    Join Date
    Dec 2007
    Answers
    9

    Print elements in array

    the expected output of the following C program is to print the elements in the array. But when actually run, it doesn't do so.

    #include
    #define
    TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))

    int array[] = {23,34,12,17,204,99,16};

    int main()
    {
    int d;

    for(d=-1;d <= (TOTAL_ELEMENTS-2);d++)
    printf("%dn",array[d+1]);

    return 0;
    }

    Find out what's going wrong.


  2. #2
    Banned
    Join Date
    Oct 2007
    Answers
    173

    Re: Print elements in array

    u can also take the below example for java

    (Toggle Plain Text)
    System.out.println ( "All the values in the array are: " );
    for ( T x: array1 )
    System.out.println ( x );System.out.println ( "All the values <strong class="highlight">in</strong> the <strong class="highlight">array</strong> are: " );
    for ( T x: array1 )
    System.out.println ( x );
    Change T to whatever the type of the array is. Or, if you aren't up to the latest version of Java:

    (Toggle Plain Text)
    System.out.println ( "All the values in the array are: " );
    for ( int i = 0; i < array1.length; i++ )
    System.out.println ( array1[i] );System.out.println ( "All the values <strong class="highlight">in</strong> the <strong class="highlight">array</strong> are: " );
    for ( int i = 0; i < array1.length; i++ )
    System.out.println ( array1[i] );


  3. #3
    Junior Member
    Join Date
    Feb 2008
    Answers
    4

    Re: Print elements in array

    I think because u have used array name before declaration may be tat's why it's not working.......
    coz compiler needs to have information about the array declaration..


  4. #4
    Junior Member
    Join Date
    Mar 2008
    Answers
    4

    Re: Print elements in array

    Make this small change in the code.

    for(d=-1;d <= (int)(TOTAL_ELEMENTS-2) ;d++)

    Since Sizeof() returns unsigned int , thus it might be the problem while comparing a int with an unsigned int...


  5. #5
    Junior Member
    Join Date
    Dec 2007
    Answers
    9

    Re: Print elements in array

    Hi,

    Thats a right answer..


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact