dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2018
    Posts
    4

    Delete all elements that are less than the average value of queue

    Hello!
    I need some help.
    I must do the following task: create a one-way queue with numbers in the range from -50 to +50. After creating the queue, perform the following: find the average value of all queue elements and delete all elements that are less than the average value. At the end of the job, all queues must be deleted.
    I was able to write code to create a queue, delete it, and find the average value of all the elements.
    However, I can not implement the function of removing elements in the queue that are less than average.
    Please help me write this function to remove elements that are less then average value of all the elements of the queue, I will be very grateful for your help!
    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <stdlib.h>
    using namespace std;
    
    struct queue{            
    	int info;			
    	queue *next;			
    }*b, *e;             
    
    void AddQueue (queue **b, queue **e, int in)
    {
    	queue *t=new queue;   
    	t->info=in;          
    	t->next=NULL;        
    	if (*b==NULL) 
    		*b=*e=t; 
    	else {        
    		(*e)->next=t;
    		*e=t;
    	}
    	return;
    }
    
    queue *ReadQueue(queue *t, int &in)   
    {
    	if (t==NULL)
    	{
    		cout<<"Queue is empty!\n";
    		return NULL;
    	}
    	while (t!=NULL)
    	{
    		in=t->info;
    		cout<<in<<" ";
    		t=t->next;
    	}
    	cout<<endl;
    	return t;
    }
    
    void Average(queue *k, queue *e)
    {
    	queue *p=NULL;
    	int number=0, sum=0;
    	double aver;
    	p=b;
    	while(p!=NULL)
    	{
    		number++;
    		sum+=p->info;
    		p=p->next;
    	}
    	aver=(double)sum/number;
    	cout<<"Average value= "<<aver<<endl;
    }
    
    
    void DeleteQueue (queue **b, queue **e)  
    {
    	queue *t;
    	while(*b != NULL)
    	{
    		t=*b;
    		*b=(*b)->next;
    		delete t;
    	}
    	*e=NULL;
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	b=e=NULL;
    	queue *t=NULL;
    	int inf, n;
    	double sum;
    	cout<<"Input the number of elements"<<endl;
    	cin>>n;
    	for(int i=0; i<n; i++)
    	{
    		inf=rand()%100-50;
    		AddQueue(&b, &e, inf);
    	}
    	cout<<"Queue is: "<<endl;
    	ReadQueue(b, inf);
    	
    	Average(b, e);
    	ReadQueue(b, inf);
    	DeleteQueue(&b, &e);
    	if(b==NULL)
    		cout<<"Queue was deleted"<<endl;
    	return 0;
    }
    I really need your help!

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,309

    Re: Delete all elements that are less than the average value of queue

    can not implement the function of removing elements in the queue that are less than average
    Do you know how to remove an element in a one-way queue by adjusting pointers? As this is presumably a homework exercise, we're not going to write the code but will give advice. When removing an element, you need to consider whether the queue is empty, whether it is the first element and top (and bottom if only one element) needs adjusting or the last element and bottom needs adjusting. If neither, then the element is in the middle of the list so the next of the previous element (so much easier if doubly linked - keep a copy of the address of the previous element) needs to be set to the next of the element to be deleted.

    Once you are able to correctly delete elements and you have the required average value, then iterate the queue and delete the required elements.

    Note that you don't need to iterate the queue to find the average value. Keep a total and count when adding to the queue.

    If you post your attempted code we'll provide further guidance.

    Note that for pointers, nullptr should be used instead of NULL. Also rather than passing a pointer to a pointer for begin and end, you could just pass a reference to a pointer. This would make the code somewhat easier.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.7.5)

  3. #3
    Join Date
    Apr 2018
    Posts
    4

    Re: Delete all elements that are less than the average value of queue

    Quote Originally Posted by 2kaud View Post
    we're not going to write the code but will give advice.
    Thank you for your advice, I'm trying to understand the process itself, so I always write the code myself, so thank you very much for the advice that helped me understand the essence!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)