Recent

Author Topic: Can someone help me with my program  (Read 3435 times)

shivan

  • Newbie
  • Posts: 3
Can someone help me with my program
« on: February 15, 2018, 01:00:12 am »
This is the first program that i have to do for school and i need some help. (also just joined and my first post). we needed to make a program that stores names and points generated and store it in an array. it will also have to be able to sort the points in the array from the highest to the lowest

Here is my program so far. Please help.

Code: Pascal  [Select][+][-]
  1. Program CarPoints (Input,output);
  2.  
  3. const
  4. Max=25;
  5. StringMax=26 ;
  6. VAR
  7. branchname,Salper,name:String[25];
  8. class1,class2,class3,ttlpts:Integer;
  9. I: Integer;
  10.  
  11. SalesArr:Array[1..max] of string;
  12. TotalPts:Array[1..max] of integer ;
  13. Begin
  14.       For i:=1 to max do
  15.       begin
  16.        SalesArr[i]:='';
  17.        TotalPts[i]:=0;
  18.        End;
  19. Begin
  20.      For i:=1 to max do
  21.  
  22.  
  23. Begin
  24.       Writeln('enter salper name');
  25.       Readln(Salper);
  26.       Writeln('Enter Branch name');
  27.       Readln(branchname);
  28.       Writeln('enter number of class 1 vehicles sold');
  29.       Readln(class1);
  30.       Writeln('enter number of class 2 vehicles sold');
  31.       Readln(class2);
  32.       Writeln('enter number of class 3 vehicles sold');
  33.       Readln(class3);
  34.       ttlpts :=(class1 * 1)+(class2 * 3)+(class3 * 5);
  35.       Writeln(ttlpts);
  36.       SalesArr[i]:=Salper ;
  37.        TotalPts[i]:=ttlpts;
  38.  
  39.       if (Salper = 'end')     then
  40.       'display''results'
  41. (Else)
  42. begin
  43.      ('repeat until name'='end');
  44.     end;
  45.  
  46.    End;
  47.  
  48. Begin
  49.       Writeln('Search array for highest location');
  50.       Writeln('locate salesperosn with highest point');
  51.       Writeln('display salesperson with highest point name to user');
  52.       Read ln('salesperosn name');
  53.       End;
  54.  
  55. end;
  56.  
  57. END.
  58.  
« Last Edit: February 15, 2018, 01:39:54 am by shivan »

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Can someone help me with my program
« Reply #1 on: February 15, 2018, 01:08:42 am »
Hi shivan. Welcome to these forums.

Could you edit your first post and place your code between code tags ? Right now your code is unreadable and error-prone because the forums software is bel to "eat"characters when displaying code without code-tags.

Secondly... i must have missed it... i failed to locate an actual question other then help... with what you exactly require help was not clear from your question.

In order to be able for someone to give some help, you must formulate a concrete question. In doing so you will aid yourself as well as formulating an actual question will make you think about what your problem actually is (which will also be able to help better understand your own code).

shivan

  • Newbie
  • Posts: 3
Re: Can someone help me with my program
« Reply #2 on: February 15, 2018, 01:16:34 am »
I don't understand what you mean by code between tags.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Can someone help me with my program
« Reply #3 on: February 15, 2018, 01:21:25 am »
I don't understand what you mean by code between tags.

with code-tags i meant: at the top of your code place the word code between square brackets and at the end of your code the word /code (also between squared brackets). See also What is the correct way to ask questions in the forum?

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Can someone help me with my program
« Reply #4 on: February 15, 2018, 01:30:33 am »
Let me show you:

