max or min

This is a discussion on max or min within the C++ Programming forums, part of the General Programming Boards category; say does anyone out there got a good algorithm for finding the max and min of two numbers here is ...

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

    max or min

    say does anyone out there got a good algorithm for finding the max and min of two numbers here is the code that I have but I cant get the minimum to come out correct.
    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=0, min=0, 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;
    		
    	}
    	if(p->info<max)
    		min=p->info;
    	
       	
    		
    	p->next=new(node);
    	
    
    	p=p->next;
    											   //this one works for sure
    }
    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<<list->info<<"  "<<list->next->info<<"  "<<list->next->next->info<<"  "<<list->next->next->next->info
    <<"   "<<list->next->next->next->next->info<<"  "<<endl;
    return 0;
    }

  2. #2
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    think your logic through one step at a time...

    Code:
    if(p->info<max)
    		min=p->info;
    ...so if a number is less than the max, that makes it the min? something is wrong here

    also, your initializing the values to 0. this isnt a good idea.... ill let you think about why

  3. #3
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    the way i would do it is scan through the input numbers, find the largest. Than change the value your checking against to the found highest value, and work backwards through the input to find the lowest. Then you can find the average.

    If you decide that the first number less than the largest is the minimum, your not being accurate in your data.

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

    lost completely

    Dude I'm not going to lie I'm completley lost I just need to see some basic code to get me on the right track

  5. #5
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    loop values and if greater than test value then process storage until you test it by the next value.
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  6. #6
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    Quote Originally Posted by drdodirty2002
    Dude I'm not going to lie I'm completley lost I just need to see some basic code to get me on the right track
    were only going to give you psudo code, we wont do the work for you. We've given you the advice, theres a board search, and theres a ton of webservers (google, eseek) for the same thing. Do some work, make an effort, then come back.

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    assign first value to both max and min. After all, since there are no other values it is both the max and the min. Then with each new value added compare it with max and min. If it is either greater than max or less than min then assign it to the appropriate variable.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help finding the max and min of a 2d array
    By finalreign in forum C Programming
    Replies: 6
    Last Post: 04-29-2009, 11:39 AM
  2. Max Min Problem
    By smithc2005 in forum C Programming
    Replies: 7
    Last Post: 10-22-2008, 10:38 AM
  3. Ranged numbers
    By Desolation in forum Game Programming
    Replies: 8
    Last Post: 07-25-2006, 10:02 PM
  4. Variable Min and Max Program
    By EdanD in forum C Programming
    Replies: 8
    Last Post: 06-30-2004, 07:48 PM
  5. Double output!
    By Spurnout in forum C++ Programming
    Replies: 3
    Last Post: 11-02-2002, 02:35 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21