-
Junior Member
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.
-
Banned
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] );
-
Junior Member
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..
-
Junior Member
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...
-
Junior Member
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
-
Forum Rules