Vector exercise and function

Thank you very much, they helped me, I understood all my mistakes (:

This is my first time using this site, so I'm not sure if it is in the correct format. Basically, I have to create a function that fills the vector, but it doesn't work at all. English is not my first language, so this is probably really confusing, but I would appreciate it if someone helped me. Thank you very much.

#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; } 
+4
source share
4 answers

You do not call le_vet in your main function, you rather do something more by creating a function pointer called "le_vet", which takes an int (by default, because the type is not specified) and returns invalid. I am pretty sure that this is not what is needed.

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]); } } 

:

 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 do not need to pass i from outside the function, there is no need to include it in the arguments of the function. The first element in the for loop runs once right when the loop is introduced, so it is often used to declare an iteration variable for the loop, as I did here.

EDIT: Oops. Can't do it in C. I have to use for C ++ what I did goof here. Declare i just above the loop, as @Over Flowz suggests. Updating my revised code, but leaving this entry as evidence that it is time to stop working and go eat lunch :)

+6
source

You only pass one argument to le_vet() if three arguments are required. You also need to remove void , since you are calling the function.

Maybe it will work.

 void le_vet(int n) { static int v[1000]; for (int i = 0; i < n; i++) { printf("Type the number %d: ", i+1); scanf("%d", &v[i]); } } 

You do not need the int i passed as a parameter, since you are creating another in a for loop.

 int i = 0; while (i < n) { i++; } 

coincides with

for (int i = 0; i < n; i++)

+3
source

When called:

 ... scanf("%d", &n); void le_vet(n); //you are declaring a function. You need to remove the void keyword system ("pause"); ... 

You should call the following:

 ... scanf("%d", &n); le_vet(n); system ("pause"); ... 

Then you will see real errors, such as the number of parameters

+2
source

to try:

 #include <stdio.h> #include <stdlib.h> void le_vet(char v[], int n) { int i = 0; for(i = 0; i < n; i++) { printf("Type the number %d: ", i+1); scanf("%s", &v[i]); //Read string, not decimal for output. } } int main() { char v[1000] = {0}, n; printf("Type size of the vector: "); scanf("%d", &n); le_vet(v, n); printf("%s", v); system("pause"); return 0; } 

Hope this helps.

+2
source

All Articles