Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8714001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:29:02+00:00 2026-06-13T05:29:02+00:00

I am tryin to take input from the user and store it into an

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T05:29:03+00:00Added an answer on June 13, 2026 at 5:29 am

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to take user input directly from console using getline into a
I was trying to make a code that will take input from a user
I’m trying to make something that will take lines of input from the user,
I am trying to take input from the user and want to compile regex
I am trying to take a single input from the user and delete some
I have a data in form a dictionary.. NOw I take the input from
So I'm trying to take an input from a user using Scanner(System.in) but when
Hey I'm trying to take a String input from the user (in this case
I'm trying to take user form input and display it back to the user,
I am trying to using the JS to take user input and modify certain

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.