Thread: vectors in c?

  1. #1
    Unregistered
    Guest

    Unhappy vectors in c?

    okay heres my problem

    i need to write a small program using the vector data structure but i am unsure how to set up the structure itself, i can manage queues and stacks without too much difficulty, but the vector eludes my small mind

    i dont want anyone to write my program, just some hints and tips will be great

    heres what i think i need

    struct datatype
    {
    char errorMessage[27];//used to store each message
    };

    struct vectorTable
    {
    datatype data;
    };

    .
    .
    .

    int main(void)
    {
    datatype data[10];//maximum messages required
    .
    .
    .
    }

    and so on

    any help will be appreciated

    thanx

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You will have to fake using a vector in C. To do so, use a linked list. Actually, as we were discussing in another tread, you could actually kind of use a vector. Not really. According to the C99 (according to someone else on this board) you can use sized arrays. However, this will be compiler specific! If you have an older compiler, this is invalid. If you have a new one, it isn't:

    int x;
    puts("Enter the size of the vector"
    scanf("%d", &x);
    puts("Creating vector.");

    struct myStruct myVector[x];



    Will that work for you?

    Quzah.

  3. #3
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    very few compiler manufacters seem to be keen on implementing C99

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Umm... sorry guys, but could someone fill me in on what a vector is, because I'm thinking...
    Code:
    struct vector
    {
     int x;
     int y;
    }
    Sometimes the ties between math and programming are not so helpful, eh?
    Callou collei we'll code the way
    Of prime numbers and pings!

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    There is a "vector" class in C++ and an implementation in Java. Basicly, and I haven't used them myself, so I may be off, it's like a resizable array. It has built in features (members) to tell you the size, to add to it, etc.

    It's pretty much like:

    Vector myVector[20];
    for(x=0; myVector.Size();x++) myVector[x] = somenumber;

    myVector.resize( 30 );

    for(x=21;x<myVector.Size();x++) myVector[x] = somenumber;

    Kinda like that. That's a fair example I believe. Again, I haven't personally used them..actually that's not quite true, I did a passing glance/bit of code at them in C++ once..so I'm not an expert on them.

    Quzah.

  6. #6
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    do you mean two dimensional space , three dimensional space etc .

    for eg., you could represent the R^3 as

    struct point_3d
    {
    double p[3];
    };
    p[0], p[1] , p[2] representing the x,y and z axes in your favorite order,
    there is no inbuilt support for these types , in C99 there is inbuilt support for complex numbers , but I do not know much about it .
    You will have to write your own functions for manupulating them
    eg.
    Code:
    double norm_3d(const struct point_3d *pt)
    {
          double d=0.;
          int i;  
          for(i=0;i<3;i++) d += (pt->p[i])*pt->p[i];
          return sqrt(d);
    }

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The "vector" I'm talking about can be of any type (if I remember correctly). Basicly, think of it as an array which you can resize at any time. That's pretty much what a C++/Java "Vector" is.

    Quzah.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vectors
    By naseerhaider in forum C++ Programming
    Replies: 11
    Last Post: 05-09-2008, 08:21 AM
  2. How can i made vectors measuring program in DevC++
    By flame82 in forum C Programming
    Replies: 1
    Last Post: 05-07-2008, 02:05 PM
  3. How properly get data out of vectors of templates?
    By 6tr6tr in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2008, 10:35 AM
  4. How to use Vector's in C++ !?!
    By IndioDoido in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2007, 11:13 AM
  5. Points, vectors, matrices
    By subnet_rx in forum Game Programming
    Replies: 17
    Last Post: 01-11-2002, 02:29 PM