Thread: Maximum And Minimum

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    78

    Maximum And Minimum

    Okay everyone I managed to do a linked list I did a couple of comparisons to get a maximum and a minimum out of the code observe

    Code:
    #include <iostream>
    #include <cstring>
    using namespace std;
    //linked list first example
    
    struct node
    {
    	int info; 
    	node *next;    //a pointer named node of size node(the structure in which it is contained)
    };
    
    int main()
    {
    int d=0, max, min, counter=0,a[5];
    node *list;		    //a pointer called list of size node
    
    node *p;		    //a pointer called p of size node
    
    list = new(node);   //have list point to a new address
    
    p=list;             //make both of those adresses the same
    
    
    
    for(int i=0; i<5; ++i)
    {
    	cout<<"Enter the value of this node: "<<endl;
    
    	cin>>p->info;                                //enter a value for the first  
    	
    	d=d+p->info;
    	
    	a[i]=p->info;
    	
    	counter++;
    	
    	if(p->info > max)
    	{
    		
    	max=p->info;
    		
    	}
    
    	
    	p->next=new(node);
    	p=p->next;
    											   
    }
    	min=list->info;									//set the first node as the min
    if(list->next->info < list->info)					//compare 2nd node
    													//if smaller than first node it is min	
    	min=list->next->info;							
    
    if(list->next->next->info <list->info 
       && list->next->next->info < list->next->info)    //compare 3rd node if smaller than 1st or 2nd node
    													//3rd node becomes the min
       min=list->next->next->info;						
    
    if(list->next->next->next->info < list->info 
       && list->next->next->next->info < list->next->next->info //compare 4th node if smaller than than previous
       && list->next->next->next->info < list->next->info)		//nodes 4th node becomes the min
    	
    	min=list->next->next->next->info;
    
    if(list->next->next->next->next->info < list->info
       && list->next->next->next->next->info < list->next->next->next->info //compare 5th node if smaller than all 
       && list->next->next->next->next->info < list->next->next->info		//5th node becomes the min
       && list->next->next->next->next->info < list->next->info)
       
       min=list->next->next->next->next->info;	
    ;
    
    cout<<"THIS IS THE MAXIMUM NUMBER "<<max<<endl;
    cout<<"THE AVERAGE IS: "<<d/counter<<endl;
    cout<<"THE NUMBER OF VARIABLES IN THIS LINKED LIST IS "<<counter<<endl;
    cout<<"THE MINIMUM IS "<<min<<endl;
    
    cout<<"THE FIVE NODES ARE "<<list->info
        <<"  "<<list->next->info
        <<"  "<<list->next->next->info
    	<<"  "<<list->next->next->next->info
        <<"  "<<list->next->next->next->next->info<<"  "<<endl;
    return 0;
    }
    Now my question is does anybody know a better way to sort through a linked list to get a minimum

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    One solution is a loop instead of the if statement that points deep inside the linked list.

    Kuphryn

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    while(list->next)
    {
    //some code
    list = list->next;
    }
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-28-2009, 09:58 PM
  2. maximum and minimum
    By aslak in forum C Programming
    Replies: 35
    Last Post: 12-14-2008, 03:54 PM
  3. Displaying Minimum and Maximum values from input
    By mgardnertech in forum C Programming
    Replies: 1
    Last Post: 06-29-2008, 08:47 PM
  4. Maximum and Minimum Values
    By swaugh in forum C Programming
    Replies: 7
    Last Post: 12-16-2006, 09:43 PM
  5. Replies: 2
    Last Post: 10-31-2002, 07:27 PM