Help with list homework please

exampple:7, 3, 5, 4
-List the total count of odd values in the linked list(from example above: 3)
-List the nodes data value of odd integers (from example above: 5, 7, 3)
-List the total sum of odd integers (from example above: 15)
-List the smallest and the largest odd number in the linked list (from example above: smallest= 3, largest= 7).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void List::Count()
{
        node * temp = head;
        int even;
        int acc;
        while(temp != NULL && (temp->data % 2) == 0)
        {
                acc += *temp->*data;
                temp = temp->next;
        }
        if (acc != 0)
        {
                even++;
        }
        cout << "There are " <<even<< " even nodes." << endl;
        cout << "The added value is "<<acc<<".\n";


}


I think the code gives me back the added address of temp instead of the actual values in acc int.
I need help to list the odd values on the linked list.
the rest of my functions work properly, I was thinking of using a vector or maybe I should use an array to store the list of odds, but I am not sure, also I am lost in how to do the last question.
help would really be appreciated, thank you
Hello Psalazar2016,

Without having the rest of the program to test with I am thinking that this might work for you:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void List::Count()
{
	node * temp = head;
	int even{};
	int acc{};

	while (temp != NULL)
	{
		if (temp->data % 2 == 0)
			even++;
		else
		{
			acc += temp->data;
			temp = temp->next;
		}

	cout << "There are " << even << " even nodes." << endl;
	cout << "The added value is " << acc << ".\n";
}

Notice I initialized the variables "acc" and "even". Uninitialized they only contain garbage because the compiler only sets aside a memory location for the variable and the variable tries to use what is there and make an "int" out of it. On my computer an uninitialized variable usually contains the number "-858993460". Weather you add one to "even" or do the "+=" with "acc" you are only adding to to "-858993460" nor starting with zero like you should.

In line 13 I removed the "*" because you do not need to de-reference the variables just use them.

Put this in your program and see if it does what you want. If not let me know.

Hope that helps,

Andy
Thank you for tour help, I am in church right now, but I will test it and let you know in an hour.
Hello again, your input is really appreciated, but the loop you provided after closing the while loop gives me a never ending loop, see the output below...

There are 1 even nodes.
The added value is 0.
There are 2 even nodes.
The added value is 0.
There are 3 even nodes.
The added value is 0.
There are 4 even nodes.
The added value is 0.
There are 5 even nodes.
The added value is 0.
There are 6 even nodes.
The added value is 0.
There are 7 even nodes.
The added value is 0.
There are 8 even nodes.
The added value is 0.
There are 9 even nodes.
The added value is 0.
There are 10 even nodes.
The added value is 0.
There are 11 even nodes.
The added value is 0.
There are 12 even nodes.
The added value is 0.
There are 13 even nodes.
The added value is 0.
...

Also if you could help me with the 3rd and 4th part of the question it would be appreciated
I tried adding to an int called temporal... but the compiler gave me an error saying that I cannot add nodes to integer, if you have any tip or have an idea of how I can modify this function to make it work I would really appreciate it. I already did all my previous functions but I am stuck in this one.

Thanks in advance.
Show your code that gave those results, please, @Psalazar2016. It looks like your loop structure is wrong. Andy's
temp = temp->next;
should have had a closing brace before it and been backed up one indentation.
This is the code I used to create the nodes

node *temp=new node;
temp->data=value;
temp->next=NULL;
if(head==NULL)
{
head=temp;
tail=temp;
temp=NULL;
}
else
{
tail->next=temp;
tail=temp;
}

and this is the code I ran when for this question...

void List::Count()
{
node * temp = head;
int even{};
int acc{};

while (temp != NULL)
{
if (temp->data % 2 == 0)
even++;
else
{
acc += temp->data;
temp = temp->next;
}

cout << "There are " << even << " even nodes." << endl;
cout << "The added value is " << acc << ".\n";
}
}

I just need help with this function, everything else is working properly, it is only this function that needs work... Sorry if I dont add any more code, is just that I am trying to follow the policy for cplusplus and not show homework results.
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
void List::Count()
{
   int even = 0;
   int odd = 0;
   int acc = 0;

   node * temp = head;
   while (temp != NULL)
   {
      if (temp->data % 2 == 0)      // the following section will be done if even
      {
         even++;
      }
      else                          // the following section will be done if odd
      {
         odd++;
         acc += temp->data;
      }                             // *** MOVED TO CLOSE THIS HERE

      temp = temp->next;            // *** MAKE SURE THIS IS DONE FOR ALL NODES
   }

   cout << "There are " << even << " even nodes." << endl;   // Aren't you asked for ODD nodes?
   cout << "The added value is " << acc << ".\n";
}


You should now have two clearly demarked sections for you to fill in with any commands if the data is odd or if it is even. That should allow you to do the other parts of your homework as well. I think you have currently done (the opposite of part 1) and part 3.
Last edited on
I was able to figure it , Thank you all who helped me. at the end I used an array to store all the values of odd and a loop to print them out. used another loop to select the largest and another to select the smallest. At the end I answered all questions. Now I feel good =).

Happy holidays all.
@lastchance,

Sorry about that I must have accidentally deleted the closing brace when I moved things around and did not catch it.

Thanks for point that out.

Andy
Topic archived. No new replies allowed.