Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

A simple program for pros..IM STUCK!

JP161470JP161470 Posts: 26Member
[u]A plus B... again[/u]
Given two integers A and B, find their exact sum.

This time there are no limitations on A and B (of course, they will fit in memory).

[u]Primes[/u]
Given an integer N <= 100, output the first N primes, one per line.

Sample Input:
4

Sample Output:
2
3
5
7

THX really stuck on this questions but i no they are easy but im a beginner so cant seem to get it right.
«1

Comments

  • ActorActor Posts: 438Member
    [u]A plus B... again[/u]
    I
  • JP161470JP161470 Posts: 26Member
    : [u]A plus B... again[/u]
    : I
  • ActorActor Posts: 438Member
    :
    : so what would the final "prime" question look like...the whole code?
    : sry really new to this so got all confused what you said would be
    : easier to see if the whole code was there at once. And for the "a
    : plus b 2" thats all they have give but this doesnt work:
    :
    : [code]: readln(a,b);
    : writeln(a+b);[/code]:
    [blue]
    It should work. It does work on my computer. The problem may be with the way you're entering the data. If you enter [b]a[/b] and [b]b[/b] on the same line you need to separate them with a space. It's kind of intuitive to separate them with a comma but if you do the program will see the comma as your input for [b]b[/b], and since a comma is not an integer you get a run time error.

    Another way to enter the data is to enter the first number and hit ENTER, then enter the second number and hit ENTER again.
    [/blue]

  • ActorActor Posts: 438Member
    : : [u]A plus B... again[/u]
    : : I
  • JP161470JP161470 Posts: 26Member
    KK i got the prime one thx but in a plus b this code doesnt work:

    [u][b]var
    a,b:integer;
    begin
    readln(a);
    readln(b);
    writeln(a+b);
    end.[/b][/u]

    THE HINT WAS THAT IT uses string and arrays somehow.
  • ActorActor Posts: 438Member
    : KK i got the prime one thx but in a plus b this code doesnt work:
    :
    : [u][b]var
    : a,b:integer;
    : begin
    : readln(a);
    : readln(b);
    : writeln(a+b);
    : end.[/b][/u]
    :
    : THE HINT WAS THAT IT uses string and arrays somehow.
    :
    The code you have posted should work.

    Exactly how does in not work? Does it compile? Do you get a run-time error?

    I don't see how strings and arrays enter into it. What is the exact wording or the problem?


  • JP161470JP161470 Posts: 26Member
    : : KK i got the prime one thx but in a plus b this code doesnt work:
    : :
    : : [u][b]var
    : : a,b:integer;
    : : begin
    : : readln(a);
    : : readln(b);
    : : writeln(a+b);
    : : end.[/b][/u]
    : :
    : : THE HINT WAS THAT IT uses string and arrays somehow.
    : :
    : The code you have posted should work.
    :
    : Exactly how does in not work? Does it compile? Do you get a
    : run-time error?
    :
    : I don't see how strings and arrays enter into it. What is the exact
    : wording or the problem?
    :
    -----------------------------------------------------------------------

    the question i sent was copied and pasted so its the exactly worded and we did two questions. the first one it was the code i put before but this second time it says there is no limitation on A and B.
  • ActorActor Posts: 438Member
    : : : KK i got the prime one thx but in a plus b this code doesnt work:
    : : :
    : : : [u][b]var
    : : : a,b:integer;
    : : : begin
    : : : readln(a);
    : : : readln(b);
    : : : writeln(a+b);
    : : : end.[/b][/u]
    : : :
    : : : THE HINT WAS THAT IT uses string and arrays somehow.
    : : :
    : : The code you have posted should work.
    : :
    : : Exactly how does in not work? Does it compile? Do you get a
    : : run-time error?
    : :
    : : I don't see how strings and arrays enter into it. What is the exact
    : : wording or the problem?
    : :
    : ---------------------------------------------------------------------
    : --
    :
    : the question i sent was copied and pasted so its the exactly worded
    : and we did two questions. the first one it was the code i put before
    : but this second time it says there is no limitation on A and B.
    :
    Ok. It says "this time." What was it the previous time?

  • JP161470JP161470 Posts: 26Member
    : Ok. It says "this time." What was it the previous time?
    :
    :=========================================================================

    Ok the first time it was "a plus b"

    and the answer to that was:

    [code]var
    a,b:integer;
    begin
    readln(a);
    readln(b);
    writeln(a+b);
    end.[/code]

    and then the second one we got was "a plus b..again" and it had the same instruction but it said that the variables "a" and "b" had no limitations on how many numbers.
  • ActorActor Posts: 438Member
    : : Ok. It says "this time." What was it the previous time?
    : :
    : :====================================================================
    : =====
    :
    : Ok the first time it was "a plus b"
    :
    : and the answer to that was:
    :
    : [code]: var
    : a,b:integer;
    : begin
    : readln(a);
    : readln(b);
    : writeln(a+b);
    : end.[/code]:
    :
    : and then the second one we got was "a plus b..again" and it had the
    : same instruction but it said that the variables "a" and "b" had no
    : limitations on how many numbers.
    :
    [blue]
    Ok. Now it makes sense. In the original [b]a[/b] and [b]b[/b] were integers and the maximum value of an integer is 32767. What your second assignment wants is to surmount that limitation.

    One approach is to store [b]a[/b] and [b]b[/b] as strings. The sum is stored in a third string. In Turbo Pascal (my compiler) a string has a maximum length of 255 characters.
    [/blue]
    [code]
    Program BigSum ;

    CONST
    BLANK = Chr(32) ;
    ZERO = '0' ;

    Var
    a, b, Sum : String ;
    x, y, z : Byte ;

    begin
    {
    data entry
    }
    Write ('Enter the first integer ') ;
    ReadLn (a) ;
    Write ('Enter the second integer ') ;
    ReadLn (b)
    {
    least significant digit is on the right so we right justify both
    }
    while Length(a) < 255 do
    a := BLANK + a ;
    while Length(b) < 255 do
    b := BLANK + b ;
    {
    add from left to right
    }
    for i := 255 downto 1 do begin
    x := Ord(a[i]) - Ord(ZERO) ;
    y := Ord(b[i]) - Ord(ZERO) ;
    z := x + y ;
    Sum[i] := Chr(Ord(ZERO) + z)
    end ;
    {
    left justify
    }
    while Sum[1] = BLANK do
    Delete(sum,1,1) ;
    {
    print the result
    }
    WriteLn(sum)
    end.
    [/code]
    [blue]
    I have not compiled and tested this but I hope it gives you an idea of the approach. As is, there are a couple of bugs in it. First, it will only work with positive integers. Second, when the sum of two digits is more than 9 it will not handle the carry. Handling the carry is fairly simple.
    [/blue]
    [code]
    Program BigSum ;

    CONST
    BLANK = Chr(32) ;
    ZERO = '0' ;

    Var
    a, b, Sum : String ;
    x, y, z : Byte ;
    Carry : Boolean ;

    begin
    Carry := FALSE ;
    {
    data entry
    }
    Write ('Enter the first integer ') ;
    ReadLn (a) ;
    Write ('Enter the second integer ') ;
    ReadLn (b)
    {
    least significant digit is on the right so we right justify both
    }
    while Length(a) < 255 do
    a := BLANK + a ;
    while Length(b) < 255 do
    b := BLANK + b ;
    {
    add from left to right
    }
    for i := 255 downto 1 do begin
    x := Ord(a[i]) - Ord(ZERO) ;
    y := Ord(b[i]) - Ord(ZERO) ;
    z := x + y ;

    if Carry then
    z := z + 1 ;

    if z < 10 then
    Carry := FALSE
    else begin
    Carry := TRUE ;
    z := z - 10
    end ;

    Sum[i] := Chr(Ord(ZERO) + z)
    end ;
    {
    left justify
    }
    while Sum[1] = BLANK do
    Delete(sum,1,1) ;
    {
    print the result
    }
    WriteLn(sum)
    end.
    [/code]
    [blue]
    There is no error checking here. The program will only work correctly if the input is valid, i.e., no non-numeric characters appear in the input.

    There is still a limit of course, but a series of 255 nines represents a very big number, more than the total number of electrons in the known universe or the number of nanoseconds since the big bang.
    [/blue]
«1
Sign In or Register to comment.