-
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
-
No that is an array. A vector is a C++ thingy.
-
-
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
-
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.
-
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...
-
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();
-
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
-
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.
-
I think I get it now, thanks