PDA

View Full Version : So anyone who knows some programming logic feel like saving my life??


dvd_master
11th December 2003, 23:07
Hi everyone! Huge huge problem I can really use help with. I got this programming logic class, where a program that would take two numbers and add them together would look like this:

Program_Name_Here
read num_1, num_2
newnumber = num_1 + num_2
Print newnumber
END

We've gone through arrays and modules (Fuctions, etc) and even Object Oriented Design. Well, I HAVE to do this paper, and I have no clue! No at all! Can someone PLEASE PLEASE help me! I know that you'll take previous time out of your schedule to do it, but I won't take it for granted! Please!

---------------------------

A real estate company has 25 salespersons in its staff. A record is kept of each sale made by each salesperson. Each of these records contains:

Salesperson number, salesperson name, amount of sale

The number of input records is unkown. The Number of sales may vary. For example, one sales person may have 12 sales, and another may have 10. The input records are not in sequence. The report is to look as follows:

Salesperson No. Salesperson Name Total Amount
1 John Doe 50240.75
2 Sue Brown 72349.50
.
.
.
25 Bill Smith 21355.00

The end-of-file mthod should be used to detect the end of data (i.e., DOWHILE not EOF)

----------------------------------------------

Please please please! Even if you know any segments of it, help me out!

gqtie
11th December 2003, 23:39
It looks like you'll need to loop through the text file once for every sales person in order to compute total for each, then print


for i = 1 to NumberOfSalesPersons

totalsales = 0
do until EOF

if ROW_ID = i then 'add up current sales person's sales
totalsales = totalsales + ROW_SALES
name = ROW_NAME
id = ROW_ID
end if

loop

print id name totalsales

next



This assumes IDs are contiguous.

I hope this helps.

dvd_master
13th December 2003, 03:29
Thanks a ton! But we've never done the

for i = 1 to NumberOfSalesPersons


thing. Is there a different way to do that same function?

Thanks A LOT for the help, I worship you!

pacohaas
13th December 2003, 04:31
any sort of loop will do(for, while, etc), sounds like they want you do use dowhile. i'd read up on that in your book if i were you, then go for it.

b0b0b0b
13th December 2003, 05:03
Here you go. I didn't try compiling it.


import java.io.*;

public class jobu {
public static void main(String[] args)
throws Exception
{
if (args.length == 0) {
System.out.println("Required argument input file name missing");
return;
}

BufferedReader in = new BufferedReader(new FileReader(args[0]));
int MAX = 25;
String names = new String[MAX];
double amts = new double[MAX];
String line;
// Salesperson number, salesperson name, amount of sale
while ((line = in.readLine()) != null) {
for (StringTokenizer st = new StringTokenizer(line, ","); st.hasMoreTokens();) {
int num = Integer.parseInt(st.nextToken());
String name = st.nextToken();
double amt = Double.parseDouble(st.nextToken());
names[num] = name;
amts[num] += amt;
}
}
in.close();

System.out.println("Salesperson No. Salesperson Name Total Amount");
for (int i=0; i<MAX; i++) {
System.out.println(i+" "+names[i]+" "+amts[i]);
}
}


}

wmansir
13th December 2003, 05:18
EDIT: Looks like b0b0b0b beat me to it...but I'm still finishing this up (accidentally submitted it early).

If you have done arrays it is easy.

Create an array, size 25, and zero out the initial values. Say you call it Total_Sales[25]. Then just read each input and calculate:

Lets say your input is of the format

ID NAME SALE_AMOUNT

For each order entered you would execute (or until OEF):

Total_Sales[ID]=Total_Sales[ID]+ SALE_AMOUNT

For example if your input order was

3. John Smith $3000

you would want to add $3000 to John smith's total sales.

So Total_Sales[3]=Total_Sales[3]+$3000

In the end your array would contain a total for each salesperson, indexed by their ID. You Could also create a second array to hold their name, indexed by ID. This assumes each salesperson recorded at least one sale, so they are present on the input list.

I'm not sure what kind of pseudo-code your using, but it could look like this:

create array Total_Sales[25]=0
create array Names[25]=0

DO WHILE not EOF
read ID,SALESNAME,SALESAMOUNT
Names[ID]=SALESNAME
Total_Sales[ID]=Total_Sales[ID]+SALESAMOUNT
END WHILE

FOR i = 1 TO 25
Print i Names[i] Total_Sales[i]

END


If you can't do the FOR TO you can enumerate it

PRINT 1 Names[1] Total_Sales[1]
PRINT 2 Names[2] Total_Sales[2]
....

Atamido
13th December 2003, 07:32
Is this for a specific programming language?

b0b0b0b
13th December 2003, 08:06
From the original post, it doesn't seem like it's for any particular programming language, but a paper about the problem.

Topics worth mentioning (if it's a paper) are:
--error handling, e.g., what if someone has no sale-- we cannot report his name given these constraints, sales person id # not matching with the name, ioexception, ... could fail-fast if you're chancing messing up a salesperson's numbers
--arbitrary number of salespeople. arrays with a "MAX" length will be no good. hashtable / hashmap. what if there are *very many* salespeople? doesn't make sense to keep all that in memory if you are constrained-- external storage such as scratch file or database might be useful.
--accuracy of float currency representation. due to the nature of the float / double data representation, math isn't accurate in the sense that a salesperson might want it. note how sql has a currency datatype and java has bigdecimal/biginteger. alternatives are to use a more precise, inevitably slower structure, or to just keep everything in terms of integers and remember where the decimal point goes.
--if this data were in a database table you could get this report in one sql statement
--given that you've already talked about object oriented programming, you could apply it to this case, but it might be appropriate to mention that it would be overengineering this problem.

dvd_master
13th December 2003, 16:34
Thanks SO MUCH for the awesome help from all of you guys! This isn't a programming language, but a logic thing, so an actual program source wouldn't work, though i can't tell you how much I appreciated all the trouble you went through.

I hope to use all the examples from here and put them into one, but i realized there's a second part.

I have to find who has the most sales, and the second most sales. Is there a quick way to add this in afterwards? I think it's like

John Do - $50
John Do - $24

That would count as two sales, not 74, so I guess I'd have to add a counter somewhere to?

Thanks sooo much! I have to turn it in Monday, so I should be ready!