943,822 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2521
  • C++ RSS
Nov 19th, 2007
0

2d arrays

Expand Post »
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

C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sugarflaps is offline Offline
5 posts
since Oct 2007
Nov 19th, 2007
0

Re: 2d arrays

Click to Expand / Collapse  Quote originally posted by sugarflaps ...
C++ Syntax (Toggle Plain Text)
  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?
Last edited by Nick Evan; Mar 26th, 2010 at 9:16 am.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Nov 19th, 2007
0

Re: 2d arrays

Quote ...
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?

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.
Last edited by Nick Evan; May 11th, 2010 at 9:08 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sugarflaps is offline Offline
5 posts
since Oct 2007
Nov 19th, 2007
0

Re: 2d arrays

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)
Last edited by Nick Evan; Mar 26th, 2010 at 9:16 am.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Nov 19th, 2007
0

Re: 2d arrays

Click to Expand / Collapse  Quote originally posted by sugarflaps ...
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.
Moderator
Reputation Points: 3278
Solved Threads: 892
Posting Sage
WaltP is offline Offline
7,718 posts
since May 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Receiving user input to direct program
Next Thread in C++ Forum Timeline: Help with finding a good use for a hash table in my program....





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC