Thread: How to scan in numbers from a file and find the largest?

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    28

    How to scan in numbers from a file and find the largest?

    I'm working on a silent auction program that will scan a file and find the highest from each group of bids, then have a running total of money made throughout the auction. I'm pretty sure the rest of my code works, i'm just getting stuck on finding the largest number from the line in the file, saving it, then moving to the next auction. I hope this is explained clearly, and help would be greatly appreciated!

    input file text (first number is num of auctions, after that it's num of bids, then the bids):
    5
    4
    100 500 250 300
    1
    700
    3
    300 150 175
    2
    920 680
    8
    20 10 15 25 50 30 19 23

    Sample Output
    Auction 1 sold for $500.00!
    Auction 2 sold for $700.00!
    Auction 3 sold for $300.00!
    Auction 4 sold for $920.00!
    Auction 5 sold for $50.00!

    The silent auction raised $2470.00 for charity!




    Code:
    # include <stdio.h>
    # include <stdlib.h>
    # include <time.h>
    
    
    int main () {
    
    
        FILE * ifp;
    
    
        ifp = fopen("bids.txt", "r");
    
    
        float winning_bid, sum=0;
        int num_auctions, num_bids, max_bid, next_bid, i;
    
    
        fscanf(ifp, "%d", &num_auctions);
    
    
         //Loop set up based on the number of auctions
        for (i=0; i<num_auctions; i++) {
    
    
            fscanf(ifp, "%d", &num_bids);
    
           //loop set up based on number of bids
            for (i=0; i<num_bids; i++) {
    
    
                fscanf(ifp, "%d", &max_bid);
    
    
            //scan in numbers from the list and keep the largest
    
    
    
    
            //keep a running sum of total made from auctions
    
    
            }
    
    
        }
    
    
    
    
    
    
    
    
    
    
    }

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    int maximumBid = 0;
    
    ...
    
    for(...)
    {
       int currentBid;
       //  read the bid here into currentBid
       if(currentBid > maximumBid )
          maximumBid  = currentBid ;
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Sep 2013
    Posts
    28
    That helped a lot! My program seems to only run through the first auction though, this is the output:
    Auction 1 sold for $100.
    Auction 2 sold for $500.
    Auction 3 sold for $500.
    Auction 4 sold for $500.

    I'm guessing my first loops are incorrect?

    Code:
    # include <stdio.h># include <stdlib.h>
    # include <time.h>
    
    
    int main () {
    
    
        FILE * ifp;
    
    
        ifp = fopen("bids.txt", "r");
    
    
        float winning_bid[5];
        int num_auctions, num_bids, max_bid=0, current_bid, next_bid, i, j,sum=0;
    
    
        fscanf(ifp, "%d", &num_auctions);
    
    
         //Loop set up based on the number of auctions
        for (i=0; i<num_auctions; i++) {
    
    
            fscanf(ifp, "%d", &num_bids);
    
    
            for (i=0; i<num_bids; i++) {
    
    
                fscanf(ifp, "%d", &current_bid);
    
    
                        if (current_bid > max_bid )
                                max_bid = current_bid;
    
    
    
    
        printf("Auction %d sold for $%d.\n", i+1, max_bid);
            }
    
    
            //keep a running sum of total made from auctions
    
    
    
    
    
    
        }
    
    
    
    
    
    
    
    
    
    
    }

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    You're reusing i as a loop variable, which means the inner loop is destroying the value set by the outer loop. Fixing that will help resolve the initial problem, after which you'll see a couple others which should be easily solvable with a little thinking.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    you also need to reset max_bid after each auction, and after adding it to the overall total.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 01-30-2013, 09:30 AM
  2. Program to find largest of 3 numbers
    By stuartbaggs in forum C Programming
    Replies: 9
    Last Post: 10-11-2012, 04:50 AM
  3. Find largest numbers
    By krakatao in forum C Programming
    Replies: 1
    Last Post: 02-04-2012, 09:58 AM
  4. To Find Largest Consecutive Sum Of Numbers In 1D Array
    By chottachatri in forum C Programming
    Replies: 22
    Last Post: 07-10-2011, 01:43 PM
  5. Find largest and second largest number (help)
    By Arkon in forum C++ Programming
    Replies: 6
    Last Post: 01-20-2006, 11:21 PM