putting values into an array

This is a discussion on putting values into an array within the C Programming forums, part of the General Programming Boards category; Hello, I feel like this shouldn't be that hard, but I haven't been able to do it and I'm starting ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    30

    putting values into an array

    Hello,

    I feel like this shouldn't be that hard, but I haven't been able to do it and I'm starting to go a bit crazy. I have a function that runs in a loop and gives 10 values. I want to put the values into an array. Everything's fine if I manually type all the values in, but I want to have the results automatically go into the array. Especially since this is going to lead to a much longer loop after I figure it out.

    I was just trying to do it with a one dimensional array first, since I've been having so much trouble, but what I'm getting at is being able to have the function evaluated at a bunch of x's, paired up with the corresponding x in the array. (The loop is changing x and giving the values of the function at each x.) Then I want to access certain "f(x)'s".

    I don't know if all that is relevant to the question, but what I really need is how to put the values from the loop I'm running directly into an array, and I'll take it again from there (if I can :S).
    Thanks a lot to anyone who can help get me on the right track.

    (I don't have a lot of experience. And I've also looked a lot online, but I think I might no be looking at the correct things or something, due to the aforementioned lack of experience... So any help is appreciated. Thanks again.)

  2. #2
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Post the code that you've got already and then we can tell you what you need to work on.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    30
    I don't really have code past making the function into a loop. I could post that, but I didn't think it would be that helpful since it's just a loop and it works fine. Do you want me to? But I don't really have a specific code question. It was more of a general question...I need help starting what to do next, then when/if I have a specific question I can post in what I've tried, but I think if someone tells me what to use I'll be all right.

  4. #4
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Post what you have, a loop's already a good start and you said you're reading the values in fine, so that will be a good starting point.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    30
    All right, here's the loop.

    Code:
    #include <stdio.h>
    #include <math.h>
    double alpha( double x, double y)
    {
      double m0;
      double t1, t2;
      double answer;
      m0=x*x;
      t1=4*m0;
      t2=x+m0;
      answer=m0+t1;
      answer+=t2;
      
      return answer;
    }
    
    main()
    {
          double x;
          double y;
          double alpha_bru;
          y=2;
          for (x=1; x<=10; x++){
          alpha_bru=alpha(x,y);
          printf("&#37;e %e\n",x, alpha_bru);
          }
          system("PAUSE");
          }
    I know it's more complicated than it needs to be, but that's because it's the model for the more complicated function (that's why I still have the variables as doubles and things).
    Thanks!
    Last edited by zdream8; 05-20-2008 at 10:10 AM.

  6. #6
    Registered User
    Join Date
    May 2008
    Posts
    30
    Hi, I actually just need help getting it into a two dimensional array that looks like the output from the code I posted earlier. I could actually get just the function values into a one-dimensional array. This is what I did.

    Code:
    main ()
    {
         int i;      
         double x;
         double y;
         double alpha_bru;
         y=2;
         i=-1;
         for (x=1; x<=10; x++){
             i++;
             alpha_bru=alpha(x,y);
             array_todd[i]=alpha_bru;
             }
             for (i=0; i<10; i++) {
             printf("%e\n", array_todd[i]);
             }
             system("PAUSE");
    }
    The first part of the code is the same as before.

  7. #7
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    You gotta declare alpha_todd like this:
    Code:
    double alpha_todd[10];
    Oh, and the for loop should go from x=0 to x=9.

    Oh, and system("pause") is not portable. Use getchar(); instead.

    edit: Oh, post #251 whoooooohoooooooooo!

  8. #8
    Registered User
    Join Date
    May 2008
    Posts
    30
    Actually I wasn't having trouble with this code. My question was on how to have a two dimensional array, instead. This works for one dimensional.
    x goes from 1 to 10 because those are the numbers I want the function to evaluate for x, not 0 to 9. But if you look, i goes from 0 to 9, which is for the array.
    (And I use system("PAUSE") for some quirky reasons, but it's easy to change if I need.)
    Thanks.

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    My question was on how to have a two dimensional array, instead.
    You declare a 2D array by adding one dimension:
    double array[xmax][ymax]

    and you access it by using 2 indexes as expected:
    array[x][y]=somevalue;

    Are you simply bothered by the syntax or is it about the algorithm itself?

  10. #10
    Registered User
    Join Date
    May 2008
    Posts
    30
    All right, so continuing with what I was doing I would have
    array_todd[i][j]; instead...and I want the x's to be in the first column, with the function values in the second.
    So I want to say as x goes through the loop, the x value gets assigned to the first column, first column and the function value gets assigned to the second row, first column, proceeding down the columns. It was easy for one dimension because I just had i increase by one each time and wrote array_todd[i]=alpha_bru. But I didn't know what to write for the two dimensional because it's not as simple as saying array_todd[i][j]=[x][alpha_bru].
    I'm sure it's not that hard, I'm probably just stuck on something with notation.
    Thanks for your response.

  11. #11
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    ...and I want the x's to be in the first column, with the function values in the second.
    Then you need the array with 2 columns and 10 rows
    Code:
    array_todd[2][10];
    ...and inside the loop you do something like

    Code:
    for(x=1;x<=10;x++)
    {
         alpha_bru=alpha(x,y);
         array_todd[0][x-1]=x;
         array_todd[1][x-1]=alpha_bru;
    }

  12. #12
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    The loop is there to go through the array, therefore IMO x should relate to an index (ie 0 to 9) NOT 1 to 10 (n - 1)
    Code:
    for(x = 0; x < 10; x++)
    {
        alpha_bru = alpha(x + 1, y);
        array_todd[0][x] = x + 1;
        array_todd[1][x] = alpha_bru;
    }
    Makes more sense to me.

  13. #13
    Registered User
    Join Date
    May 2008
    Posts
    30
    My x is actually a double, not an integer, so it can't be an index in the array. I've been putting an integer i in the loop, which increases by one each time, for the index, but it's a little more confusing with two dimensions.

    I tried the suggestion with i instead of x, but I still couldn't get it to work (at least right away). I'm going to play with it a bit more, though. I'll be back if I still need help.

    And also, how should I have it print out the array when I'm done?
    Right now I have
    Code:
             for (i=0; i<10; i++) {
             printf("%e\n", array_todd[i]);
             }
    but how do I relate the other dimension into the loop? (Especially since the "j" would only loop from 0 to 1 if it was by itself.)
    I guess I could say something like printf("%e %e\n", array_todd[0][i]), array_todd[1][i];
    but that would get inconvenient if "j" were a lot bigger than two...

  14. #14
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    but that would get inconvenient if "j" were a lot bigger than two...
    Then you make nested for loops.

  15. #15
    Registered User
    Join Date
    May 2008
    Posts
    30
    I guess that's what was confusing me, because i goes from 0 to 9, j goes from 0 to1. I tried to do it with something similar and it was spitting out way too many of "j's" I think. I don't remember exactly what I was doing, I don't think I saved the code since I was just messing around and didn't know what I was doing.
    Is it just as simple as having
    Code:
    for...<the j loop>{
             for...<the i loop>{
    printf ... array[i][j];
    } }
    ? (obviously with correct notation)

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Converting Char Array Loop values to integer
    By azamsharp1 in forum C Programming
    Replies: 8
    Last Post: 10-27-2005, 09:13 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. 3-d array assign string values
    By WaterNut in forum C++ Programming
    Replies: 8
    Last Post: 07-01-2004, 12:02 AM
  5. Duplicate values in Array
    By TONYMX3 in forum C++ Programming
    Replies: 2
    Last Post: 01-30-2002, 02:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21