linkedsa list

Jul 5, 2015 at 7:13pm
void HighestLowest()
{
node *cur;
cur = head;
double lowestvalue,highestvalue;

highestvalue = lowestvalue = cur->number; //set the high&low to first element

// loop through the linked list
for (cur = cur->next; cur != NULL; cur = cur->next)
{
if (cur->number > highestvalue)
highestvalue = cur->number;
else if (cur->number < lowestvalue)
lowestvalue = cur->number;
}
cout << "The highest value is " << highestvalue<< "\n\n";
cout << "The lowest value is " << lowestvalue<< "\n\n";
}

Can anyone help me with this code?? This code give me the highest value but it didn't give me the lowestvalue. Please can anyone help me?????
Last edited on Jul 6, 2015 at 3:29pm
Jul 5, 2015 at 11:34pm
Really?

Are you saying you can't modify this sample code to find the lowest. Have you any idea how it finds the highest?
Jul 5, 2015 at 11:43pm
First, please see the article "How to use code tags"
http://www.cplusplus.com/articles/jEywvCM9/

Then...

Looks OK to me?

Do you have more information??

Andy

C++ Shell o/p (as expected)

The highest value is 23.5

The lowest value is 0.2


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// quick "hack" to test HighestLowest() function
#include <iostream>
using namespace std;

struct node
{
    node *next;
    double number;
};

node n5 = {NULL, 0.2};
node n4 = {&n5, 23.5};
node n3 = {&n4, 19.0};
node n2 = {&n3, 4.0};
node n1 = {&n2, 7.4};

node *head = &n1;

void HighestLowest()
{
    node *cur;
    cur = head;
    double lowestvalue, highestvalue;
    // NOTE assumption that cur is non-NULL
    highestvalue = lowestvalue = cur->number; //set the high&low to first element

    // loop through the linked list
    for (cur = cur->next; cur != NULL; cur = cur->next)
    {
        if (cur->number > highestvalue)
            highestvalue = cur->number;
        else if (cur->number < lowestvalue)
            lowestvalue = cur->number;
    }
    cout << "The highest value is " << highestvalue<< "\n\n";
    cout << "The lowest value is " << lowestvalue<< "\n\n";
}

int main()
{
    HighestLowest();

    return 0;
} 
Last edited on Jul 5, 2015 at 11:51pm
Jul 6, 2015 at 3:56am
but this code doesn't give the lowest value when user input.
Jul 6, 2015 at 5:24am
Enter a value:(0 to print/exit)
3
Enter a value:(0 to print/exit)
2
Enter a value:(0 to print/exit)
1

Highest value is: 3
Lowest value is:-6.27744e+066

Above are the output. Can anyone help me to solve this problem????
Last edited on Jul 6, 2015 at 5:25am
Jul 6, 2015 at 10:46am
We need to see your input code!

HighestLowest() appears to work so the problem is very likely elsewhere.

Andy
Jul 6, 2015 at 12:27pm
Here are the code:-

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include<iostream>

using namespace std;

struct node
{
	double number;
	node *next;
};

node *head = NULL;

void InsertValue()
{
	node *cur;
	node *num;

	double temp;

	num = new node;
	head = num;
	cur = head;

	cout<<"Enter a value:(0 to print/exit) \n";
	cin>>temp;

	if(temp != 0)
		cur->number = temp;

	while(temp != 0)
	{
		num = new node;
		cur->next = num;
		cur = num;

		cout<<"Enter a value:(0 to print/exit) \n";
		cin>>temp;

		 if(temp != 0)
        {
            cur->number = temp;
        }
	}

	 cur->next = NULL;
	 
	 cur = NULL;
     num = NULL;

}

void HighestLowest()
{
	node *cur;
	cur = head;
	double lowestvalue, highestvalue;

    highestvalue = lowestvalue = cur->number; 

    for (cur = cur->next; cur != NULL; cur = cur->next)
    {
        if (cur->number > highestvalue)
            highestvalue = cur->number;
        else if (cur->number < lowestvalue)
            lowestvalue = cur->number;
    }
    cout << "The highest value is " << highestvalue<< "\n\n";
    cout << "The lowest value is " << lowestvalue<< "\n\n";
}

int main()
{
        InsertValue();
	HighestLowest();
}


Can somebody check my code please????
Last edited on Jul 6, 2015 at 12:58pm
Jul 6, 2015 at 12:53pm
Did you not see my request to use code tags? If you want people to go to the trouble of checking out your code you should present it in a clearly formatted way! It not only makes it easier for someone to read your code, it also makes it possible to refer to the line where an error is found.

