2d arrays

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2007
Posts: 5
Reputation: sugarflaps is an unknown quantity at this point 
Solved Threads: 0
sugarflaps sugarflaps is offline Offline
Newbie Poster

2d arrays

 
0
  #1
Nov 19th, 2007
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

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. const int PIXEL_1 = 177;
  5. const int PIXEL_2 = 176;
  6.  
  7. const int MAXROWS = 3 , MAXCOLUMNS = 3;
  8.  
  9. void initalise (int pixel_array[][MAXCOLUMNS], int size_of_first_dimension);
  10. void print_array ();
  11.  
  12.  
  13. int main ()
  14. {
  15. int num_array [MAXROWS][MAXCOLUMNS];
  16. cout << " Please enter the DN values for num_array [0],[0]." << endl;
  17. int dn_value;
  18. cin >> dn_value;
  19.  
  20.  
  21.  
  22. initalise (num_array, MAXROWS);
  23.  
  24. for (int row = 0; row < MAXROWS; row++)
  25. {
  26. for (int column = 0; column < MAXCOLUMNS; column++)
  27. {
  28. cout << num_array[row][column] << char ( PIXEL_1 );
  29. }
  30. cout << endl;
  31. }
  32.  
  33. return 0;
  34. }
  35.  
  36.  
  37. void initalise (int this_array[][MAXCOLUMNS], int size_of_first_dimension)
  38. {
  39. int i = char ( PIXEL_1 ), j = char ( PIXEL_2 );
  40.  
  41. for (i = 0; i < size_of_first_dimension; i++)
  42. for (j = 0; j < MAXCOLUMNS; j++)
  43. {
  44. this_array[i][j] = char ( PIXEL_2 );
  45. }
  46. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,889
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 302
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: 2d arrays

 
0
  #2
Nov 19th, 2007
Originally Posted by sugarflaps View Post
  1. 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
Last edited by niek_e; Nov 19th, 2007 at 9:27 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 5
Reputation: sugarflaps is an unknown quantity at this point 
Solved Threads: 0
sugarflaps sugarflaps is offline Offline
Newbie Poster

Re: 2d arrays

 
0
  #3
Nov 19th, 2007
Originally Posted by niek_e View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,889
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 302
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: 2d arrays

 
0
  #4
Nov 19th, 2007
Perhaps I'm missing the point completly, but why not initialize your array like this:

  1. char Screen[PIXEL_1][PIXEL_2];
  2. for (int i = 0; i < PIXEL_1; i++)
  3. {
  4. for (int k = 0; k < PIXEL_2; k++)
  5. {
  6. Screen[i][k] = (char)178;
  7. }
  8. }
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:
  1. for (int i = 0; i < PIXEL_1; i++)
  2. {
  3. for (int k = 0; k < PIXEL_2; k++)
  4. {
  5. cout << Screen[i][k];
  6. }
  7. cout << endl;
  8. }
  9. 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
Last edited by niek_e; Nov 19th, 2007 at 9:59 am.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: 2d arrays

 
0
  #5
Nov 19th, 2007
Originally Posted by sugarflaps View Post
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.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC