C program for arrays

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

Join Date: Aug 2005
Posts: 15,684
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: 1505
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning
 
-7
  #11
Oct 29th, 2009
line 12: its int main().

The rest is just too difficult to read because of terrible formatting. Don't be afraid to use some spaces to indent lines.
Last edited by Ancient Dragon; Oct 29th, 2009 at 8:52 pm.
I told Santa what I wanted for Christmas and he washed my mouth out with soap.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 65
Reputation: neithan is an unknown quantity at this point 
Solved Threads: 2
neithan's Avatar
neithan neithan is offline Offline
Junior Poster in Training
 
0
  #12
Oct 29th, 2009
Originally Posted by wangatang126 View Post
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.
Your code looks kind of crazy. I don't understand any of this, why would you need 6 variables just to create a simple array with that serie? I don't get it.

  1. void displayArray(int nArray[])
  2. {
  3. int b;
  4. printf("%d",nArray[b]);
  5. }

Why would you want b for? And by the way it isn't initialized so you'll print random memory stuff there.

Anyways I strongly recommend you to throw all that code away, even if it's working, and write us a pseudocode of how you want your program to run, and we will work by there. If you don't know what pseudocode is try google it. Now is the best point to start for you in my opinion.
Seriously.
Last edited by neithan; Oct 29th, 2009 at 9:04 pm.
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
  #13
Oct 29th, 2009
Originally Posted by neithan View Post
Your code looks kind of crazy. I don't understand any of this, why would you need 6 variables just to create a simple array with that serie? I don't get it.

  1. void displayArray(int nArray[])
  2. {
  3. int b;
  4. printf("%d",nArray[b]);
  5. }

Why would you want b for? And by the way it isn't initialized so you'll print random memory stuff there.

Anyways I strongly recommend you to throw all that code away, even if it's working, and write us a pseudocode of how you want your program to run, and we will work by there. If you don't know what pseudocode is try google it. Now is the best point to start for you in my opinion.
Seriously.
HOw would you initialize b, since if you initialize it in the function then it will constantly keep b at the same number. However if i initialize it in the program it will still keep b at the same number.
here i indented the program. Here is it reposted
  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. int 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  
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
  #14
Oct 29th, 2009
HOw come this always terminates? Plz help and write out the problem and post the code that fixes it.
  1. # include <stdio.h>
  2. # include "simpio.h"
  3. # include "genlib.h"
  4. # define SIZE 500
  5. void displayArray(int nArray[])
  6. {
  7. int f;
  8. printf("%d ",nArray[f]);
  9. }
  10.  
  11.  
  12. main()
  13. {
  14. int a,b,c,d,e,f,g,h;
  15. int nArray[SIZE];
  16.  
  17. printf("what is the max amount of 2's you want?");
  18. a=GetInteger();
  19. b=a;
  20. c=a;
  21. e=0;
  22. f=0;
  23. h=1;
  24. for(d=0;d<=c;d++)
  25. {
  26. if(e==0)
  27. {
  28. d=1;
  29. nArray[f]=d;
  30. displayArray(nArray);
  31. f=f+1;
  32. }
  33. else
  34. g=f;
  35. while(g!=0)
  36. {
  37. h=2*h;
  38. g=g-1;
  39. }
  40. nArray[f]=d;
  41. displayArray(nArray);
  42. }
  43. getchar();
  44. }
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
  #15
Oct 29th, 2009
alright i totally revamped it, but it still immidiatly terminates. Why. Help fix please
  1. # include <stdio.h>
  2. # include "simpio.h"
  3. # include "genlib.h"
  4. # define SIZE 500
  5. void displayArray(int nArray[])
  6. {
  7. int arraynum;
  8. printf("%d ",nArray[arraynum]);
  9. }
  10.  
  11.  
  12. main()
  13. {
  14. int amount,count,num1,num2,result,arraynum;
  15. int nArray[arraynum];
  16. amount=GetInteger();
  17. count=amount;
  18. num1=count;
  19. num2=num1;
  20. result=1;
  21. arraynum=0;
  22. while(num2!=0)
  23. {
  24. while(num1!=0)
  25. {
  26. result=result*2;
  27. num1=num1-1;
  28. }
  29. displayArray(nArray);
  30. arraynum=arraynum+1;
  31. num2=num2-1;
  32. }
  33. getchar();
  34. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 65
Reputation: neithan is an unknown quantity at this point 
Solved Threads: 2
neithan's Avatar
neithan neithan is offline Offline
Junior Poster in Training
 
0
  #16
Oct 30th, 2009
Originally Posted by wangatang126 View Post
alright i totally revamped it, but it still immidiatly terminates. Why. Help fix please
  1. # include <stdio.h>
  2. # include "simpio.h"
  3. # include "genlib.h"
  4. # define SIZE 500
  5. void displayArray(int nArray[])
  6. {
  7. int arraynum;
  8. printf("%d ",nArray[arraynum]);
  9. }
  10.  
  11.  
  12. main()
  13. {
  14. int amount,count,num1,num2,result,arraynum;
  15. int nArray[arraynum];
  16. amount=GetInteger();
  17. count=amount;
  18. num1=count;
  19. num2=num1;
  20. result=1;
  21. arraynum=0;
  22. while(num2!=0)
  23. {
  24. while(num1!=0)
  25. {
  26. result=result*2;
  27. num1=num1-1;
  28. }
  29. displayArray(nArray);
  30. arraynum=arraynum+1;
  31. num2=num2-1;
  32. }
  33. getchar();
  34. }

Ok, you didn't pay attention to my first post trying to help you, so just answer this one.

Do you want us to fix your code or do you want to learn how to fix it by yourself?
Last edited by neithan; Oct 30th, 2009 at 6:49 am.
Reply With Quote Quick reply to this message  
Reply

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




Views: 573 | Replies: 15
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC