Sign in to follow this  

[c++ array]

This topic is 3184 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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[i][j] = j;
cout << " array["<<i<<"]"<<"["<<j<<"]"<<" = "<<j;
}


cout<<"\n\n";

return 0;

}

Share this post


Link to post
Share on other sites
Sign in to follow this