Geeks Talk

Prepare for your Next Interview




Print elements in array

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) / ...


Go Back   Geeks Talk > Software Development > C and C++

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 02-16-2008
Junior Member
 
Join Date: Dec 2007
Location: India
Posts: 12
Thanks: 2
Thanked 7 Times in 3 Posts
swetha_joein is on a distinguished road
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.
Reply With Quote
The Following User Says Thank You to swetha_joein For This Useful Post:
Sponsored Links
  #2 (permalink)  
Old 02-21-2008
Banned
 
Join Date: Oct 2007
Location: Hyderabad
Posts: 176
Thanks: 2
Thanked 11 Times in 11 Posts
quality_quantity is on a distinguished road
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] );
Reply With Quote
  #3 (permalink)  
Old 02-26-2008
Junior Member
 
Join Date: Feb 2008
Location: india
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts
crazyaboutc is on a distinguished road
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..
Reply With Quote
  #4 (permalink)  
Old 03-12-2008
Junior Member
 
Join Date: Mar 2008
Location: India
Posts: 4
Thanks: 0
Thanked 1 Time in 1 Post
arvinsup is on a distinguished road
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...
Reply With Quote
The Following User Says Thank You to arvinsup For This Useful Post:
  #5 (permalink)  
Old 05-21-2008
Junior Member
 
Join Date: Dec 2007
Location: India
Posts: 12
Thanks: 2
Thanked 7 Times in 3 Posts
swetha_joein is on a distinguished road
Re: Print elements in array

Hi,

Thats a right answer..
Reply With Quote
Reply

  Geeks Talk > Software Development > C and C++


Thread Tools
Display Modes


Similar Threads

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


All times are GMT -4. The time now is 12:23 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Copyright © 2008 GeekInterview.com. All Rights Reserved