Hi -- I am writing a program taht is supposed to input 12 integers into a program, sort them as they are entered, and then Print out the Sorted list. Second step is compute the average. I have done all that.
THIRD is to delete all the integers that are less than the average. I am having trouble with that part. The entire program is below and it runs great when I take out the "Delete less than " information in Int Main. I only get 2 errors, but still won't run. Can someone point me in the right direction? I am a beginner on Linked Lists.
Thanks
**************************************************

``````#include <iostream>
#include <fstream>
usingnamespace std;

//Functions
void GetData (int& number);
constint nil=0;
class node_type // Declaration of Class
{
public:
int num; // Numbers that we are entering
node_type *next;
};
int main ()
{
node_type *first, *p, *q, *newnode, *w; // Introducing the Nodes
int i;
int number;
int sum, average;
sum = 0;
first = new node_type;
p=first;
GetData(number);
(*first).num = number;
(*first).next = nil;
for (i=1; i<=11; i++)
{
GetData (number); // Input Numbers
newnode = new node_type;
(*newnode).num=number;
(*newnode).next=nil;
(*p).next = newnode; // Links previous Node to new Node //
p=newnode;
}

bool sw=false;
while(!sw)//you do this till there are no adjacent numbers in reverse order
{
sw=true;
if (first->num>first->next->num)
{
q=first->next;
first->next=q->next;
q->next=first;
first=q;
sw=false;
}
q=first;
while (q->next->next)
{
p=q->next;
w=p->next;
if (p->num>w->num)
{
q->next=w;
p->next=w->next;
w->next=p;
sw=false;
}
q=q->next;
}
}

q=first;
cout<<" The ordered numbers are: \n";
while (q != nil)
{
cout << (*q).num << "\n ";
sum = sum + (*q).num;
q = (*q).next;
}//endwhile
//Compute Average
average = sum/12;
cout << "The Average of the Numbers entered is " <<average<< "\n";
//Delete Nodes from List
if ((*first).num >=average)
{
cout << "Nothing to Delete \n" ;
}

else
{
q=first;
p=(*q).next;
while (((*p).next != nil)&&((*p).next < average))
{
q=p;
p=(*p).next;
}
//endwhile
if ((*p).num > average)
{
first = p;
}
else
{
first = nil;
}
}
//end if
//end if
//end if
return 0;
}

void GetData(int& number)
{
cout<< "Enter an integer \n";
cin>>number;
cout<< number << "\n";
}``````

`while (((*p).next != nil)&&((*p).next < average))`

You're comparing a pointer with an integer. It's exactly what the my compiler said about that line. You should compare it with the value of 'num' contained in that node that you're currently comparing it with...

Secondly, you don't have to always use the syntax (*pointer).member, instead there's a shorthand version ->. let me show you an example:

``````p->next = blahblah; // same as (*p).next except it's easier on the eyes
``````

`while (((*p).next != nil)&&((*p).next < average))`

You're comparing a pointer with an integer. It's exactly what the my compiler said about that line. You should compare it with the value of 'num' contained in that node that you're currently comparing it with...

Secondly, you don't have to always use the syntax (*pointer).member, instead there's a shorthand version ->. let me show you an example:

`p->next = blahblah; // same as (*p).next except it's easier on the eyes`

******************************************
I guess I am confused as to how I would compare the average i just computed that is an integer to one of the integers that is in the info field of the nodes that are sorted. Would I have to insert that average into a node in the list? Am I missing something? I understand the concept and the flow, but for some reason just don't know how to compare the two. Thanks for the help.

What I'm saying is that you can't just compare p->next, because that's a pointer. You would want to compare the average to p->next->num, which is the actual value contained in the node.

Wow -- thanks I did not even realize I was pulling from the next feild instead of "info" field on that one...lol...

My new code is here. Only prob is it is not printing the new list after the numbers are deleted that are below average.

``````//Delete Nodes from List
if ((*first).num >=average)
{
cout << "Nothing to Delete \n" ;
}

else
{
q=first;
p=(*q).next;
while (((*p).next!= nil)&&(p->next->num < average))
{
q=p;
p=(*p).next;
}
//endwhile
if ((*p).num > average)
{
first = p;
}
else
{
first = nil;
}
}
//end if
//end if
//end if
q=first;
cout<<" The First List of Ordered Numbers after Deleting those Less than Average : \n";
while (q != nil)
{
cout << (*q).num << "\n ";
sum = sum + (*q).num;
q = (*q).next;
}
//endwhile
return 0;
}``````

>Only prob is it is not printing the new list after the numbers are deleted that are below average.
You've got a few little bugs below...

``````q=first;
p=(*q).next; // why set this to the next node?
// do you realize that you will skip the first node?

// by comparing the 'next' node, you skip another node, so the first 2 nodes are skipped (from the first bug)
while (((*p).next!= nil)&&(p->next->num < average))
{
q=p;
p=(*p).next;
}
//endwhile

// because you were comparing the 'next' node,
// 'p' will be one node behind and this statement will always fail
if ((*p).num > average)
{
first = p;
}
else
{
first = nil;
}``````

Thank you so much. Basically with your help you reminded me to draw it out and walk through what I was doing on my program. It was very helpful. Here is my new program - it runs good but if you see any more errors that may interfere with my next few steps I have to write, please let me know. Thanks again.

