Using C++ Write the follow: On a separate line create a function that will do th
ID: 3575219 • Letter: U
Question
Using C++ Write the follow:
On a separate line create a function that will do the following:
A function to perform the weighted average to compute the grade. The first and last elements in your list are weighted 30% and the second and third is weighted 20%. Only write the CalculateWeightedAverage function.
#include<iostream>
using namespace std;
typedef struct node
{
int number;
struct node *next;
}node;
void create_list(int num, node **head)
{
node *temp = new node; //creating node
temp->number = num;
temp->next = NULL;
if(*head == NULL) //if inserted node is first node then modifying head
{
*head = temp;
}
else //inserting node after head
{
node *current = *head;
while(current->next != NULL)
current = current->next;
current->next = temp;
}
}
double CalculateAverage(node *head)
{
node *current = head;
int sum = 0;
while(current != NULL) //travelling linked list and calculating sum
{
sum += current->number;
current = current->next;
}
return sum/4.0;
}
int main()
{
int num;
node *head = NULL;
for(int i=0; i < 4; i++)
{
cout<<" Enter Number :";
cin >> num;
create_list(num,&head);
}
double avge = CalculateAverage(head);
cout<<"Average adjusted test score: "<<avge<<endl;
return 0;
}
Explanation / Answer
As everything is so specified, the function can be written easily. I went through each node and used its percentage proportion to add in order to get final answer, the weighted average.
double CalculateWeightedAverage(node *head)
{
node *current = head;
int average = 0;
average += current->number*0.3;
current = current->next;
average += current->number*0.2;
current = current->next;
average += current->number*0.2;
current = current->next;
average += current->number*0.3;
//current = current->next;
return average;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.