(You could go back and tag/reformat your earler posts so this thread is more readable in tbe future?)

But it does look like you are adding a node with uninit number member when temp == 0 (you should check whether you want to terminate before adding the new node, not after.)

(I would point out which lines the problem was on but I can't be bothered to count!)

Also be aware that testing doubles with == can be problematic. 0 is safe if you set it specifically (well, if this wasn't the case you're loop would never have temrinated!), but if you are expecting the result of a calculation to be 0 you could come unstruck as it might really be some really small number due to rounding errors. Dor this reason, double value are usually tested to determine it they're within some 'epsilon' of each other, than for equality.)

Andy
Last edited on Jul 6, 2015 at 12:57pm
Jul 6, 2015 at 12:56pm
andywestken wrote:
First, please see the article "How to use code tags"
http://www.cplusplus.com/articles/jEywvCM9/
You ignore this?

The problem is this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
while(temp != 0)
{
num = new node; // Here you add the node
cur->next = num;
cur = num;

cout<<"Enter a value:(0 to print/exit) \n";
cin>>temp;

if(temp != 0) // Here you check whether the value should be set
// If the user enters 0: The node is added but no value is set (hence you get some garbage value like -6.27744e+066)
{
cur->number = temp;
}
}
Jul 6, 2015 at 1:02pm
So should I erase the if (temp != 0) ? and what should I change to then?
Last edited on Jul 6, 2015 at 1:18pm
Jul 6, 2015 at 1:19pm
what do you mean by terminate the add node before and not after????

I said "you need to check whether you want to terminate before adding the node" as in "terminate the loop".

Now you have line numbers... (even tho' the indenting is straggly!)

You should not be creating a new node on line 32. By doing so you always end up with an extra node on the end of your list with uninit number member.

Instead, it should be created within the if block starting on line 40 (with knock on adjustments elsewhere.)

Asctually, you also have the same problem for the first node. It's too early to create the node on line 20. Get the number first and only create a new node it the numer isn't 0.

Andy
Last edited on Jul 6, 2015 at 1:24pm
Jul 6, 2015 at 1:35pm
andy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
void InsertValue()
{
	node *cur;
	node *num;

	double temp;

	cout<<"Enter a value:(0 to print/exit) \n";
	cin>>temp;

	num = new node;
	head = num;
	cur = head;

	if(temp != 0)
		cur->number = temp;

	while(temp != 0)
	{
		
		cout<<"Enter a value:(0 to print/exit) \n";
		cin>>temp;

		 if(temp != 0)
        {
			num = new node;
			cur->next = num;
			cur = num;

            cur->number = temp;
        }
	}

	 cur->next = NULL;
	 
	 cur = NULL;
     num = NULL;

	 return;
}


Is this the correct way of doing it? but what if I want to show the list that user entered does it show the last node number?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void show_list()
{
    cout << endl;
 
    node *cur;
    cur = head;
 
	cout<<"The list contains: \n";

    while(cur->next != NULL)
    {
        cout << cur->number << endl;
        cur = cur->next;
    }

	return;
}


because my show list code doesn't give the last element. Can you give me the code to add at my insert value?
Last edited on Jul 6, 2015 at 2:07pm
Jul 6, 2015 at 3:03pm
Indenting correctly would help!!! I would be rather more inclined to help if your code was tidier!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
void InsertValue()
{
	node *cur;
	node *num;

	double temp;

	cout<<"Enter a value:(0 to print/exit) \n";
	cin>>temp;

	num = new node; // what happens if user enters 0?
	head = num;
	cur = head;

	if(temp != 0)
		cur->number = temp;

	while(temp != 0)
	{
		cout<<"Enter a value:(0 to print/exit) \n";
		cin>>temp;

		 if(temp != 0)
	 	 {
			num = new node;
			cur->next = num;
			cur = num;

         	 	cur->number = temp;
	         }
	}

	cur->next = NULL;
	 
	cur = NULL;
	num = NULL;

	//return;
}


The loop looks OK, but the code now on line 11-13 should only be run if the value entered (temp) is not 0.

And

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void show_list()
{
    cout << endl;
 
    node *cur = head;
    // in C++ there is no point declaring on one line
    // and inititlizing on another!
 
    cout<<"The list contains: \n";

    while(cur != NULL) // not cur->next != NULL (do you see why?)
    {
        cout << cur->number << endl;
        cur = cur->next;
    }

    //return; pointless it at end of function (but useful for ealy returns)
}


Andy
Last edited on Jul 6, 2015 at 3:05pm
Topic archived. No new replies allowed.