Question

In: Computer Science

Create a program that generates random number between 0 and 50 and puts them into a...

Create a program that generates random number between 0 and 50 and puts them into a linked list. When it generates 49, instead of adding it to the list, it prints the list, freeing each node and then exits. Submit your .c file

Solutions

Expert Solution

Code

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

void sorted_ins(struct Node **,struct Node *);
struct Node
{
    int value;
    struct Node *next;
};

struct Node *head=NULL;

void add(struct Node **head,int v)
{
    struct Node *node = (struct Node*)malloc(sizeof(struct Node)); 
    node->value=v;
    struct Node *temp=*head;
    if(*head==NULL) //if head is null create head
    {
        *head=node;
        return;
    }
    while(temp->next!=NULL) //loop till the end,add element
    {
        temp=temp->next;
    }
    temp->next=node;
}





void print(struct Node** head)
{
    struct Node *temp=*head;
    while(temp!=NULL)
    {
        printf("%d ",temp->value);
        temp=temp->next;
    }
}

void freelist(struct Node** head)
{
    struct Node* current=*head;
    struct Node* next;
    while(current!=NULL)
    {
        next=current->next;
        free(current); //free the element
        current=next;
    }
    *head=NULL; //assign head to null
}
int main ()
{
    int i, n; time_t t;
    struct Node *s=NULL;
    int d;
/* Intializes random number generator */
    srand((unsigned)time(&t)); /* Print 5 random numbers from 0 to 49 */
    while(1)
    {
        d=rand()%50;
        if(d==49) //if random value is 49 print the list and exit
        {
            print(&s);
            freelist(&s);
            break;
        }
        add(&s,d); //add element to the list if value is 49
    }
    return(0);
}

Terminal Work

.


Related Solutions

Here is my assignment: Write a program that generates a random number between 1 and 50...
Here is my assignment: Write a program that generates a random number between 1 and 50 and asks the user to guess it. As a hint, it tells the user how many divisors it has, (excluding 1 and the number itself). Each time the user makes a wrong guess, it displays "Wrong" and says if the guess was too low or too high, until the user gets it right, in which case it says "Right" and says how many trials...
Write a Python program which generates a random number between 0 and 24 inclusive and that...
Write a Python program which generates a random number between 0 and 24 inclusive and that print out: • the double of the random number if the random number is greater than 12, • the triple of the random number if it is equal to 12 • and the quadruple of the random number if it is less than 12
Create a program that generates a file of random numbers, and then prints them in neat...
Create a program that generates a file of random numbers, and then prints them in neat fashion to another file, and also saves to that file the average and standard deviation of those numbers. I) First, you would need to generate a file of random numbers that consists of N random numbers (100 < N < 1000). Each random digit should be a real number (type double) between 0 and 50. This file and its digits would now serve as...
Please code C# 6. Write a program that generates a random number between 0 and 10...
Please code C# 6. Write a program that generates a random number between 0 and 10 and asks the user to guess that number. The program then informs the user if they guessed correctly or incorrectly then displays the correct answer.
(+30) Write a python program that generates four random between -50 and +50 using python’s random...
(+30) Write a python program that generates four random between -50 and +50 using python’s random number generator (RNG) see the following code (modify as needed ) import random a = 10 # FIX b =100 # FIX x = random.randint(a,b) # (1) returns an integer a <= x <= b # modify a and b according to the lab requirement print('random integer == ', x) and displays 1. the four integers 2. The maximum integer 3. The minimum integer...
(+30) Write a python program that generates four random between -50 and +50 using python’s random...
(+30) Write a python program that generates four random between -50 and +50 using python’s random number generator (RNG) see the following code (modify as needed ) import random a = 10 # FIX b =100 # FIX x = random.randint(a,b) # (1) returns an integer a <= x <= b # modify a and b according to the lab requirement print('random integer == ', x) and displays the four integers The maximum integer The minimum integer The number of...
Random Number Guessing Game Write a program in C++ that generates a random number between 1...
Random Number Guessing Game Write a program in C++ that generates a random number between 1 and 100 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display “Too high. Try again.” If the user’s guess is lower than the random number, the program should display “Too low. Try again.” The program should use a loop that repeats until the user correctly guesses the random number....
Random Number Guessing Game C++. Write a program that generates a random number between 5 and...
Random Number Guessing Game C++. Write a program that generates a random number between 5 and 20 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display Too high. Try again. If the user’s guess is lower than the random number, the program should display Too low, Try again. The program should use a loop that repeats while keeping a count of the number of guesses...
Create a C++ program that generates a random number from 1 to 100 using the rand()...
Create a C++ program that generates a random number from 1 to 100 using the rand() function. Give the user a total 5 chances to guess the number. Stop the program and indicate the user guessed the correct number and should be applauded. Also, please find out which guess was the closest to the actual number. For example, if the rand() produces 57 and the user guessed 10, 80, 52, 33 and 44 in their respective turns then print out...
Write a program in C++ coding that generates a random number between 1 and 500 and...
Write a program in C++ coding that generates a random number between 1 and 500 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display “Too high, try again.” If the user’s guess is lower than the random number, the program should display “Too low, try again.” The program should use a loop that repeats until the user correctly guesses the random number. Count the number...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT