1

I am tryin to take input from the user and store it into an array and print it out: i have 2 functions:

 /* Read a vector with n elements: allocate space, read elements,
    return pointer */
double *read_vector(int n){
    double *vec = malloc(n * sizeof(double));

    int i;
    for (i = 0; i < n; i++)
       vec[i] = n;

    return vec;
}

and the print function is:

void print_vector(int n, double *vec){
    int i;
    for (i = 0; i < n; i++) {
       printf("%d\n", vec[i]);
    }
}

the main function is:

#include <stdio.h>
#include <stdlib.h>

double *read_vector(int n);
void print_vector(int n, double *vec);
void free_vector(double *vec);

int main(){
    int n;
    double *vector;
    /* Vector */
    printf("Vector\n");
    printf("Enter number of entries: ");
    scanf("%d", &n);
    printf("Enter %d reals: ", n);
    vector = read_vector(n);
    printf("Your Vector\n");
    print_vector(n,vector);
    free_vector(vector);
}

when i run this, it does not let me enter any numbers, it just skips it and prints out 0's. How do i fix this?

1
  • "it does not let me enter any numbers" So you do not get a chance to enter in n between printf("Enter number of entries: "); and printf("Enter %d reals: ", n);? Commented Jan 22, 2015 at 16:01

3 Answers 3

3

Try the code below. You're almost certainly either not compiling with warnings, or ignoring the warnings. All warnings mean something, and to a beginner they all matter. With gcc use the -Wall option, or even -pedantic.

As halfelf pointed out, you need a scanf in your read loop but it needs to be a pointer (&vec[i]). Always return something at the end of main. Also check the return value of malloc, it could fail and return a null pointer.

#include <stdio.h>
#include <stdlib.h>

double *read_vector(int n)
{
    double *vec = malloc(n * sizeof(double));
    int i;

    for (i = 0; i < n; i++) {
        printf("Enter number %i of %i: ", i + 1, n);
        scanf("%lf", &vec[i]);
    }
    return vec;
}

void print_vector(int n, double *vec)
{
    int i;

    for (i = 0; i < n; i++) {
        printf("%f\n", vec[i]);
    }
}

void free_vector(double *vec)
{
    free(vec);
}


int main()
{
    int n;
    double *vector;

    printf("Vector\n");
    printf("Enter number of entries: ");
    scanf("%i", &n);
    vector = read_vector(n);
    printf("Your Vector\n");
    print_vector(n, vector);
    free_vector(vector);
    return 0;
}
Sign up to request clarification or add additional context in comments.

2 Comments

hey CaptainMurphy, thanks very much. I just have a follow up to print id's: how do i put out the numbers like this: say i enter: 1.5 , it will display like: 1.50e+00
%e or %g will work. Take a look at the printf format specifiers: cplusplus.com/reference/clibrary/cstdio/printf
1

In the read_vector(int n) function's for loop:

for (i=0; i<n; i++) 
   vec[i] = n;       // this should be scanf("%lf",vec+i) to read input from stdin

and notice your { and } there. If there's only one line in the loop, { and } is not necessary, OR you have to use a pair of them. The return clause must be out of the loop.

Btw, add return 0 at the end of your main function.

2 Comments

I just edited the changes you said in, but it gave an segmentation fault.
the read_vector function is now : double *read_vector(int n){ double *vec = malloc(n * sizeof(double)); int i; for (i = 0; i < n; i++) scanf("%lf",vec[i]) return vec; }
0

simple..you forgot to write scanf..!

double *read_vector(int n)
{

    double *vec = malloc(n * sizeof(double));

    int i;
    for (i = 0; i < n; i++)
       scanf("%d",&vec[i]);

    return vec;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.