Question

im trying to generate random numbers for a linked list and it is giving me the...

im trying to generate random numbers for a linked list and it is giving me the same number 20 times. what am i doing wrong.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Try this code:

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

typedef struct node {
int data;
struct node *next;
} node;

node *newNode(int data) {
node *new_node = (node *) malloc(sizeof(node));
new_node->data = data;

new_node->next = NULL;;
return new_node;
}

node *insert_node(node *root, int data) {
if (root == NULL)
return newNode(data);
else {
node *cur;

cur = insert_node(root->next, data);
root->next = cur;

}
return root;
}

void print(node *np) {
if (np) {
printf("(%d)", np->data);
print(np->next);
}
}

int main() {
int T = 20;
node *root = NULL;
while (T-- > 0) {
int r = rand() % 200;
root = insert_node(root, r);
}
print(root);
printf("\n");
return 0;
}

Output:

(183)(86)(177)(115)(193)(135)(186)(92)(49)(21)(162)(27)(90)(59)(163)(126)(140)(26)(172)(136)

Screenshots:

1.

2.

3.Output:

Add a comment
Know the answer?
Add Answer to:
im trying to generate random numbers for a linked list and it is giving me the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • IMPLEMENT AN IMMUTABLE SINGLY LINKED LIST IN JAVASCRIPT ------------------------------------------------------------------------------------------------------------------------- So I am trying to implement a...

    IMPLEMENT AN IMMUTABLE SINGLY LINKED LIST IN JAVASCRIPT ------------------------------------------------------------------------------------------------------------------------- So I am trying to implement a singly linked list that given the below function will pass the bottom four tests when ran. Please help me generate the singly linked list that will work and pass the tests. I will only use this as a reference so please add comments to explain what you are doing so I can understand and write this myself. I think a join method may need...

  • Use another random decimal fraction generator at Random.org, linked here, to generate a list of ten...

    Use another random decimal fraction generator at Random.org, linked here, to generate a list of ten two-digit random numbers between 10 and 30. Calculate the z-score of the median of the data set. 15, 16, 18, 19, 20, 21, 23, 27, 28, 29 What does the z-score of the data set median just above tell you about the shape of the distribution. How do you know this.

  • Write a program that can: 1. Insert twenty random numbers into a linked list. The numbers...

    Write a program that can: 1. Insert twenty random numbers into a linked list. The numbers should be within a range (E.g., 1 to 7). The user should be prompted to enter the minimum number and maximum number of the range. Each number should be inserted at the end of the list. Section 7.8 of the textbook covers the random number generator. Examples of how to use the random number generator are in Fig 7.6 and 7.7. Here is a...

  • Generate a random list NUMBERS of size 100. Sort NUMBERS using QUICKSORT until the sublist has...

    Generate a random list NUMBERS of size 100. Sort NUMBERS using QUICKSORT until the sublist has size 15 or less; then use INSERTIONSORT on the sublist. Generate 10 random lists of 100 numbers. Sort each list using QUICKSORT and then sort the same list using the combination of QUICKSORT and INSERTIONSORT as described above. Compare the times for each of the two algorithms. You can measure the duration of the time which each algorithm takes as follows: Instant first =...

  • Use another random decimal fraction generator at Random.org, linked here, to generate a list of ten...

    Use another random decimal fraction generator at Random.org, linked here, to generate a list of ten two-digit random numbers between 10 and 30. Calculate the z-score of the median of the data set. Set 1: 13, 14, 20, 21, 22, 24, 25, 26, 27, 30 What does the z-score of the data set median just above tell you about the shape of the distribution? How do you know this? If you were to take repeated random samples of n =...

  • Use the random decimal fraction generator at Random.org, linked here, to generate a list of three...

    Use the random decimal fraction generator at Random.org, linked here, to generate a list of three fractions with four decimal places. Assume those decimal fractions represent probability values associated with z-scores. Then use the standard normal table to look up the z-score that is closest to matching with that probability. List them below. (3 points) probability value (generated fraction) = associated z-score = probability value (generated fraction) = associated z-score = probability value (generated fraction) = associated z-score = The...

  • I am trying to program a dice game in Visual Studios c++ but am having trouble....

    I am trying to program a dice game in Visual Studios c++ but am having trouble. The program allows the user to choose the number of rolls, not exceeding 100,000. I am trying to print out the number of times each number(1-6) was rolled, but it's not working. What am I doing wrong? The link below is a copy of my code. https://docs.google.com/document/d/1baKfnof4pjdQ4905hwXluFtbcA1Fs7QESMQKy45xdpE/edit

  • Im trying to figure out how to do these three questions, im not positive of the...

    Im trying to figure out how to do these three questions, im not positive of the formulas and cant find them in my notes. I did come up with an answer for number 20. but im not positive if i worked thru it right or not. i converted N to lbs (8) then got an answer of 7.4 seconds, if thats right please let me know, if it isnt maybe you can help me with where i may have gone...

  • Use the random decimal fraction generator at Random.org, linked here, to generate a list of three...

    Use the random decimal fraction generator at Random.org, linked here, to generate a list of three fractions with four decimal places. Assume those decimal fractions represent probability values associated with z-scores. Then use the standard normal table to look up the z-score that is closest to matching with that probability. List them below. 0.7571 0.5064 0.6887

  • Problem 1: Implement an algorithm to generate prime numbers. You will need to implement the following...

    Problem 1: Implement an algorithm to generate prime numbers. You will need to implement the following ingredients (some of them you developed for earlier assignments): 1. A method to generate random binary numbers with n-digits (hint: for the most significant digit, you have no choice, it will be 1; similarly, for the least significant digit there is no choice, it will have to be 1; for all other position, generate 0 or 1 at random) 2. A method to compute...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT