Thanks a lot, with you help I understood all my mistakes (:
This is my first time using this website so I'm not sure if it is in the right format. Basically I have to make a function that fills a vector, but it isn't working at all. English isn't my first language so this is probably really confusing, but I'd appreciate if somebody helped me. Thanks a lot.
#include <stdio.h>
#include <stdlib.h>
void le_vet(int v[1000], int n, int i)
{
for (i = 0; i < n; i++) {
printf("Type the number %d: ", i+1);
scanf("%d", &v[i]);
}
}
int main()
{
int v[1000], n;
printf("Type the syze of the vector: ");
scanf("%d", &n);
void le_vet(n);
system ("pause");
return 0;
}
You are not calling le_vet
in your main function, you are rather doing something more along the lines of creating a function pointer called "le_vet" that takes an int (by default, as no type is specified) and returns a void. I'm pretty sure this is not what's intended.
Instead, change void le_vet(n)
to le_vet(v, n)
and change this:
void le_vet(int v[1000], int n, int i)
{
for (i = 0; i < n; i++) {
printf("Type the number %d: ", i+1);
scanf("%d", &v[i]);
}
}
to this:
void le_vet(int v[], int n)
{
int i;
for (i = 0; i < n; i++) {
printf("Type the number %d: ", i+1);
scanf("%d", &v[i]);
}
}
Since you're not needing to pass i
in from outside the function, there's no need to include it in the arguments to the function. The first element in a for
loop is executed once right as the loop is entered, therefore it is often used to declare the iteration variable for the loop, as I did here.
EDIT: Whoops. Can't do that in C. I'm to used to C++ that I made a goof here. Declare i
just above the loop, as @Over Flowz suggests. Updating my revised code, but leaving this record as evidence that it's time to stop working and go eat dinner :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With