Finding the maximum and minimum numbers in a file containing 20 integers

This is a discussion on Finding the maximum and minimum numbers in a file containing 20 integers within the C Programming forums, part of the General Programming Boards category; I am having trouble with writing a program that is to find the smallest and largest numbers from a file ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    40

    Finding the maximum and minimum numbers in a file containing 20 integers

    I am having trouble with writing a program that is to find the smallest and largest numbers from a file containing 20 integers. I started it but i cant seem to get it to work. Can someone please help me.

    Code:
    #include <stdio.h>
    
    int main (void)
    {
    	int counter = 0;
    	int num1;
    	int num2;
    	int largest;
    	int smallest;
    	
    	while (counter < 20)
    	{
    		scanf("%d%d", &num1, &num2);
    		if (num1 > num2)
    			largest = num1;
    		if (num1 < num2)
    			smallest = num1;
    		counter++;
    	}
    	printf("%d  %d", largest, smallest);
    	
    	return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You're comparing wrong. You should be:
    Code:
    while not done
        read a number
        compare number to smallest
            if smaller, update smallest
        compare number to largest
            if larger, update largest
    You're comparing 1 aginst 2, but not against large or small.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    I would suggest using a for loop instead of a while loop
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help in finding average of a class of numbers
    By hastefan2001 in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2009, 01:11 PM
  2. Replies: 2
    Last Post: 02-08-2009, 08:26 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. I'm not THAT good am I?
    By indigo0086 in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-19-2006, 10:08 AM
  5. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM

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