C program for arrays

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2009
Posts: 23
Reputation: wangatang126 is an unknown quantity at this point 
Solved Threads: 0
wangatang126 wangatang126 is offline Offline
Newbie Poster

C program for arrays

 
0
  #1
Oct 27th, 2009
hi im trying to write a C program that produces an array like this
1,2,4,8,16...
Basically i produce an array with with the power of 2 starting at 0 and going up to n. Here is my current code: However the array dosent show. What is the problem?
# include <stdio.h>
# include "simpio.h"
# include "genlib.h"
# include "strlib.h"
# define maxElements 500


main()
{
int a,b,c,d,e,f,g;
int nArray[maxElements];

printf("what is the max amount of 2's you want?");
a=GetInteger();
b=0;
c=a;
d=2;
while(c!=0)
{
e=a;
while(e!=0)
{
d=d*2;
e=e-1;
printf("yo");
}
if(e==0)
{
d=1;
a=a-1;
nArray[b]=d;
b=b+1;
c=c-1;
break;
}
else
a=a-1;
nArray[b]=d;
b=b+1;
c=c-1;
}
getchar();
}
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,469
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1477
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning
 
-7
  #2
Oct 27th, 2009
>>However the array dosent show
What does that mean? And please use code tags.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 34
Reputation: dan63043 is an unknown quantity at this point 
Solved Threads: 4
dan63043 dan63043 is offline Offline
Light Poster
 
0
  #3
Oct 27th, 2009
I just can't bear the thought of deciphering what your variables represent. You need meaningful variable names.

Also, indent your control structures so that you know what is nested. (You may just need to use the [code] brackets.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 18
Reputation: Chilton is an unknown quantity at this point 
Solved Threads: 2
Chilton Chilton is offline Offline
Newbie Poster
 
0
  #4
Oct 28th, 2009
Also, your code is overly complicated. Since you know the stopping value "maxElements", why not loop around that number of times while calling the pow function?
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 23
Reputation: wangatang126 is an unknown quantity at this point 
Solved Threads: 0
wangatang126 wangatang126 is offline Offline
Newbie Poster
 
0
  #5
Oct 28th, 2009
how do i use code tags? And what i mean by it dosnt display is that the array dosent appear when i run the program
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 461
Reputation: Grn Xtrm will become famous soon enough Grn Xtrm will become famous soon enough 
Solved Threads: 38
Grn Xtrm's Avatar
Grn Xtrm Grn Xtrm is offline Offline
Posting Pro in Training
 
0
  #6
Oct 28th, 2009
The array isnt being output because you dont include that line in your program. You have to print the array when your done filling it.
To use code tags click the code button above the box in which you type your reply.
Last edited by Grn Xtrm; Oct 28th, 2009 at 6:35 pm.
Check out my new band URL on facebook. I'm the bass player. :) Become a fan and leave comments if you like.
URL on facebook!
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 18
Reputation: Chilton is an unknown quantity at this point 
Solved Threads: 2
Chilton Chilton is offline Offline
Newbie Poster
 
0
  #7
Oct 28th, 2009
You're not using any print statements with your array. Create a loop to display its elements.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 23
Reputation: wangatang126 is an unknown quantity at this point 
Solved Threads: 0
wangatang126 wangatang126 is offline Offline
Newbie Poster
 
0
  #8
Oct 28th, 2009
how do i use a print statement for arrays?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,469
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1477
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning
 
-7
  #9
Oct 28th, 2009
Originally Posted by wangatang126 View Post
how do i use a print statement for arrays?
something like this:
  1. int array[10];
  2. int i;
  3. for(i = 0; i < 10; i++)
  4. {
  5. printf("%d\n", array[i]);
  6. }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 23
Reputation: wangatang126 is an unknown quantity at this point 
Solved Threads: 0
wangatang126 wangatang126 is offline Offline
Newbie Poster
 
0
  #10
Oct 29th, 2009
K here is what i've got. However the program immediatley termanates. Why? Also if you could take my program and edit it it would be heavily appreciated.
  1. # include <stdio.h>
  2. # include "simpio.h"
  3. # include "genlib.h"
  4. # include "strlib.h"
  5. # define maxElements 500
  6. void displayArray(int nArray[])
  7. {
  8. int b;
  9. printf("%d",nArray[b]);
  10. }
  11.  
  12. main()
  13. {
  14. int a,b,c,d,e,f,g;
  15. int nArray[b];
  16.  
  17. printf("what is the max amount of 2's you want?");
  18. a=GetInteger();
  19. b=0;
  20. c=a;
  21. d=2;
  22. while(c!=0)
  23. {
  24. e=a;
  25. while(e!=0)
  26. {
  27. d=d*2;
  28. e=e-1;
  29. }
  30. if(e==0)
  31. {
  32. d=1;
  33. a=a-1;
  34. nArray[b]=d;
  35. displayArray(nArray);
  36. b=b+1;
  37. c=c-1;
  38. break;
  39. }
  40. else
  41. a=a-1;
  42. nArray[b]=d;
  43. displayArray(nArray);
  44. b=b+1;
  45. c=c-1;
  46. }
  47. getchar();
  48. }
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC