More Array code problems

This is a discussion on More Array code problems within the C++ Programming forums, part of the General Programming Boards category; In this program I'm trying to use arrays to keep track of the (x,y,z) coordinates of a given number of ...

  1. #1
    Registered User
    Join Date
    Dec 2003

    More Array code problems

    In this program I'm trying to use arrays to keep track of the (x,y,z) coordinates of a given number of projectiles, starting from (0,0,0). I hope to use this as the basis for a collision system for a game. The only clear error is the x value has an incorrect value after the program executes. I think there is also problems with the looping, but I can probably fix that myself. Here's the code.

    #include <iostream>
    using std::cout;
    using std::cin;
    using std::endl;
    int a,n,x;
    int forcex, forcey, forcez;
    int main()
    	cout<<"How many projectiles are fired?:";
    	cout<<"What is the vector acceleration?"<<endl;
    	cout<<"There is force at ("<<forcex<<","<<forcey<<","<<forcez<<")"<<endl<<endl;
    	int posx[]={0}, posy[]={0}, posz[]={0};
    		posx[n] = posx[n]+forcex;
    		posy[n] = posy[n]+forcey;
    		posz[n] = posz[n]+forcez;
    			cout<<"The position of particle "<< n+1 <<" is:"<<endl;
    	return 0;
    When inputting all variables as 1, the output coordinate is: (1245120,1,1). Assuming that I'm not changing the values of the posx[] array, I dont know what is causing this error. Please help!

  2. #2
    Registered User
    Join Date
    Aug 2003
    It looks like you want something like

    int posx[MAX_PROJECTILES] = { 0 };
    int posy[MAX_PROJECTILES] = { 0 };
    int posz[MAX_PROJECTILES] = { 0 };
    Instead of the array with only one element.

    I'm also not sure what you're inner loop with indice x also needs to be fixed. Also you don't ever want variables for indices global, because it makes your program hard to understand.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem(s) with an array!!
    By Leojeen in forum C Programming
    Replies: 6
    Last Post: 05-02-2008, 08:26 PM
  2. problems finding the average of an array column
    By mrgeoff in forum C Programming
    Replies: 4
    Last Post: 04-19-2005, 12:49 AM
  3. Code: An auto expanding array (or how to use gets() safely).
    By anonytmouse in forum Windows Programming
    Replies: 0
    Last Post: 08-10-2004, 01:13 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM
  5. array problems
    By slamit93 in forum C++ Programming
    Replies: 7
    Last Post: 04-25-2002, 02:09 PM

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