Code: Pascal  [Select][+][-]
  1. Program CarPoints (Input,output);
  2.  
  3. const
  4. Max=25;
  5. StringMax=26 ;
  6. VAR
  7. branchname,Salper,name:String[25];
  8. class1,class2,class3,ttlpts:Integer;
  9. I: Integer;
  10.  
  11. SalesArr:Array[1..max] of string;
  12. TotalPts:Array[1..max] of integer ;
  13. Begin
  14.       For i:=1 to max do
  15.       begin
  16.        SalesArr[i]:='';
  17.        TotalPts[i]:=0;
  18.        End;
  19. Begin
  20.      For i:=1 to max do
  21.  
  22.  
  23. Begin
  24.       Writeln('enter salper name');
  25.       Readln(Salper);
  26.       Writeln('Enter Branch name');
  27.       Readln(branchname);
  28.       Writeln('enter number of class 1 vehicles sold');
  29.       Readln(class1);
  30.       Writeln('enter number of class 2 vehicles sold');
  31.       Readln(class2);
  32.       Writeln('enter number of class 3 vehicles sold');
  33.       Readln(class3);
  34.       ttlpts :=(class1 * 1)+(class2 * 3)+(class3 * 5);
  35.       Writeln(ttlpts);
  36.       SalesArr[i]:=Salper ;
  37.        TotalPts[i]:=ttlpts;
  38.  
  39.       if (Salper = 'end')     then
  40.       'display''results'
  41. (Else)
  42. begin
  43.      ('repeat until name'='end');
  44.     end;
  45.  
  46.    End;
  47.  
  48. Begin
  49.       Writeln('Search array for highest location');
  50.       Writeln('locate salesperosn with highest point');
  51.       Writeln('display salesperson with highest point name to user');
  52.       Read ln('salesperosn name');
  53.       End;
  54.  
  55. end;
  56.  
  57. END.
  58.  

If you'll quote this post, you'll see the actual tags in the message you are editing so you can copy-paste if you want. Note the differences between your original code and the code placed between code-tags. That is why i asked: in order to avoid confusion caused by the forum software.

shivan

  • Newbie
  • Posts: 3
Re: Can someone help me with my program
« Reply #5 on: February 15, 2018, 01:40:23 am »
thanks..I am kind of a noob at this

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Can someone help me with my program
« Reply #6 on: February 15, 2018, 01:44:24 am »
thanks..I am kind of a noob at this
No problem. We all were at some point in time  :)

As for your questions. Always make sure you describe the problem, what you are trying to achieve and what you've tried to solve your problem.

If it concerns a school related assignment then always make sure to exactly copy the assignment for us to read. Perhaps you have been given some restrictions or other specific instructions (which might be important for us to know).

Do realize though that school assignments never been given a copy-paste answer, rather people usually try to guide you into the right direction. Only if you are really stuck on constructs someone might have pity on you and show how it should be done.

Quote
Writeln('Search array for highest location');
Let's say you have a piece of paper. On that paper you have drawn a table/list with a total of max values. If you would need to determine the location of the highest value in that table/list then how would you do that without using a computer (using a pen and paper only) ?

Quote
Writeln('locate salesperosn with highest point');
The salesperson with the highest number of points in sales is the one what has the highest number. see also answer given above.

Quote
Writeln('display salesperson with highest point name to user');
If you keep your arrays in sync then you already have to location inside the array. you would only have to wrote out the corresponding name.
« Last Edit: February 15, 2018, 01:46:03 am by molly »

jamie

  • Hero Member
  • *****
  • Posts: 6130
Re: Can someone help me with my program
« Reply #7 on: February 15, 2018, 02:58:30 am »
When pasting code here so that it shows correctly in the same manner as you would see it in your
code editor:

 Look up in the options for the icon "#", click on that and it will insert a couple of bracketed start
and end frames [ code = pascal ]
Quote
Place your cursor here
  [ /code ]

make sure there isn't any spaces after the [  and before the ] in the frames as this will cause the text
here to get distorted  :D

The only true wisdom is knowing you know nothing

egsuh

  • Hero Member
  • *****
  • Posts: 1290
Re: Can someone help me with my program
« Reply #8 on: February 15, 2018, 03:55:58 am »
I don't know exactly what language you are using (classic pascal, delphi, object pascal, ..etc.), but just to comment on the logic:


1) Initializing the array : looks fine, but mind whether there would be exactly 25 data or may be less.

Code: Pascal  [Select][+][-]
  1.       For i:=1 to max do
  2.       begin
  3.        SalesArr[i]:='';
  4.        TotalPts[i]:=0;
  5.        End;

2) Then data input part. There are many ways to input data, but if we follow your approach, you should use one of your options.

Option 1: Using for loop, but end when you enter 'end' to var Salper 

