C programming: Trying to have an "atom" bounce off of a jar. Please help!

This is a discussion on C programming: Trying to have an "atom" bounce off of a jar. Please help! within the C Programming forums, part of the General Programming Boards category; <script src="http://pastebin.com/embed_js.php?i=wN0E3tjz"></script> Code: Code: #include <stdio.h> #include <stdlib.h> #include <stdbool.h> int main ( ) { typedef struct { double x ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    2

    C programming: Trying to have an "atom" bounce off of a jar. Please help!

    <script src="http://pastebin.com/embed_js.php?i=wN0E3tjz"></script>

    Code:
     
    Code:
    1. #include <stdio.h>
    2. #include <stdlib.h>
    3. #include <stdbool.h>
    4. int main()
    5. {
    6. typedef struct
    7. {
    8. double x, y, z;
    9. int protons, neutrons, electrons, speed;
    10. }
    11. atom;
    12. void move(atom someatom)
    13. {
    14. someatom.x+=1;
    15. }
    16. int main()
    17. {
    18. //initialize the atom
    19. atom helium;
    20. helium.x = 0;
    21. helium.y = 0;
    22. helium.z = 0;
    23. helium.protons = 2;
    24. helium.neutrons = 2;
    25. helium.electrons = 2;
    26. int time = 0;
    27. double speed = 1;
    28. //move in a straight line 100 units along the x axis
    29. int i;
    30. for (i = 0; i < 100; i+=speed)
    31. {
    32. time++;
    33. move(helium);
    34. printf("time = %d: (%d,%d,%d)", time, helium.x, helium.y, helium.z);
    35. }
    36. //bounce in the box
    37. double dx = 1, dy = 2, dz = 3; // these values represent the velocity vector, change these to speed up or slow down.
    38. while (true)
    39. {
    40. atom.x += dx; //move the atom
    41. atom.y += dy;
    42. atom.z += dz;
    43. if (atom.x > 100 || atom.x < 0) {
    44. atom.x -= dx; // move back to before it hit
    45. dx *= -1; // change the direction to bounce
    46. }
    47. if (atom.y > 100 || atom.y < 0) {
    48. atom.y -= dy; // move back to before it hit
    49. dy *= -1; // change the direction to bounce
    50. }
    51. if (atom.z > 100 || atom.z < 0) {
    52. atom.z = dx; // move back to before it hit
    53. dz *= -1; // change the direction to bounce
    54. }
    55. }
    56. return 0;
    57. }



    1. My Problems:

    2. line 54: atom.x += dx;
      it says: syntax error before '.' token
      line 58: if(atom.x>100|| atom.x<0)
      it says: syntax error before 'atom'

      I really appreciate the help since I am in a beginning programming class. I have NEVER coded. I'm sorry if the problems are super obvious and my code is messy, but I would love if you could make my code work for C programming. I'm using the compiler bloodshed. I'm trying to make the 'atom' bounce off the 'jar's' walls in this code. Thank you!!

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    atom is a type.
    Your variable is called helium.

    And for your move function to change the value of helium in main, you need to pass a pointer:
    Code:
    void move(atom *someatom)
    {
        someatom->x++;
    }
    
    // the call in main:
        move(&helium);
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    2
    Thank You, oogabooga!
    I have now one simple problem with the code.
    It says "syntax error at end of imput" and highlights the last bracket.

    Any help is appreciated, and I am sorry if this is not how you update a post.
    Please tell me the correct way.

    Code:
    1. #include <stdio.h>
    2. #include <stdlib.h>
    3. #include <stdbool.h>
    4. int main()
    5. {
    6. typedef struct
    7. {
    8. double x, y, z;
    9. int protons, neutrons, electrons, speed;
    10. }
    11. atom;
    12. void move(atom *someatom)
    13. {
    14. someatom->x++;
    15. }
    16. int main()
    17. {
    18. //initialize the atom
    19. atom helium;
    20. helium.x = 0;
    21. helium.y = 0;
    22. helium.z = 0;
    23. helium.protons = 2;
    24. helium.neutrons = 2;
    25. helium.electrons = 2;
    26. int time = 0;
    27. double speed = 1;
    28. //move in a straight line 100 units along the x axis
    29. int i;
    30. for (i = 0; i < 100; i+=speed)
    31. {
    32. time++;
    33. move(&helium);
    34. printf("time = %d: (%d,%d,%d)", time, helium.x, helium.y, helium.z);
    35. }
    36. //bounce in the box
    37. double dx = 1, dy = 2, dz = 3; // these values represent the velocity vector, change these to speed up or slow down.
    38. while (true)
    39. {
    40. helium.x += dx; //move the atom
    41. helium.y += dy;
    42. helium.z += dz;
    43. if (helium.x > 100 || helium.x < 0)
    44. {
    45. helium.x -= dx; // move back to before it hit
    46. dx *= -1; // change the direction to bounce
    47. }
    48. if (helium.y > 100 || helium.y < 0)
    49. {
    50. helium.y -= dy; // move back to before it hit
    51. dy *= -1; // change the direction to bounce
    52. }
    53. if (helium.z > 100 || helium.z < 0)
    54. {
    55. helium.z = dx; // move back to before it hit
    56. dz *= -1; // change the direction to bounce
    57. }
    58. }
    59. system("PAUSE")
    60. return 0;
    61. }

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    In the listing above, you need to delete lines 5 - 7.

    And you're missing a semicolon at the end of line 79 (after system("PAUSE")).
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    Why do you have two int main, nested one inside the other?

    When you paste code to the forum, please "paste as text". The board will line number it and syntax highlight it for you.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 08-19-2012, 06:15 AM
  2. Replies: 46
    Last Post: 08-24-2007, 04:52 PM
  3. ""every atom is everywhere..."
    By axon in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 03-15-2004, 08:27 AM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 07:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 06:59 AM

Tags for this Thread


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