Thread: Max&Min values of a given array

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    9

    Max&Min values of a given array

    I have been using this function to calculate the maximum and minimum value of integers of a given array. However this code only prints the last integer that I put in. I think it is a problem with where I have the loop but I am not sure.


    int max_function()

    {

    char std_array3[20];
    int x;
    int a=0;
    int b=0;
    int max;
    bool quit = false
    do

    {

    scanf("%s",std_array3);
    for(x=0;x<1;x++)
    if (!isdigit((int)std_array3[x]))
    {
    quit=true;
    break;
    }
    if(!quit)
    {
    b=atoi(std_array3);
    if(b>a){
    max=b;
    }
    else
    max=a;
    }
    }
    while(!quit);
    printf("The maximum value of integers is %i\n",max);
    return 0;
    }

    I thought that this would work but as I said it only gives me the last integer that I typed in.....I almost missed it as the largest digit that I put in was always the biggest so I thought I was right.
    Any ideas as to how to fix this error

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I think it is a problem with where I have the loop but I am not sure.
    The problem is how you are solving the problem. If you want an array of integers, then simply declare the array as such and input integers instead of strings. It would be so much simpler if you did this. But you obviously haven't been listening to my advice since your first post, so I'll offer one last suggestion that is roughly what you wanted and then leave you to your own erroneous solutions.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string.h>
    #define true  1
    #define false 0
    
    int max_function ( void ) 
    { 
      char std_array3[20]; 
      int b = 0, max = 0; 
      int quit = false; 
      do 
      {
        printf ( "Enter a number (type end to quit): " );
        scanf ( "%s", std_array3 );
        if ( strcmp ( std_array3, "stop" ) == 0 ) 
          quit = true;
        else {
          b = atoi ( std_array3 );
          if ( b > max ) max = b;
        }
      } 
      while ( !quit ); 
      printf ( "The maximum value of integers is %i\n", max ); 
      return 0; 
    }
    
    int main ( void )
    {
      max_function();
      return 0;
    }
    -Prelude
    Last edited by Prelude; 04-18-2002 at 12:06 PM.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    589
    You beat me to it Prelude..Here is another solution. it isn't pretty but I think it works,alittle bit of a kludge. There is stil problems if the user enters something that isn't a number. My advice would be that if you want to work with ints get the input as an array of ints

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    int max_function();
    
    int main()
    {
    	max_function();
    	return 0;
    }
    
    int max_function() 
    { 
    	char std_array3[20];
    	char Char;
    	char *pChar = &Char;
    	int x; 
    	int b=0; 
    	int max = 0;
    	bool quit = false;
    	
    	do 
    	{ 
    		scanf("%s",std_array3); 
    		for(x=0;x<sizeof(std_array3);x++)
    		{
    			if (!isdigit((int)std_array3[x])) 
    			{ 
    				quit=true; 
    				break; 
    			} 
    			else
    			{ 
    				Char = std_array3[x];
    				b=atoi(pChar);
    				
    				if(b > max)
    				{ 
    					max = b; 
    				} 
    			}
    		}
    	}
    	while(!quit); 
    
    	printf("The maximum value of integers is %d\n",max); 
                    return 0;
    }
    Last edited by Barjor; 04-18-2002 at 12:16 PM.

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    9
    No prelude I completely agree with you in this case with the strings and integers as it is far easier, but the reason I am asking is because it is a specification that is required of me.
    And you are right it makes it much harder, but that is the way it is, thanks for your help though.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >but the reason I am asking is because it is a specification that is required of me.
    Okay, that makes more sense. If mine and Barjor's most recent solutions don't work for you, please show us the exact requirements you were given so that we can better solve the problem.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 11-19-2008, 02:36 PM
  2. Count distinct array values
    By rkooij in forum C Programming
    Replies: 4
    Last Post: 10-03-2006, 03:03 AM
  3. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  4. How to read in empty values into array from input file
    By wpr101 in forum C++ Programming
    Replies: 5
    Last Post: 11-28-2002, 10:59 PM
  5. Duplicate values in Array
    By TONYMX3 in forum C++ Programming
    Replies: 2
    Last Post: 01-30-2002, 03:57 PM