Code: Pascal  [Select][+][-]
  1.     n := 0;   // let's add N to store the number of salespersons
  2.      For i:=1 to max do
  3.      Begin
  4.         Writeln('enter salper name');
  5.         Readln(Salper);
  6.  
  7.         if Salper = 'end'  Break; // this jumps out of for loop
  8.    
  9.         Writeln('Enter Branch name');
  10.         Readln(branchname);
  11.         Writeln('enter number of class 1 vehicles sold');
  12.         Readln(class1);
  13.         Writeln('enter number of class 2 vehicles sold');
  14.         Readln(class2);
  15.         Writeln('enter number of class 3 vehicles sold');
  16.         Readln(class3);
  17.         ttlpts :=(class1 * 1)+(class2 * 3)+(class3 * 5);
  18.         Writeln(ttlpts);
  19.         SalesArr[i]:=Salper ;
  20.         TotalPts[i]:=ttlpts;
  21.     end;
  22.     if i < max then n := i-1 { because we jumped out of loop, n is one less than i }
  23.                    else n := i;

Option 2: Use whild loop until you enter "end" for name. You have to think over the logic behind this.

Code: Pascal  [Select][+][-]
  1.    n := 0;
  2.     Writeln('enter salper name');  
  3.     Readln(Salper);                  // you may enter 'end' at the first input.
  4.  
  5.     while (Salper <> 'end') {and i < Max)} do begin   // not sure whether to put i < max condition
  6.          inc(n);  
  7.         Writeln('Enter Branch name');
  8.         Readln(branchname);
  9.         Writeln('enter number of class 1 vehicles sold');
  10.         Readln(class1);
  11.         Writeln('enter number of class 2 vehicles sold');
  12.         Readln(class2);
  13.         Writeln('enter number of class 3 vehicles sold');
  14.         Readln(class3);
  15.         ttlpts :=(class1 * 1)+(class2 * 3)+(class3 * 5);
  16.         Writeln(ttlpts);
  17.         SalesArr[n]:=Salper ;
  18.         TotalPts[n]:=ttlpts;
  19.  
  20.         Writeln('enter salper name');
  21.         Readln(Salper);
  22.     end;
  23.  
           
 
Then data input is done.


3)  Sorting by the points

   There are a few sorting algorithms, but I use the most basic option .. comparing all possible combinations.

Code: Pascal  [Select][+][-]
  1.      // Two temporal variables must have been declared:  tname:string; ttotalpts: integer;
  2.      
  3.  
  4.      for i := 1 to n-1 do begin
  5.          for j := i+1 to n do begin
  6.               if TotalPts[i] < TotalPts[j] then    // you'd like to put highest score first
  7.               begin  
  8.                  tTotalPts := TotalPts[i];        
  9.                  TotalPts[j] := TotalPts[i];
  10.                  TotalPts[i] := tTotalPts;
  11.  
  12.                  tname := SalesArr[i];              // Exchange SalesArr as well
  13.                  SalesArr[i] := SalesArr[j];
  14.                  SalesArr[j] := tname;
  15.                end;  // if.. .then block
  16.             end; // for j
  17.           end;  // for i  


Now you have "sorted" arrays. SalesArr[1] is the name who has the highest points, and TotalPts[1] is the highest score.

In actuality almost no one would write this kinds of code, but you may think over the codes what each line means. I haven't tested it myself. I just want to show you the logic behind solving this problem.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Can someone help me with my program
« Reply #9 on: February 15, 2018, 04:03:35 am »
Nice egsuh.

A question for you as well:
Now you have "sorted" arrays. SalesArr[1] is the name who has the highest points, and TotalPts[1] is the highest score.
Why did you choose to sort the array ? is it necessary ?

if you have a list of 5 numbers:
#1 = 2
#2 = 5
#3 = 100
#4 = 19
#5 = 1

and you would have to determine the location of the highest number (#3 in this case), then how would you go about that ? Mind that you have sorted your array so the position (#3, or 2 when addressing it as index) is lost during sorting.  ;D

egsuh

  • Hero Member
  • *****
  • Posts: 1290
Re: Can someone help me with my program
« Reply #10 on: February 17, 2018, 11:45:51 pm »
Quote
Why did you choose to sort the array ? is it necessary ?

if you have a list of 5 numbers:
#1 = 2
#2 = 5
#3 = 100
#4 = 19
#5 = 1

and you would have to determine the location of the highest number (#3 in this case), then how would you go about that ?

Haha.. probably I've gone too far. If finding the highest score is the only objective, sorting is not necessary.

 

TinyPortal © 2005-2018