Thread: Vectors

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    53

    Vectors

    I'm (still!) learning to program in C, and I encountered "vectors". Since English is not my first language, I wasn't sure if in programming it was the same type of vector (Since there are a few differences though the answer looks obvious).
    Is this a vector?

    Code:
     
    ....
    int cont[10];
    int i,j,x;
    
    x=cont[1]
    x=cont[i]
    x=cont[i * 2 + j]
    ...
    Thanks

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    No that is an array. A vector is a C++ thingy.
    Woop?

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    53
    thanks a bunch

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    53
    Another question on (edit)arrays....
    The tutorial I'm reading shows me a program that outputs how many times a number from 0 to 99 was typed. What I don't understand is how that is stored in cont[i] or even how cont[i] works if cont[100]....

    Code:
    #include <stdio.h>
    #define MAX 100
    int main()
    {
        int i,num,cont[MAX];
        
        for (i=0;i<MAX;i++)
        cont[i]=0;
        scanf("%d", &num);
        while(num !=-1)
            {
            if ((num>=0) && (num <=MAX))
            cont[num]++;
            scanf("%d", &num);
         }
         for (i=0; i<MAX;i++)
            printf("Count [%d]= %d\n", i, cont[i]);
    }
    Thanks
    Last edited by Alastor; 10-15-2005 at 01:45 PM.

  5. #5
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Again, it's not vectors, C doesn't have such a thing. Your code above declares an array (cont) of 100 ints. This array starts at cont[0] and goes upto cont[99]. cont[100] is outside the range of the array and is illegal. Your code should check num < MAX, not num <= MAX.

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    53
    Thanks, but I still don't know how cont[i] gets information, it's value is defined as 0 and I don't see it come up again in the program...

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Change
    Code:
    for (i=0;i<MAX;i++)
        cont[i]=0;
    to something else, like
    Code:
    for(i = 0; i < MAX; i ++) cont[i] = rand();
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Oct 2005
    Posts
    53
    mmm... I still don't understand how cont[i] gets its value according to the user's input

    What I think I understand now is that cont can be used for cont[i] while i is an integer from 0-100, is that right?
    Thanks
    Alastor
    "I tend to use my inbox as a todo list. But that's like using a screwdriver to open bottles; what one really wants is a bottle opener" - Paul Graham

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Consider the following valid input range: 0 through 99. Consider an array, with valid indexes of 0 through 99. Take the value of any valid index, and start it at zero. Now, take any of the valid input variables, and use that to index a corresponding valid array index. If they input 3, use it for array index 3. Now simply increase the value stored at that index each time that value is entered.

    If the value 5 is entered ten times, and each time, you use that value as described above, when you're done, the value of array[ 5 ] will be ten. You see?


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Oct 2005
    Posts
    53
    I think I get it now, thanks
    "I tend to use my inbox as a todo list. But that's like using a screwdriver to open bottles; what one really wants is a bottle opener" - Paul Graham

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