473,405 Members | 2,444 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Max and mean function in c++

I am giving u complete code. It works properly
But add max and mean functions so that it can work properly.
*
/LinkList.h/
*
#include "Node.h"
*
/* The LinkList class declaration*/
class** LinkList
{
public:
*** LinkList();
*** void** add (int** addObject);
*** int**** get();
*** bool** next();
***
*** friend void* traverse(LinkList list);
*** friend LinkList addNodes();
*** friend bool isSorted(LinkList );
***
private:
*** int* size;
*** Node *** headNode;
*** Node *** currentNode;
*** Node *** lastCurrentNode;
*
};
*
/LinkList.cpp/
*
*
#include "LinkList.h"
*
*
/* The LinkList class implementation*/
*
/* Constructor */
LinkList::LinkList()
{
headNode** =** new Node();
*********** headNode->setNext(NULL);
*********** currentNode** =** NULL;
*********** lastCurrentNode* =** NULL;
*********** size** =** 0;
}
***********
/* add() class method */
void** LinkList::add (int** addObject)
{
*** Node *** newNode** =** new** Node();
*** newNode->set(addObject);
*** if( currentNode** !=** NULL )
*** {
******* newNode->setNext(currentNode->getNext());
******* currentNode->setNext( newNode );
******* lastCurrentNode** =** currentNode;
******* currentNode** =** newNode;
**** }
**** else
**** {
*********** newNode->setNext(NULL);
*********** headNode->setNext(newNode);
*********** lastCurrentNode** =** headNode;
*********** currentNode** =** newNode;
***** }
***** size ++;
}
*
/* get() class method */
int** LinkList::get()
{
*********** if (currentNode* !=* NULL)
*********** *return** currentNode->get();
}
*
/* next() class method */
bool** LinkList::next()
{
*** if (currentNode* ==* NULL)** return* false;
*
*** lastCurrentNode* =* currentNode;
*** currentNode* =* currentNode->getNext();
*** if (currentNode == NULL || size == 0)
return* false;
*** else
*********** return* true;
}
*
/* Friend function to traverse linked list */
void traverse(LinkList list)
{
*** Node* savedCurrentNode* =* list.currentNode;
*** list.currentNode* =* list.headNode;
***
*** for(int i = 1; list.next(); i++)
*** {
******* cout << "\n Element " << i << " of the list is* " << list.get()<< endl;
*** }
***
*** list.currentNode* =* savedCurrentNode;
}
*
/* Friend function to add Nodes into the list */
LinkList addNodes(){
***
*********** LinkList list;
*********** int count = 0;
*********** cout* << "\n Enter the length of the LinkList you want to create: ";
*********** cin>>count;
*********** int temp;
*********** for(int i = 0; i < count ; i++)
*********** {
*************** cout* <<"\n Enter the Element No # "<<i+1<<": ";********
***************
*************** cin>>temp;
***************
*************** list.add(temp);
***************
*********** }***
*********** cout* <<* "\n---------------------------------------------";
*********** cout* <<* "\n**** List size is = "* <<* list.size* <<'\n';********
*********** cout* <<* "\n---------------------------------------------";***********
*********** return list;*************
*********** ** }
*****bool isSorted(LinkList list){
****
*** Node* savedCurrentNode* =* list.currentNode;
*
*** list.currentNode* =* list.headNode;
*** list.next();* // move to first element in the list
***
*** bool sorted = true;
***
*** for(int i = 0; i < list.size-1 ; i++)
******* {
********** int current = list.get();* // get current node value in the list
********** list.next();* // moved the list to next node
********** int next = list.get(); // get next node value in the list
********** cout<<"\nComparing current value: "<<current<<" and next value: "<<next<<endl;
********** if (current > next)
********** sorted = false;**********
********** cout<<"bool sorted value is: "<<sorted<<endl;*********
******* }*******
***** list.currentNode* =* savedCurrentNode;***
*** return sorted;
****
****
*** }******
*
/Node.h/
*
#include** <iostream>
#include** <stdlib.h>
using namespace std;*
/* The Node class declaration */*
class** Node
{
*** public:
*********** *** int* get();
*********** *** void set(int );
*********** *** Node * getNext();
*********** *** void setNext(Node *);
*
**** private:
****** int* object;
****** Node * nextNode;
};********
***********
/Node.cpp/
*
#include "Node.h"*
/* The Node class implementation*/*
int Node:: get() { ***
*** return** object; ** *
*** }
void Node:: set(int** object) {
***
*** this->object** =** object;
*** }*
Node * Node::getNext() {
***
*** return** nextNode;
***
*** }
void Node:: setNext(Node** * nextNode) {
***
*** this->nextNode** =** nextNode;
***
*** }
*
*********** /main.cpp/
*
#include** "LinkList.h"*
int main()
{
*** LinkList list* =* addNodes();
*** traverse(list);
*
*** if(isSorted(list))
*** cout<<"\n\n----------- List is sorted----------\n\n";
*** else
*** cout<<"\n\n----------- List is not sorted-------\n\n";*
*** system("pause");
*** return 0;
}
*
Suppose we have to add two basic statistics functions using LinkList, these functions
description is given below,
Our first function is,
Mean:
Description:
This function will calculate mean of the all the elements present in the list and will show
the result. Its formula is,
Mean = Total of all elements of the list / Length of the list.
If list is empty (has length equal to zero) it will show message mean is undefined
Max:
Description:
This function will find the maximum element present in the list and will show it.
If list is empty (has length equal to zero) it will show message undefined value.
*
After that you will add two friend functions in second LinkList as we have added
isSorted function in *project,
friend void mean(LinkList list)
/*it will show the mean of link list elements, but if the list has zero length then it
will display the result “that mean is undefined for this list” */
friend void max(LinkList list)
/*it will show the max element from the list, if list has zero length then it will
display the result “that max element is undefined for this list” */
*
Redefine the above course so it can work properly. Add Mean and Max functions
Oct 18 '07 #1
2 4995
OK, so this appears like you copied directly from assignment... If I am wrong tell me, but no one will help you if they think they are doing your homework for you.
Oct 18 '07 #2
sicarie
4,677 Expert Mod 4TB
I am giving u complete code. It works properly
Expand|Select|Wrap|Line Numbers
  1.  
  2. *** LinkList();
  3. *** void** add (int** addObject);
  4. *** int**** get();
  5. *** bool** next();
  6. *** 
  7.  
