Skip to main content
gamedev.net
🔒 Locked

[c++ array]

Started by chuiy Apr 30, 2009 at 6:47 PM 4 replies 700+ views
Original Post
chuiy
chuiy
Hey guys, im not sure what the source code brakets are, its been awhile ;) so ill have to use it without them. heres the source

#include <cstdlib>
#include <iostream>

using namespace std;
int array[5][5] = {{1,1,1,1,1},
                   {1,1,1,1,1},
                   {1,1,1,1,1},
                   {1,1,1,1,1},
                   {1,1,1,1,1}};
int number1;
int number2;
int number3;
int number4;
int main(int argc, char *argv[])
{
    cout<<"enter four single digit numbers please!";
    cin>> number1;
    cin>> number2;
    cin>> number3;
    cin>> number4;
    system("PAUSE");
    return EXIT_SUCCESS;
}
NOW FIX IT haha no just kidding, but I'm trying to take numbers that you enter and put them into a spot on the array, then clal them out onto the screen, however i cant get this to work! so could put a snippet of code to help me? [edit] Added code tags - Apoch [Edited by - ApochPiQ on April 30, 2009 8:50:14 PM]
Alatar
Alatar
Instead of using number1, number2,... use cin >> array[r][c], but use the actual number of the row and column you want it to be in.

I think you could also do something like array[r][c] = number1;

Edit - I'm guessing the other two posts were accidents.

[Edited by - Alatar on April 30, 2009 7:53:57 PM]
"All you have to decide is what to do with the time that is given to you." - Gandalf
MaulingMonkey
MaulingMonkey
Quote:
Original post by Alatar
Edit - I'm guessing the other two posts were accidents.


chuiy: You can delete your extra threads by clicking the edit button on those posts and then checking the [X] Delete checkbox at the top of the form. Since they have 0 replies, this will delete the thread as a whole.
Servant of the Lord
Servant of the Lord
The source box tags are [ source ] and [ /source ].

To put a number into a spot in the array, you go like this:
array[x][y] = myNumber;

So to put the number at spot 4, 2, in the array, you'd put go like this:
array[4][2] = myNumber;

Remember, arrays start at 0, so a 5 element array holds it's values in numbers 0 - 4. Meaning, an 'array[5]' can be accessed at [0], [1], [2], [3], and [4].If you access a array with a number less than 0, or equal to or greater than the array's size, your program will crash. So with an array[5], your program will crash if you try to access [-1], or [5], or anything else that's not 0 - 4.
tnutty
tnutty
Use this to your advantage.

#include<iostream>using namespace std;int main(){	const int row = 10;	const int col = 5;	int array[row][col];	for(int i=0; i< row; i++)		for(int j = 0; j < col; j++)		 {			if(j%col == 0) cout <<endl; 			array[j] = j;			cout << " array["<<i<<"]"<<"["<<j<<"]"<<" = "<<j;		  }	cout<<"\n\n";	return 0;}
Our whole life is a opengl application.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.