arrays? can't figure it out.

Write your question here.

Hey folks,
This is a hw problem that i can't seem to figure out.

This is my input file text:
5
4
100 500 250 300
1
700
3
300 150 175
2
920 680
8
20 10 15 25 50 30 19 23

This is my problem :
Programmers for a Better Tomorrow is hosting a silent auction to benefit one of their main charities. Participants in a silent auction write down which item they are bidding for and how much they are willing to bid.

This program will read from a file that contains a list of bids on a number of auctions. The program must determine the amount that each item will be sold for. This will be the highest amount that someone is willing to bid on that item.

When the final bid amount for each item has been determined, print this information to the screen.

Finally, calculate the total amount of money made for the auction (the final bid of each item) and print this information to the screen.

This is an example:
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!

this is what i have right now im having problems either reading from the input file or getting it to be able to choose the highest bid as well as summing up the highest bids. my outout file just says that 0$ was the highest bid for all of them and that 0$ was raised at the end any help will be appreciated thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
 //main function
int main() {
    int i, num_bids[0], max, sum = 0 ,num_auctions, x;
    float bids[0];

    //open input file
    FILE * ifp = fopen("/Users/administrator/Desktop/bids.txt" , "r");
    //open output file
    FILE * ofp = fopen("/Users/administrator/Desktop/output.txt" , "w");
    //scan in info
    fscanf(ifp, "%d" , &num_auctions);

    for(i=0; i<num_auctions; i++){

        fscanf(ifp, "%d",&num_bids[i]);
            for (x = 0; x < num_bids[i];x++)

            {

            fscanf(ifp, "%d", &bids[i]);
            max = bids[i];

            }


        for(i=1; i<num_auctions; i++)
            if (bids[0]>max)
            max = bids[i];

    }

    //sum
    sum += bids[i];

     // print out to the output
    fprintf(ofp,"Auction 1 was sold for $%.2f\n", bids[0]);
    fprintf(ofp,"Auction 2 was sold for $%.2f\n", bids[1]);
    fprintf(ofp,"Auction 3 was sold for $%.2f\n", bids[2]);
    fprintf(ofp,"Auction 4 was sold for $%.2f\n", bids[3]);
    fprintf(ofp,"Auction 5 was sold for $%.2f\n", bids[4]);
    fprintf(ofp, "The total made for charity is $%.2f ", bids[i]);
    // close
    fclose(ifp);
    fclose(ofp);





return 0;

}



Your bids arrays are set to size 0, which means you are reading and writing to invalid locations. This may help you understand the problem.

http://www.cplusplus.com/forum/beginner/112550/

You have three options that I know of.

1. Declare arrays a lot bigger than you expect to need.

2. Use a vector.

3. Manually resize your array as needed (Not recommended).
You don't need to use arrays for this at all.

For each auction item, set the maximum bid to zero at the start. Then as each bid is read, test whether it is greater than the current max. If it is, replace max with the current bid.

After reading all bids for an item, output the text and maximum bid to the output file. Add the maximum bid to the total.

After processing all items, output the total to the file.
Last edited on
Could you show me how please I'm not getting it?
Well, your variables should be declared something like this:
1
2
3
4
5
6
7
    int i, x;
    int num_bids;
    int num_auctions;

    float bid;
    float max;
    float sum = 0;


At the start of each new item, do max = 0;

After reading each bid,
1
2
            if (bid > max)
                max = bid;

At the end of all the bids for that item:
1
2
        sum += max;
        fprintf(ofp, "Auction %d was sold for $%.2f\n", i+1, max);
Topic archived. No new replies allowed.