hi guys

i was doing a exercise about invert an integer,but i have to do this using vectors .

example 321 will be 123

I did the program, it worked, but when I put a number with 3 digits or more, the last digit does not appear.

Someone can help me ?I'm not understanding the problem

thank u

my code:

#include <stdio.h>

#include <stdlib.h>

int main() {

int n,cifra,i;

int *v;

v =(int*) malloc (n*sizeof(int));

printf("put a number:");

scanf("%i",&n);

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

cifra=n%10;

n=n/10;

v[i]=cifra;

printf("%i",v[i]);}

return 0;}

i know how to do using loop ,but i need to do using vector

i was doing a exercise about invert an integer,but i have to do this using vectors .

example 321 will be 123

I did the program, it worked, but when I put a number with 3 digits or more, the last digit does not appear.

Someone can help me ?I'm not understanding the problem

thank u

my code:

#include <stdio.h>

#include <stdlib.h>

int main() {

int n,cifra,i;

int *v;

v =(int*) malloc (n*sizeof(int));

printf("put a number:");

scanf("%i",&n);

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

cifra=n%10;

n=n/10;

v[i]=cifra;

printf("%i",v[i]);}

return 0;}

i know how to do using loop ,but i need to do using vector

Last edited on

if I understand correctly you want to take the number and place each digit into a cell of a vector?

so if you have the number 1234

you want the vector to look like this?

vector[0] = 4,

vector[1] = 3,

vector[2] = 2,

vector[3] = 1

so if you have the number 1234

you want the vector to look like this?

vector[0] = 4,

vector[1] = 3,

vector[2] = 2,

vector[3] = 1

ok I assume this is meant to remain in C and that you don't want a C++ vector but an array (mathmatical vector of sorts terminology?).

lets look at the code...

int n,cifra,i;

int *v;

v =(int*) malloc (n*sizeof(int)); //what is n right here??? This is a serious bug.

lets look at the code...

int n,cifra,i;

int *v;

v =(int*) malloc (n*sizeof(int)); //what is n right here??? This is a serious bug.

Topic archived. No new replies allowed.