Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 05-15-2003, 09:36 AM   #1 (permalink)
zergmuncher
Registered User
 
zergmuncher's Avatar
 
Join Date: May 2003
Location: CA
Posts: 12
zergmuncher is on a distinguished road
Send a message via AIM to zergmuncher
more help?

ok, now I'm trying to manipulate the color in the program that it prints. This is my guinae pig file (I think I spelled that right), so I know all my names are inapropriate. Take a look and tell me what I'm doing wrong:

#include <conio.h>
#include <iostream.h>
#include <apvector.h>

int main()
{
apvector<apvector<int> >harry(5);
textcolor(BLUE);
for ( int i = 0 ; i < 5 ; i++ )
{
harry[i].resize(5);
for ( int k = 0 ; k < 5 ; k++ )
{
cout << harry[i][k] << " ";
}
cout << endl;
}
getch();
return 0;
}

I think the fault is when I use the cout statement, a friend of mine used something like cputs to make it print color, but I can't quite figure that out. I tried cputs(harry[i][k]) and it didn't like that, it says it wants a char *, but I think it should work anyway...maybe if I said cputs(char(harry[i][k])) it would work?

thanks guys,

oh yeah, do I have to resize jerry (the inner array of harry), or can I have it be initalized somehow? I know I'm probably driving you all insane, sorry. Thank you guys so much for your time!!
zergmuncher is offline   Reply With Quote
Old 05-15-2003, 09:39 AM   #2 (permalink)
zergmuncher
Registered User
 
zergmuncher's Avatar
 
Join Date: May 2003
Location: CA
Posts: 12
zergmuncher is on a distinguished road
Send a message via AIM to zergmuncher
and yes, I know it prints garbage values.
zergmuncher is offline   Reply With Quote
Old 05-15-2003, 09:54 AM   #3 (permalink)
zergmuncher
Registered User
 
zergmuncher's Avatar
 
Join Date: May 2003
Location: CA
Posts: 12
zergmuncher is on a distinguished road
Send a message via AIM to zergmuncher
ok ok, I know, 3 posts in a row on a thread I started. I got some help from a friend who is a 1337 h4X0r (ha ha...), and he helped me out, here's what I have.

#include <conio.h>
#include <iostream.h>
#include <apvector.h>

int main()
{
apvector<apvector<int> >harry(5);
textbackground(RED);
textcolor(BLUE);
int bob;
for ( int i = 0 ; i < 5 ; i++ )
{
harry[i].resize(5);
for ( int k = 0 ; k < 5 ; k++ )
{
bob = harry[i][k];
cprintf("%i" , bob);
cputs(" ");
}
cout << endl;
}
getch();
return 0;
}


I have a few questions: Is there any more simple way to do this? what is %i? I know, I don't need to know, but I'm curious. And where can I find a list of all the things that I can put like %i and \n and stuff like that? Thank you guys yet again.

P.S.

Did you guys hear about the prequil to that matrix? They're going to call it "The Array". And then before that, they're going to have "The Variable".
zergmuncher is offline   Reply With Quote
Old 05-16-2003, 05:15 PM   #4 (permalink)
Unicorn
Registered User
 
Unicorn's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 11
Unicorn is on a distinguished road
Quote:
I have a few questions: Is there any more simple way to do this?
Certainly.

Since your vector-of-vectors is always used with a fixed width and height, you can also use a two-dimensional array. There are several ways to do this, like in old-fashioned C (which doesn't allow you to use some of the nice tricks C++ has to offer), by computing your own offsets in a single vector (multiply y by the width and add x), or by using the multiarray container from the excellent Boost library. (http://www.boost.org/)

My guess is that the second method works best for you. I'll show you how to do this in a "simple" way (simple as in little typing work, not conceptually):

Code:
#include <vector>
#include <algorithm> // for generate ()
#include <iterator> // for ostream_iterator
#include <iostream> // for cout
#include <cstdlib> // for rand ()

using namespace std;

int main (void)
{
  const unsigned int size (5);
  
  // Pretend we have a 5 x 5 array by reserving 25 elements.
  vector<int> harry (size * size);

  // Put some random junk in there.
  generate (harry.begin(), harry.end(), rand);

  // Copy everything to cout.
  copy (harry.begin(), harry.end(),
            ostream_iterator<int> (cout, "\n"));
  
  // Show some specific values
  cout << "At (2, 3) : " << harry[3 * size + 2] << endl;
  cout << "At (1, 4) : " << harry[4 * size + 1] << endl;

  return 0;
}
Maybe you prefer writing the for-loops yourself, this code can be tricky to understand right away.

(Of course there's lots of room for improvement; that "y * size + x" belongs in a function, or better yet, a class.)

Quote:
I tried cputs(harry[i][k]) and it didn't like that, it says it wants a char *, but I think it should work anyway...
Well no, harry[i][k] is an int, and the compiler cannot know how you would like to have it converted to a char *. But this is a problem that you don't have to worry about in C++: cout will do the trick just fine. (You can also do colors I think, but not in an easy way.)

Quote:
what is %i? I know, I don't need to know, but I'm curious.
The %i is "an integer", and it means that cprintf will substitute the appropriate value before printing it. A quick Google for "printf" or "cprintf" should be enough to find out how to use these functions.

But again, this is C stuff. C++'s cout is easier to use.

Quote:
Did you guys hear about the prequil to that matrix? They're going to call it "The Array". And then before that, they're going to have "The Variable".
LOL, then I'm surprised the sequel isn't called "The Tensor".
Unicorn is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 01:05 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0 RC8 ©2007, Crawlability, Inc.





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting