Write a program in C that generates a list of random numbers
Write a program in C that generates a list of random numbers greater than or equal to 0 and less than 32, stores them in an array then have the program run through the numbers using a linked lists then print out the numbers in the array.
Solution
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
typedef struct node{ // create a struct for linked list
int data;
struct node *next;
}ll;
void main(){
srand(time(NULL)); // set the seed to random numbers
int n=10,i=0,num; // initilize the random numbers
ll *start = NULL; // set start = NULL
while(i<n){ // for 10 numbers
ll *temp = (ll *)malloc(sizeof(ll)); // allocate memory for a node
temp->data = rand()%32; // set a random number for a node
temp->next = start; // link node to start
start = temp; // move start to beginning
i++;
}
display(start);
}
void display(ll *start){
ll *temp = start;
while(temp!=NULL){ // while ll has elements
printf(\"%d->\",temp->data); // print data
temp = temp->next;
}
}
/* sample output
*/
