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=d;
b=b+1;
c=c-1;
break;
}
else
a=a-1;
nArray=d;
b=b+1;
c=c-1;
}
getchar();
}

>>However the array dosent show
What does that mean? And please use code tags.

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.

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?

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

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.

You're not using any print statements with your array. Create a loop to display its elements.

how do i use a print statement for arrays?

how do i use a print statement for arrays?

something like this:

int array[10];
int i;
for(i = 0; i < 10; i++)
{
    printf("%d\n", array[i]);
}

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.

# include <stdio.h>
# include "simpio.h"
# include "genlib.h"
# include "strlib.h"
# define maxElements 500
 void displayArray(int nArray[])
{
int b;
printf("%d",nArray[b]);
}

main()
{
      int a,b,c,d,e,f,g;
      int nArray[b];
      
      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;
      }
      if(e==0)
      {
      d=1;
      a=a-1;
      nArray[b]=d;
      displayArray(nArray);
      b=b+1;
      c=c-1;
      break;
      }
else
      a=a-1;
      nArray[b]=d;
      displayArray(nArray);
      b=b+1;
      c=c-1;
      }
      getchar();
}

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.

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.

void displayArray(int nArray[])
{
int b;
printf("%d",nArray[b]);
}

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.

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.

void displayArray(int nArray[])
{
int b;
printf("%d",nArray[b]);
}

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

# include <stdio.h>
# include "simpio.h"
# include "genlib.h"
# include "strlib.h"
# define maxElements 500
 void displayArray(int nArray[])
{
int b;
printf("%d",nArray[b]);
}

int main()
{
      int a,b,c,d,e,f,g;
      int nArray[b];
      
      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;
            }
      if(e==0)
                 {
      d=1;
      a=a-1;
      nArray[b]=d;
      displayArray(nArray);
      b=b+1;
      c=c-1;
      break;
                 }
else  
      a=a-1;
      nArray[b]=d;
      displayArray(nArray);
      b=b+1;
      c=c-1;
      }
      getchar();
}

HOw come this always terminates? Plz help and write out the problem and post the code that fixes it.

# include <stdio.h>
# include "simpio.h"
# include "genlib.h"
# define SIZE 500
void displayArray(int nArray[])
{
int f;
printf("%d        ",nArray[f]);
}


main()
{
      int a,b,c,d,e,f,g,h;
      int nArray[SIZE];
      
      printf("what is the max amount of 2's you want?");
      a=GetInteger();
      b=a;
      c=a;
      e=0;
      f=0;
      h=1;
      for(d=0;d<=c;d++)
      {
      if(e==0)
      {
      d=1;
      nArray[f]=d;
      displayArray(nArray);
      f=f+1;
      }
else
      g=f;
      while(g!=0)
      {
      h=2*h;
      g=g-1;
      }
      nArray[f]=d;
      displayArray(nArray);
      }
      getchar();
}

alright i totally revamped it, but it still immidiatly terminates. Why. Help fix please

# include <stdio.h>
# include "simpio.h"
# include "genlib.h"
# define SIZE 500
void displayArray(int nArray[])
{
int arraynum;
printf("%d        ",nArray[arraynum]);
}


main()
{     
      int amount,count,num1,num2,result,arraynum;
      int nArray[arraynum];
      amount=GetInteger();
      count=amount;
      num1=count;
      num2=num1;
      result=1;
      arraynum=0;
      while(num2!=0)
      {
      while(num1!=0)
      {
      result=result*2;
      num1=num1-1;
      }
      displayArray(nArray);
      arraynum=arraynum+1;
      num2=num2-1;
      }
      getchar();
}

alright i totally revamped it, but it still immidiatly terminates. Why. Help fix please

# include <stdio.h>
# include "simpio.h"
# include "genlib.h"
# define SIZE 500
void displayArray(int nArray[])
{
int arraynum;
printf("%d        ",nArray[arraynum]);
}


main()
{     
      int amount,count,num1,num2,result,arraynum;
      int nArray[arraynum];
      amount=GetInteger();
      count=amount;
      num1=count;
      num2=num1;
      result=1;
      arraynum=0;
      while(num2!=0)
      {
      while(num1!=0)
      {
      result=result*2;
      num1=num1-1;
      }
      displayArray(nArray);
      arraynum=arraynum+1;
      num2=num2-1;
      }
      getchar();
}

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?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.