How to determine and print smallest and largest values

This is a discussion on How to determine and print smallest and largest values within the C Programming forums, part of the General Programming Boards category; I need help on how to write a program to determine and print the smallest and largest values contained in ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    14

    How to determine and print smallest and largest values

    I need help on how to write a program to determine and print the smallest and largest values contained in 99-element floating-point array w.

    This is what I have thus far:
    #include <stdio.h>

    int main()

    {
    int num1, num2;
    printf("Smallest number\n");
    printf("Enter Largest number\n");
    scanf("%d%d", &num1, &num2 );

    if( num 1 is < than num2)
    printf( "%d is Smaller than %d\n", num1. num2);

    if(num 2 is > than num1)
    printf("%d is larger than %d\n", num1, num2);
    Anna

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    Sweet baby jebus! I hope that is pseudo code!!

    First off, Where the hell is your array??

    Second,
    Code:
    if( num 1 is < than num2)
    I'll leave that one for you to figure out.

    Third,
    You said it was a floating point array, so why are you using integers??

    Here is the idea behind what you need to do:
    Have a variable to hold the lowest value in your array. Set it to the first element of your array. Iterate through your array, check to see if the current indexed value in yoru array is smaller than the currect variable that holds the smalled value in the array. If it does, make your variable equal to that array indexed value, if not, continue on loop. Do the same for highest value.

    Chances are (from looking at your code) you have on idea what I am talking about, but that is what you need to do.

  3. #3
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    See if something like this can help you get started.

    Code:
    #include <stdio.h>
    
    #define MAXSIZE 7
    
    int main(void)
    {
        double array[MAXSIZE] = { 6.2, 312.8, 2.1, 99.5, 123.5, 17.3, 201.6 };
        double largest, temp;
        
        int i;
        
        largest = 0;  
        
        printf("Of these values:  ");
            
        for(i = 0; i < MAXSIZE; i++)
        {
        	temp = array[i];
        	
        	printf("%.2f, ", temp); 
        	
        	if(temp > largest)
        		largest = temp;
        } 
        
        printf("\nThe largest is: %.2f", largest);
        	
        return 0;
    
    }
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    1

    this is my answer even if its to late

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                int ctr,n,d=0,k=0;
    
    
                for (ctr = 1; ctr <= 5; ctr++)
                {
                    Console.Write("Input number: ");
                    n = int.Parse(Console.ReadLine());
                    if (d < n)
                        d = n;
                    else if (k > n)
                        k = n;
                    
                }
                Console.WriteLine("The largest is " + d);
                Console.WriteLine("The smallest is " + k);
                Console.ReadKey();
            }
        }
    }
    
    the language Im using is C#

  5. #5
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    haywire20, posting on old threads like this is typically (strongly) discouraged on this board. Had you read the board rules, you would know that (though perhaps you missed the relevant part).

    Forum Guidelines. Read before posting

    See, in particular, the second point.

    Also, this is the C forum. If you want to post C# code, do that in the C# forum.

Popular pages Recent additions subscribe to a feed

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