``````//Delete Nodes from List
if ((*first).num >=average)
{
cout << "Nothing to Delete \n" ;
}

else
{
q=first;
p=q;
while (((*p).next!= nil)&&(p->num<average))
{
first= (*p).next;
p=(*p).next;
q=p;
}
//endwhile
if ((*p).num >= average)
{
first = p;
}
else
{
first = nil;
}
}
//end if
//end if
//end if
q=first;
cout<<" The First List of Ordered Numbers after Deleting those Less than Average : \n";
while (q != nil)
{
cout << (*q).num << "\n ";
sum = sum + (*q).num;
q = (*q).next;
}
//endwhile``````

Okay so one last question! The next part of the problem was to input a second set of integers (6 this time) just like the first round and sort as I input them. I copied the first part of the problem's program, but it is coming out different. Any ideas? Here is the entire code, if you compile you will see. Thanks again.

``````#include <iostream>
#include <fstream>
usingnamespace std;
//Functions
void GetData (int& number);
constint nil=0;
class node_type // Declaration of Class
{
public:
int num; // Numbers that we are entering
node_type *next;
};
int main ()
{
node_type *first, *p, *q, *newnode, *w; // Introducing the Nodes
int i;
int number;
int sum, average;
sum = 0;
first = new node_type;
p=first;
GetData(number);
(*first).num = number;
(*first).next = nil;
// Inputting first list of Numbers (12 Integers)and Sorting them for Output
for (i=1; i<=11; i++)
{
GetData (number); // Input Numbers
newnode = new node_type;
(*newnode).num=number;
(*newnode).next=nil;
(*p).next = newnode; // Links previous Node to new Node //
p=newnode;
}

bool sw=false;
while(!sw)//you do this till there are no adjacent numbers in reverse order
{
sw=true;
if (first->num>first->next->num)
{
q=first->next;
first->next=q->next;
q->next=first;
first=q;
sw=false;
}
q=first;
while (q->next->next)
{
p=q->next;
w=p->next;
if (p->num>w->num)
{
q->next=w;
p->next=w->next;
w->next=p;
sw=false;
}
q=q->next;
}
}

q=first;
cout<<" The First List of ordered numbers are: \n";
while (q != nil)
{
cout << (*q).num << "\n ";
sum = sum + (*q).num;
q = (*q).next;
}//endwhile
//Compute Average of First List of Numbers
average = sum/12;
cout << "The Average of the Numbers entered is " <<average<< "\n";
//Delete Nodes from First List of Numbers
if ((*first).num >=average)
{
cout << "Nothing to Delete \n" ;
}

else
{
q=first;
p=q;
while (((*p).next!= nil)&&(p->num<average)) //Goes through List finding those less than average and Deleting less than
{
first= (*p).next;
p=(*p).next;
q=p;
}
//endwhile
if ((*p).num >= average) // Once Get through the sorted list of Integers that are less than average, reposition First Pointer to stay.
// This will start the new output list
{
first = p;
}
else
{
first = nil;
}
}
//end if
//end if
//end if
q=first;
cout<<" The First List of Ordered Numbers after Deleting those Less than Average : \n";

while (q != nil)
{
cout << (*q).num << " \n ";
sum = sum + (*q).num;
q = (*q).next;
}
//endwhile
cout<<"\n ";
//Inputting Second List of Numbers (6 Integers) sorting as the List is Created
for (i=1; i<=6; i++)
{
GetData (number); // Input Numbers
newnode = new node_type;
(*newnode).num=number;
(*newnode).next=nil;
(*p).next = newnode; // Links previous Node to new Node //
p=newnode;
}

while(!sw)//you do this till there are no adjacent numbers in reverse order
{
sw=true;
if (first->num>first->next->num)
{
q=first->next;
first->next=q->next;
q->next=first;
first=q;
sw=false;
}
q=first;
while (q->next->next)
{
p=q->next;
w=p->next;
if (p->num>w->num)
{
q->next=w;
p->next=w->next;
w->next=p;
sw=false;
}
q=q->next;
}
}

q=first;
cout<<" The Second List List of sorted numbers are: \n ";

while (q != nil)
{
cout << (*q).num << "\n ";
sum = sum + (*q).num;
q = (*q).next;
}//endwhile
cout<<"\n";
return 0;
}

void GetData(int& number)
{
cout<< "Enter an integer \n";
cin>>number;
cout<< number << "\n";
}``````

>I copied the first part of the problem's program, but it is coming out different.
Copying and pasting is a BAD idea, and should be avoided whenever possible.

>Any ideas?
Well, you could start by taking your original program that works, and splitting the parts of code into functions. Then once you have that working, all you have to do is call the functions from the main() function. It's also a lot easier to see your mistakes when you've split up the code.

And by the way, it's very annoying when you don't use code tags, so if you started using them, it would be much-appreciated. Thanks.

Thank -- not sure why you are being snotty for I think that for a first time Linked Lister, I am not doing too bad of a job.

Its first time for you, not for us.

And btw, you already must have got the warning for not adding code tags. Remember to use them in the future.

Thank -- not sure why you are being snotty for I think that for a first time Linked Lister, I am not doing too bad of a job.

Joe wasn't being snotty, he gave you the suggestions you needed, all 3 of them. And we have no idea what this means:

I copied the first part of the problem's program, but it is coming out different.

In what way? When you copy and paste you should get exactly what you copied.