Prepare for your Next Interview
This is a discussion on Print elements in array within the C and C++ forums, part of the Software Development category; 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) / ...
|
|||
|
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. |
| The Following User Says Thank You to swetha_joein For This Useful Post: | ||
| Sponsored Links |
|
|||
|
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] ); |
|
|||
|
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.. |
|
|||
|
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... |
| The Following User Says Thank You to arvinsup For This Useful Post: | ||
![]() |
|
| Thread Tools | |
| Display Modes | |
|
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to print an array in Richtext box | prakash.kudrekar | VB.NET | 3 | 12-26-2007 04:57 AM |
| static array or dynamic array? | rpgubba | C and C++ | 6 | 12-22-2007 03:03 AM |
| Customize and print your photos with pre-designed print templates. | JobHelper | Geeks Lounge | 0 | 12-12-2007 05:40 AM |
| An array of 100 elements consists of only 0's and 1's | Geek_Guest | C and C++ | 1 | 06-26-2007 03:18 PM |
| Retrive some elements from a mutiple array | Geek_Guest | MainFrame | 0 | 06-16-2007 09:17 AM |