4
Make a program in C that reads a value x, dynamically create a vector of x elements and pass this vector to a function that will read the elements of this vector. Then, in the main program, the filled vector must be printed. Also, before finishing the program, you must release the allocated memory area. It follows what I did and what is asked in the exercise but I could not fit the function part. Done in DEV C ++ version 5.11.
#include<stdio.h>
#include<stdlib.h>
int main ()
{
int i,n;
printf ("Digite o numero de elementos que deseja \n ");
scanf ("%i",&n);
int *ptr;
ptr =(int*) malloc (n * sizeof(int));
int i;
for (i=0;i<n;i++) /*queria passar essa parte do codigo para uma funçao ... essa parte do codigo faz a leitura dos valores do vetor
{
printf ("Digite os valores do vetor\n");
scanf("%d",&ptr[i]);
} */
for (i=0;i<n;i++)
{
printf ("%d",ptr[i]);
}
free(ptr);
system("pause>null");
return 0;
}
And what’s your problem?
– Maniero
i would like to pass ptr, n to the function to do the reading of elements of my vector and then on main I would like to read the values that were typed in the function
– ALFAEX
Select the whole code and click
{}
to format everything.– user28595
thank you very much diego, I’m new around here
– ALFAEX
I’ll try one more time. What’s the problem with the code?
– Maniero
I’ll leave the code running and display the part I’d like to move to the function
– ALFAEX
bigown, the problem is that I can’t correctly pass the parameters to the function and can’t do with the main read the values entered in the function , why I didn’t know states right, I have this difficulty with function .
– ALFAEX
Your code is a little fuzzy and so is the question, I suggest that if you have a statement, put it in the question, so that we can see what’s being asked and compare it to what you did to see what’s missing. I also advise you, whenever you can try to indent the code, so that it is easier to see what is happening, helps in the visual reading of the code.
– Miguel Soeiro
I’m sorry I posted the hurry and I didn’t care who would read the code, sorry I got into other topics and I saw that really difficult when the code is messy, I will improve in the next post and I will think more about the people who are trying to help
– ALFAEX