Jump to content

Pascal Coding Help Please!

- - - - -

  • Please log in to reply
3 replies to this topic

#1
MrAnderson

MrAnderson

    Newbie

  • Members
  • Pip
  • 8 posts
Hi Guys, This is my first forum post and I need help with a pascal programming assignment.
This is the first part of the programming that I'm working on.

Ignore the excessive comments. I placed all those comments because i need to break it into modules once the rest of the program is developed.
Program Expenditure;

uses

    crt;

Var

   Terminate : Integer; {The 'Terminate' variable, tests if data entry should be terminated}

   CustomerID: Integer; {The 'CustomerID' variable is used to index and store data in multiple arrays}


   CustomerNames : array[1..10] of string; {Stores the names of customers}

   ExpenditureAmt: array[1..10] of Real;{Stores the Expenditure of respective customers}


   CheckGreatestAmt : Real; {Records the largest expenditure amount for the second module}


   ExpenditureLoop: Integer; {This will control the flow of the loop in the second module}

Begin

{Program must be split into modules, this will act as the first module. Looping the Data Entry Process}

         Terminate := 1;

         CustomerID:= 0;


         while Terminate <> 0 do

               begin

                    CustomerID := CustomerID  + 1;

                    Writeln ('Input the First And Last Name of the Customer: ');

                    Readln (CustomerNames[CustomerID]);

                    Writeln ('Input the Expenditure of ' , CustomerNames[CustomerID] , ':');

                    Readln (ExpenditureAmt[CustomerID]);


                    Writeln ('To Terminate Data Entry Press 0 or Press Any Other Number to Continue: ');

                    Readln (Terminate);

                    ClrScr;

                    End;

{The End of the Data Entry Phase of the Program Completed}


{Second Module, This will find the customer with the greatest expenditure and also the corresponding name}

        CheckGreatestAmt := 0;

        for ExpenditureLoop := 1 to CustomerID do

            Begin

                 if ExpenditureAmt[ExpenditureLoop] > CheckGreatestAmt then

                 CheckGreatestAmt := ExpenditureAmt[ExpenditureLoop];

            End;

        Writeln ('The Largest Expenditure Made was ' , CheckGreatestAmt:2);

        Writeln ('This was made by 'CustomerNames[ExpenditureLoop]);

{Second Module Ends Here.}


Readln();

End.
The program accepts the following user input - name of customer and the respective expenditure. Then it will find the largest expenditure and then find the corresponding name of the customer. This is where the problem occurs. The largrest expenditure is found by running a For Loop in the array "ExpenditureAmt". When the largest is found i need a way to find the Customer which spent that Amount.
My other problem is the output format .
How can i get rid of output like this 1.0E+001?

Thanks in advance for your help!

#2
Zorfox

Zorfox

    Learning Programmer

  • Members
  • PipPipPip
  • 36 posts
I have never dealt with CRT applications so forgive my ignorance. The first problem is with your formatting. You are passing CheckGreatestAmt as real. The syntax for real is: ('The Largest Expenditure Made was ' , CheckGreatestAmt:2:2); ie 2 int and 2 decimal places.

The next problem is finding the customer name that made the expenditure. Add an additional integer variable X in the code below. Assign your ExpenditureLoop variable the value of X. If you don't understand ANY part of that ask!
Hope that helps. Enjoy your weekend!

program Project2;


{$APPTYPE CONSOLE}


uses

  SysUtils;


Var

   Terminate : Integer; {The 'Terminate' variable, tests if data entry should be terminated}

   CustomerID: Integer; {The 'CustomerID' variable is used to index and store data in multiple arrays}


   CustomerNames : array[1..10] of string; {Stores the names of customers}

   ExpenditureAmt: array[1..10] of Real;{Stores the Expenditure of respective customers}


   CheckGreatestAmt : Real; {Records the largest expenditure amount for the second module}

   X: Integer;

   ExpenditureLoop: Integer; {This will control the flow of the loop in the second module}

Begin

{Program must be split into modules, this will act as the first module. Looping the Data Entry Process}

         Terminate := 1;

         CustomerID:= 0;


         while Terminate <> 0 do

               begin

                    CustomerID := CustomerID  + 1;

                    Writeln ('Input the First And Last Name of the Customer: ');

                    Readln (CustomerNames[CustomerID]);

                    Writeln ('Input the Expenditure of ' , CustomerNames[CustomerID] , ':');

                    Readln (ExpenditureAmt[CustomerID]);


                    Writeln ('To Terminate Data Entry Press 0 or Press Any Other Number to Continue: ');

                    Readln (Terminate);

                    ClrScr;

                    End;

{The End of the Data Entry Phase of the Program Completed}


{Second Module, This will find the customer with the greatest expenditure and also the corresponding name}

        CheckGreatestAmt := 0;

        for X := 1 to CustomerID do

            Begin

              ExpenditureLoop := X;

              if ExpenditureAmt[X] > CheckGreatestAmt then

                 CheckGreatestAmt := ExpenditureAmt[ExpenditureLoop];

            End;

        Writeln ('The Largest Expenditure Made was ' , CheckGreatestAmt:1:2);

        Writeln ('This was made by ', CustomerNames[ExpenditureLoop]);

{Second Module Ends Here.}


#3
MrAnderson

MrAnderson

    Newbie

  • Members
  • Pip
  • 8 posts
Thanks for the help!
The problem with formatting the text is solved. It gives an answer with the Expotential (E+001)
Thanks!
But the problem with finding the customer who spent that amount still gives the problem.

The problem outputs the last name name entered and not the largest. I have an idea. I will try it and post back!
Thanks alot!

#4
MrAnderson

MrAnderson

    Newbie

  • Members
  • Pip
  • 8 posts
I got it out bro ....Thanks for all your help!
Program Expenditure;

uses

    crt;

Var

   Terminate : Integer; {The 'Terminate' variable, tests if data entry should be terminated}

   CustomerID: Integer; {The 'CustomerID' variable is used to index and store data in multiple arrays}


   CustomerNames : array[1..10] of string; {Stores the names of customers}

   ExpenditureAmt: array[1..10] of Real;{Stores the Expenditure of respective customers}


   CheckGreatestAmt : Real; {Records the largest expenditure amount for the second module}

   CheckGreatestName: Integer;{Records the Name with highest Value}


   ExpenditureLoop: Integer; {This will control the flow of the loop in the second module}

Begin

{Program must be split into modules, this will act as the first module. Looping the Data Entry Process}

         Terminate := 1;

         CustomerID:= 0;


         while Terminate <> 0 do

               begin

                    CustomerID := CustomerID  + 1;

                    Writeln ('Input the First And Last Name of the Customer: ');

                    Readln (CustomerNames[CustomerID]);

                    Writeln ('Input the Expenditure of ' , CustomerNames[CustomerID] , ':');

                    Readln (ExpenditureAmt[CustomerID]);


                    Writeln ('To Terminate Data Entry Press 0 or Press Any Other Number to Continue: ');

                    Readln (Terminate);

                    ClrScr;

                    End;

{The End of the Data Entry Phase of the Program Completed}


{Second Module, This will find the customer with the greatest expenditure and also the corresponding name}

        CheckGreatestAmt := 0;

        for ExpenditureLoop := 1 to CustomerID do

        Begin

             if ExpenditureAmt[ExpenditureLoop] > CheckGreatestAmt then

                Begin

                    CheckGreatestAmt := ExpenditureAmt[ExpenditureLoop];

                    CheckGreatestName:= ExpenditureLoop;

                End;

        End;


        writeln('The Largest Expenditure Made was ' , CheckGreatestAmt:1:2);

        writeln('This was made by ', CustomerNames[CheckGreatestName]);

{Second Module Ends Here.}



Readln();

End.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users