0

I am currently trying to write a program which provides a quarterly overview of the amount of money that a user defined number of salesmen has made for a whole year.

Here is the code I have at the moment:

program Verkopers;

var month, day, sellernr:array[0..99] of integer;
  sale:array[0..99] of real;
  count, num: integer;

begin
  num := 0;
  writeln('Enter seller number. To stop enter "0"');
  readln(sellernr[num] );
  while (sellernr[num] <> 0) do
  begin
    writeln('Enter date in DD MM format');
    readln(day[num] , month[num] );
    writeln('Enter sale amount');
    readln(sale[num] );
    num := num + 1;
    writeln('');
    writeln('Enter seller number. To stop enter "0"');
    readln(sellernr[num] );
  end;
  writeln('Seller Nr.':10, 'Date':14, 'Amount':16);
  for count := 0 to num-1 do
  begin
    writeln(sellernr[count], day[count]:20,'/',month[count], sale[count]:14:2);
  end;
  writeln('');
  writeln('ENTER to stop');
  readln();
end.   

So as you see, the program asks for the seller number, then date in DD MM format and then the amount of the sale.

It the prints out to the screen.

What I have to do next is provide the per quarter overview. I have to take each seller number which the user has defined in the array sellernr and add up the total sales per quarter.

That's where I have a problem. How do I take the values that are stored in the array and recognise when they are equal (ie when sellernr[x] and sellernr[y] are the same)

Say the seller numbers the user inputs are 10, 50, 100. I must then take only the array elements which correspond to, first, 10 and print out, for example:

Seller     Quarter1     Quarter2     Quarter3     Quarter4
10         $360.32      $567.21      $988.27      $1023.66

How can I take sellernr[x] which correspond to a particular user-defined value?

1 Answer 1

1

You can do something like this:

program Verkopers;
var
  SalesByQuarters:array[1..4]of real;//here you will store the sums
  ReporSeller:integer; //this will be the nr. of the seller for report
begin    
  ...//append your code with this
  writeln('Write seller number for report: ');
  readln(ReporSeller);
  for count:=1 to 4 do
    SalesByQuarters[count]:=0;//initialize the output variables for each quarter
  for count:= Low(sellernr) to High(sellernr) do
    begin
      if sellernr[count]=ReporSeller then //count will store the proper index
      SalesByQuarters[((month[count]-1) div 3) +1]:=
      SalesByQuarters[((month[count]-1) div 3) +1]+sale[count];
    end; //((month[count]-1) div 3) +1 gives us the number of quarter.
         // You can get it in a different way (if/case/etc)
  write('Report for seller ');
  writeln(ReporSeller);

  for count:=1 to 4 do
  begin
    write('Quarter ');
    writeln(count);
    writeln(SalesByQuarters[count]:14:2); //output the stored sums
  end;

  readln;
end.

You can also beautify the output by something like this:

  writeln('Seller     Quarter1     Quarter2     Quarter3     Quarter4');
  writeln(ReporSeller:6,
    '     $', SalesByQuarters[1]:7:2,
    '     $', SalesByQuarters[2]:7:2,
    '     $', SalesByQuarters[3]:7:2,
    '     $', SalesByQuarters[4]:7:2);
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much for this reply. It's a nice way of doing it. Is there any way of automating it so that the user doesn't have to input the seller number at the end and it just prints out a quarterly overview of every seller number that was input?
@DavidNugent Yes. You can can, of course. For example store the distinct seller nr in an array or set and then parse it.
@DavidNugent Should the post has answered your current question you can close the question by accepting the answer. You can ask a separate question if you need to know how to select distinct IDs from an array to request the community to give you a new algorithm for this. But specify the following detail. Are the ID nrs<=255 or not. If they are less then the simpliest decision will be with set using.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.