DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   2d arrays (http://www.daniweb.com/forums/thread97461.html)

sugarflaps Nov 19th, 2007 9:04 am
2d arrays
 
Hi im trying to print out rows and columns using ascai characters to resemble pixels. I am trying to get the user to input the dn values for the array and then use these values to then print out the appropriate character. Im really stuck now and have been at this for days any help would be great as i have to hand this in today. Heres my code below thankyou

#include <iostream>
using namespace std;

const int PIXEL_1 = 177;
const int PIXEL_2 = 176;

const int MAXROWS = 3 , MAXCOLUMNS = 3;

void initalise (int pixel_array[][MAXCOLUMNS], int size_of_first_dimension);
void print_array ();


int main ()
{   
    int num_array [MAXROWS][MAXCOLUMNS];
    cout << " Please enter the DN values for num_array [0],[0]." << endl;
    int dn_value;
    cin >> dn_value;

   

    initalise (num_array, MAXROWS);

    for (int row = 0; row < MAXROWS; row++)
    {
        for (int column = 0; column < MAXCOLUMNS; column++)
            {
                cout << num_array[row][column] << char ( PIXEL_1 );
            }
        cout << endl;
    }

    return 0;
}


void initalise (int this_array[][MAXCOLUMNS], int size_of_first_dimension)
{
    int i = char ( PIXEL_1 ), j = char ( PIXEL_2 );

    for (i = 0; i < size_of_first_dimension; i++)
        for (j = 0; j < MAXCOLUMNS; j++)
        {
            this_array[i][j] = char ( PIXEL_2 );
        }
}

niek_e Nov 19th, 2007 9:23 am
Re: 2d arrays
 
Quote:

Originally Posted by sugarflaps (Post 473443)
    int i = char ( PIXEL_1 ), j = char ( PIXEL_2 );

This is a very weird line of code.
If you're trying to create an array of chars dynamicly you should look at new and delete
Then in the next line you say:
for (i = 0; i < size_of_first_dimension; i++)
Which puts 'i' back to 0...

What should the function initalise() do exactly?

Does this even compile?

regards Niek

sugarflaps Nov 19th, 2007 9:29 am
Re: 2d arrays
 
Quote:

Originally Posted by niek_e (Post 473450)
This is a very weird line of code.
If you're trying to create an array of chars dynamicly you should look at new and delete
Then in the next line you say:
for (i = 0; i < size_of_first_dimension; i++)
Which puts 'i' back to 0...

What should the function initalise() do exactly?

Does this even compile?

regards Niek


Hi
It does compile but it doesnt do what i want it to do i want to create a 2d array using the black and grey blocks from the ascii code with each dn value being either grey or black.

niek_e Nov 19th, 2007 9:56 am
Re: 2d arrays
 
Perhaps I'm missing the point completly, but why not initialize your array like this:

char Screen[PIXEL_1][PIXEL_2];
for (int i = 0; i < PIXEL_1; i++)
{
    for (int k = 0; k < PIXEL_2; k++)
    {
        Screen[i][k] = (char)178;
    }
}
I didn't compile it, so it might not work...
To view the content of you array you might want to try something like this:
for (int i = 0; i < PIXEL_1; i++)
{
  for (int k = 0; k < PIXEL_2; k++)
  {
      cout << Screen[i][k];
  }
  cout << endl;
}
cin.get();

Again: I didn't try it, it's more of a suggestion

Now all you have to do is ask the user for input and check with an IF-statement if the current value in the loop is the dn'st value (input from user)

Regards Niek

WaltP Nov 19th, 2007 2:02 pm
Re: 2d arrays
 
Quote:

Originally Posted by sugarflaps (Post 473454)
Hi
It does compile but it doesnt do what i want it to do i want to create a 2d array using the black and grey blocks from the ascii code with each dn value being either grey or black.

Most of us aren't psychic here. You need to tell us:
1) what the program did
2) what you expected
3) where in the program we should look
Don't leave it up to us to read you code and figure out what's wrong. We need details.


All times are GMT -4. The time now is 4:20 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC