Answer to Question #237627 in C for Aadi

Question #237627

Write a program that inserts 25 random integers from 0 to 100 in sorted order in a linked list.

The program should calculate the sum of the elements and the floating-point average of the

elements.


1
Expert's answer
2021-09-15T17:18:34-0400
#include<conio.h>
#include <iostream>
# include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include <cmath>
#include<dos.h>
using namespace std;


#define MAX	100
#define MIN	0
#define COUNT	25


struct Node 
{
   int data;
   struct Node *next;
};
struct Node* head = NULL;


void InsertData(int NewtData)
{
	struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
	new_node->data = NewtData;
	new_node->next = head;
   	head = new_node;
}


void DisplayLinkedList(void) 
{
	struct Node* ptr; ptr=head;
	cout<<"\n\nSorted Linked List: ";
	while (ptr != NULL) 
	{
  		cout<< ptr->data <<" ";
   		ptr = ptr->next;
	}
}


main()
{
	int n,temp,Nums[COUNT];
	srand(time(0)); 
    for(n=0;n<COUNT;n++)
    {
    	temp = rand();
    	while(temp<MIN || temp>MAX) temp=rand();
       	Nums[n] = temp;
//       	cout<<Nums[n]<<" ";
	}


	for (int i = 0; i < n; ++i) 
    {
        for (int j = i + 1; j < n; ++j) 
        {
            if (Nums[i] > Nums[j]) 
                {
                    temp = Nums[i];
                    Nums[i] = Nums[j];
                    Nums[j] = temp;
                }
            }
    }		
	for(n=0;n<COUNT;n++) InsertData(Nums[n]);
	DisplayLinkedList();
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS