. Advertisement .
..3..
. Advertisement .
..4..
Assigning negativecntr with the number of negative values in the linked list allows users to download programs or create linux safely. You can also use it to modify, copy, and share the systems with public modifications.
Follow this post to get the quickest way to accomplish the task.
What Is negativeCntr?
NegativeCntr stands for negative controls. They are particular samples used in the experiment. While they are employed and treated the same ways as others, they will rarely change any of the experiment’s variables.
Usually, a negative control doesn’t receive any treatment or test. All it has to do is to get observed in its natural state. This type of control should always be used with positive controls. This way, they can reduce unwanted variables in the experiment.
In C++, there are three common ways to represent the negativecntr values. The first and least used method is signed magnitude for signed number. In this one, a fixed bit will represent the number’s sign while the rest does with the number’s magnitude.
You can also convert the bit from 0 to 1 from the corresponding positive values’ bit representation. Remember, 0 is a positive sign and a negative one gets 1.
The most commonly used approach to represent a negativectr value is to employ two values’ complement. The fixed sign bit will be calculated for a negative number. Then 1 is added to the flipped bits.
How To Assign NegativeCntr With The Number Of Negative Values In The Linked List
To assign negativeCntr with the number of negative values in the linked list, let’s follow the steps below. In the main() function, accomplish two commands at the last while looping:
- Use an if condition. This function allows you to make sure that the linked list’s values are less than zero.
- Increment the negativeCntr. If all values are less than zero, it’s time to increment the negativecntr. Complete the task one by one to avoid unwanted errors.
Before running the command, don’t forget to add the /* to the statements:
if (currObj->dataVal < 0)
negativeCntr++;
Let’s have a look at the following example to see how these two commands work and assign the negative values:
Code:
#include <stdio.h>
#include <stdlib.h>
typedef struct IntNode_struct {
int dataVal;
struct IntNode_struct* nextNodePtr;
} IntNode;
// Constructor
void IntNode_Create(IntNode* thisNode, int dataInit, IntNode* nextLoc) {
thisNode->dataVal = dataInit;
thisNode->nextNodePtr = nextLoc;
}
/* Insert newNode after node.
Before: thisNode -- next
After: thisNode -- newNode -- next
*/
void IntNode_InsertAfter(IntNode* thisNode, IntNode* newNode) {
IntNode* tmpNext = NULL;
tmpNext = thisNode->nextNodePtr; // Remember next
thisNode->nextNodePtr = newNode; // this -- new -- ?
newNode->nextNodePtr = tmpNext; // this -- new -- next
}
// Grab location pointed by nextNodePtr
IntNode* IntNode_GetNext(IntNode* thisNode) {
return thisNode->nextNodePtr;
}
int IntNode_GetDataVal(IntNode* thisNode) {
return thisNode->dataVal;
}
int main(void)
{
IntNode* headObj = NULL; // Create intNode objects
IntNode* currObj = NULL;
IntNode* lastObj = NULL;
int i; // Loop index
int negativeCntr;
negativeCntr = 0;
headObj = (IntNode*)malloc(sizeof(IntNode)); // Front of nodes list
IntNode_Create(headObj, -1, NULL);
lastObj = headObj;
for (i = 0; i < 10; ++i) { // Append 10 rand nums
currObj = (IntNode*)malloc(sizeof(IntNode));
IntNode_Create(currObj, (rand() % 21) - 10, NULL);
IntNode_InsertAfter(lastObj, currObj); // Append curr
lastObj = currObj; // Curr is the new last item
}
currObj = headObj; // Print the list
while (currObj != NULL) {
printf("%d, ", IntNode_GetDataVal(currObj));
currObj = IntNode_GetNext(currObj);
}
printf("\n");
currObj = headObj; // Count number of negative numbers
while (currObj != NULL) {
if (currObj->dataVal < 0)
negativeCntr++;
currObj = IntNode_GetNext(currObj);
}
printf("Number of negatives: %d\n", negativeCntr);
return 0;
}
Conclusion
Easy program modification and copy will require you to assign negativecntr with the number of negative values in the linked list. Check the tutorial to see how to accomplish it.
Leave a comment