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

  • SEARCH
  • Home
  • 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 9214343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:57:17+00:00 2026-06-18T01:57:17+00:00

I try to fullfill a vector (which i have to allocate) with random numbers

  • 0

I try to fullfill a vector (which i have to allocate) with random numbers between -0.8 and 0.8. My question is why in the main function when i call the function setvector() does not return the vector and i still take the initialized with zeros? Thanks a lot. here what i did

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

void allocate(double **a, int size) {
    *a = malloc(size);
}

double setvector(double *v){
    int i, seed, send_size;

    send_size = 10;

    allocate(&v, send_size * sizeof(double)); // allocate memory for the vector
    seed = time(NULL);
    srand(seed);
    for (i = 0; i < send_size; i++)
    {
        v[i] = 1.6*((double) rand() / (double) RAND_MAX) - 0.8;
    }
    printf("Inside function the vector is:\n\n");
    for (i = 0; i < 10; i++)
    {
        printf("The %d element has the random %4.2f\n", i, v[i]);
    }
    return *v;
}

int main(){
    double *v = NULL;
    setvector(v);
    printf("\nThe vector from main is:\n\n");
    printf("The 1st element of v is %4.2f\n", &v[0]);
    printf("The 1st element of v is %4.2f\n", &v[1]);

    return 0;
}

Here is my screen output:

Inside function the vector is:

The 0 element has the random -0.79
The 1 element has the random -0.34
The 2 element has the random 0.48
The 3 element has the random -0.67
The 4 element has the random -0.70
The 5 element has the random 0.61
The 6 element has the random -0.67
The 7 element has the random -0.66
The 8 element has the random -0.44
The 9 element has the random -0.36

The vector from main is:

The 1st element of v is 0.00
The 1st element of v is 0.00

  • 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-18T01:57:19+00:00Added an answer on June 18, 2026 at 1:57 am

    In main you’re passing the addresses of the array elements to printf:

    printf("The 1st element of v is %4.2f\n",&v[0]);
    printf("The 1st element of v is %4.2f\n",&v[1]);
    

    That should be printf(..., v[0]);.

    Further:

    double setvector(double *v){
    

    doesn’t change v in main, so v remains NULL there. You should have setvector take a double**, like allocate, and pass it the address of v.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Try this code - import java.io.StringReader; public class StringReaderTest { public static void main(String[]
Try this piece of code - public class WhitespaceTest { public static void main(String[]
Try to see which cast is faster (not necessary better): new c++ case or
I have a Question domain model designed as follows class Question { List<Choice> choiceCollection;
Try as I might I can't get my data outside the function with a
Is it possible to model random failures in Alloy? For instance, I currently have
I try to connect an app to an external service which provides me either
Try to work with Amazon Product API. Lot of code work, but have a
excuse my English as it's by far not my main one (: I have
I have to create the following functions (to a command-line casino in Matlab): function

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.