Recent

Author Topic: need help with the output in my program  (Read 1800 times)

whitehat

  • Jr. Member
  • **
  • Posts: 93
need help with the output in my program
« on: May 22, 2018, 02:30:48 pm »
hello i have a problem when the program show me the result of the occurrence of a number
i want to count the occurence of a number in a table
t[1]=1231451 the result of occ:='lentier '+ch[k]+'aparaitre '+ch1+'fois';: lentier 1 aparaitre 3 fois
t[2]= 00054780 => lentier 0 aparaitre 4 fois
between we just use the library wincrt we don't study the other library


Code: Pascal  [Select][+][-]
  1. program ex;
  2. uses wincrt;
  3. type
  4. tab=array[1..20] of integer;
  5. var
  6. t:tab;
  7. n:integer;
  8.  
  9. function occ (x:integer):string;
  10. var
  11. k,nb,i:integer;
  12. ch,ch1:string;
  13. begin
  14. str(x,ch);
  15. k:=1;
  16. nb:=0;
  17. for i:=1 to length(ch) do
  18. begin
  19. if pos(ch[k],ch)>0 then
  20. nb:=nb+1;
  21. delete(ch,pos(ch[k],ch),1);
  22. k:=k+1;
  23. end;
  24. str(nb,ch1);
  25. occ:='lentier '+ch[k]+'aparaitre '+ch1+'fois';
  26. end;
  27.  
  28. procedure remplire (var t:tab;var n:integer);
  29. var
  30. i:integer;
  31. begin
  32. repeat
  33. readln(n);
  34. until n in [1..20];
  35. for i:=1 to n do
  36. begin
  37. writeln('t[',i,']');
  38. readln(t[i]);
  39. end;
  40. for i:=1 to n do
  41. begin
  42. writeln(occ(t[i]));
  43. end;
  44. end;
  45. begin
  46. remplire(t,n);
  47. end.
  48.  
  49.  

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: need help with the output in my program
« Reply #1 on: May 22, 2018, 03:00:12 pm »
Homework assignment I guess.

Could you please use proper indentation for your code, it makes it a lot easier to read.

Do you have to list the occurence of each "lentier" in a given number?

t[1]=1231451:  lentier 1 aparaitre 3 fois, lentier 2 aparaitre 1 fois, lentier 3 aparaitre 1 fois, lentier 4 aparaitre 1 fois, lentier 5 aparaitre 1 fois?

I would write a function that counts "lentier" in a given number:
Code: Pascal  [Select][+][-]
  1. function CountLentier(Lentier: Char; Number: Integer): Integer;
  2. begin
  3.   //your code here
  4.   //Initialize function result to be zero
  5.   //convert Number to a string, then loop over all characters in the string and increase Result if character equals "Lentier"
  6. end;

Then I would write a procedure that outputs the count of all "Lentiers" ('0'to '9') in a given number:

Code: Pascal  [Select][+][-]
  1. procedure OutputLentiers(Number: Integer);
  2. begin
  3.   //use a loop to get the count for each "Lentier", only write them out if the count is not zero
  4. end;

Then I would write a procedure that does the output for an array of integers, calling OutputLentiers for each integer in the array.

B.t.w. you do not need Delete() in any of this code.

Bart
« Last Edit: May 22, 2018, 03:02:19 pm by Bart »

whitehat

  • Jr. Member
  • **
  • Posts: 93
Re: need help with the output in my program
« Reply #2 on: May 22, 2018, 03:09:42 pm »
how can i not need the delete() ? if i don't delete the number then the output of the occurence will be not correct

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: need help with the output in my program
« Reply #3 on: May 22, 2018, 03:27:46 pm »
Say the number (as a string variable S) is '112991'.
Fist we find the count for '1':  Set Count to 0, S[1] is '1', so increase Count to 1, S[2] is '1', increase Count to 2, S[3] S[4] and S[5] are not '1', so do nothing, finally S[5] is '1', so increase Count to 3, which is the final answer, because there are no more characters left.
Now I can do the same for '1' to '9' (and of course for '0').

As you see, I do not need to delete any character in S as we go.
(Delete is a relatively slow operation, looping over the string is fast.)

You could even have a function that returns all counts in one go:
  • The function will return an array (defined as array['0'..'9'] of integer).
  • Initialize the array to have all values to be 0
  • Loop over the string and increment the proper array value
Just one loop over the string needed.

While it would be trivial to give you this code, I won't, since this is your homework.

Bart

whitehat

  • Jr. Member
  • **
  • Posts: 93
Re: need help with the output in my program
« Reply #4 on: May 22, 2018, 03:37:51 pm »
thank you for the idea

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: need help with the output in my program
« Reply #5 on: May 25, 2018, 11:14:00 pm »
Well, deadline seems to have past ...

Bart

 

TinyPortal © 2005-2018