WTF?! Are you sure this compiles?

So what is your question on that? You have ... code ... I guess, and an assignment, but what was your question on that?
Oct 18 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

70
by: Roy Yao | last post by:
Does it mean "(sizeof(int))* (p)" or "sizeof( (int)(*p) )" ? According to my analysis, operator sizeof, (type) and * have the same precedence, and they combine from right to left. Then this...
22
by: Tony Johansson | last post by:
Hello Experts! I'm reading i a book about C++ and they mention infix with telling what it is. I hope you out there can do so. Many thanks! //Tony
32
by: Protoman | last post by:
I have a function that calculates the mean of the some numbers; I need it to accept any number of parameters (one needs to be the number of the other parameters) Can you help me out here? Here's...
16
by: Abhishek | last post by:
why do I see that in most C programs, pointers in functions are accepted as: int func(int i,(void *)p) where p is a pointer or an address which is passed from the place where it is called. what...
4
by: huguogang | last post by:
Just curious, any one know what the 3 part parameter "class CString filename" would mean. The code: int TestFunc(class CString filename) { fopen(filename, "w"); } Compile using Visaul C++,...
9
by: pamelafluente | last post by:
Hi, I was "studying" the famous (public code) BusyBox. I see the instruction: var busyBox = new BusyBox as in var busyBox = new BusyBox("BusyBox1", "busyBox", 4, "gears_ani_", ".gif",...
18
by: raghu | last post by:
hello, Iam going through a document contianing C tricks .In it they used the statement in many places i didn't understand can u please tell me the meaning and how the compiler compiles it and runs...
92
by: Heinrich Pumpernickel | last post by:
what does this warning mean ? #include <stdio.h> int main() { long l = 100; printf("l is %li\n", l * 10L);
4
by: dolphin | last post by:
Hi All I read a .cpp files,find that static void fun(void){......} int main() { .......... } What does this static function mean?Is it the same as the static
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.