Using arrays in program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Blackout
    New Member
    • Oct 2007
    • 3

    Using arrays in program

    Hi,
    I'm new to C and need to make a program that displays the output of the first six powers of 2 like this: 1 2 4 8 16 32
    (2 to the power of 0, 2 to the power of 1, 2 to the power of 2, and so forth)
    I made the program so far without using arrays and was wondering if i could get some help on how to use them in this program. Here is what I've done so far:

    #include <stdio.h>
    #include "math.h"

    main()
    {
    double x=2, y=0;
    double d;
    for(y=0;y<6;y++ )
    {
    d=pow(x, y);
    printf("%g\t", d);
    }
    getchar();
    }
  • ilikepython
    Recognized Expert Contributor
    • Feb 2007
    • 844

    #2
    Originally posted by Blackout
    Hi,
    I'm new to C and need to make a program that displays the output of the first six powers of 2 like this: 1 2 4 8 16 32
    (2 to the power of 0, 2 to the power of 1, 2 to the power of 2, and so forth)
    I made the program so far without using arrays and was wondering if i could get some help on how to use them in this program. Here is what I've done so far:

    #include <stdio.h>
    #include "math.h"

    main()
    {
    double x=2, y=0;
    double d;
    for(y=0;y<6;y++ )
    {
    d=pow(x, y);
    printf("%g\t", d);
    }
    getchar();
    }
    Ok do you know how to declare and fill an array?
    [code=c]
    double powersOf2[6];
    powersOf2[ind] = value;
    [/code]

    Comment

    • karthickbabu
      New Member
      • Sep 2007
      • 33

      #3
      hi

      U stored in array like these. What i done change in your code are as below:

      <Code removed as per our Posting Guidelines

      MODERATOR>



      Originally posted by Blackout
      Hi,
      I'm new to C and need to make a program that displays the output of the first six powers of 2 like this: 1 2 4 8 16 32
      (2 to the power of 0, 2 to the power of 1, 2 to the power of 2, and so forth)
      I made the program so far without using arrays and was wondering if i could get some help on how to use them in this program. Here is what I've done so far:

      #include <stdio.h>
      #include "math.h"

      main()
      {
      double x=2, y=0;
      double d;
      for(y=0;y<6;y++ )
      {
      d=pow(x, y);
      printf("%g\t", d);
      }
      getchar();
      }

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Also, be aware that integer calculations yield integer results. How did that double get in there??

        Comment

        Working...