A simple program for pros..IM STUCK!

[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

  • [u]A plus B... again[/u]
    I
  • : [u]A plus B... again[/u]
    : I
  • :
    : 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]

  • : : [u]A plus B... again[/u]
    : : I
  • 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.
  • : 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?


  • : : 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.
  • : : : 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?

  • : 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.
  • : : 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]
  • Did this work in yours? I opened it said unknown identifier in:
    [code]for i := 255 downto 1 do begin
    [/code]

    so then i add the varible "i" in the var section:
    [code]x, y, z , 1 : Byte ;
    [/code]

    then it worked so then i tried entering to numbers 2 and 3 buh it didnt give the answer. So whats wrong now?
  • I did not try to make it work. I was just trying to suggest a strategy. Yes, you do need to declare [b]i[/b]. Another bug is that the strings need to be front padded with [b]ZERO[/b]s, not [b]BLANK[/b]s. A very subtle bug is that as the program builds the string [b]Sum[/b] the program has no awareness of what is going on and does not really know the length of [b]Sum[/b]. I've cured this by initializing [b]Sum[/b] to a null string and adding one character at a time via the concatenation operator [b]+[/b], which keeps the program updated as to the length of the string. The program now works on my machine.
    [code]
    Program BigSum ;

    CONST
    ZERO = '0' ;

    Var
    a, b, Sum : String ;
    i,
    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 := ZERO + a ;
    while Length(b) < 255 do
    b := ZERO + b ;
    {
    add from left to right
    }
    Sum := '' ;
    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 := Chr(Ord(ZERO) + z) + Sum
    end ;
    {
    left justify
    }
    while Sum[1] = ZERO do
    Delete(sum,1,1) ;
    {
    print the result
    }
    WriteLn('The sum is ',sum)
    end.
    [/code]
    The program still does not know what to do with a minus sign (or a plus sign for that matter) and will not work with negative numbers. I've tried to make it work with negative numbers but the code quickly gets tricky. I think the algorithm can be made to work with negative numbers but it needs considerable modification.

    Good luck.

    Actor

  • : The program still does not know what to do with a minus sign (or a
    : plus sign for that matter) and will not work with negative numbers.
    : I've tried to make it work with negative numbers but the code
    : quickly gets tricky. I think the algorithm can be made to work with
    : negative numbers but it needs considerable modification.
    :
    : Good luck.
    :
    : Actor
    :
    :
    [color=Green]Thanks buddy the program worked but got some test cases right and most wrong this is what it said:

    "Integers don't have to be positive ;)

    Also you seem to have a limit of 255 - but as I said above the strings might be up to 100,000 characters long. (I know this won't work for Pascal strings; you have to read() into a char array.)"

    and by the way im really thankfull to you even if i dont get this right thanks alot for helping me! :)
    [/color]
  • : : The program still does not know what to do with a minus sign (or a
    : : plus sign for that matter) and will not work with negative numbers.
    : : I've tried to make it work with negative numbers but the code
    : : quickly gets tricky. I think the algorithm can be made to work with
    : : negative numbers but it needs considerable modification.
    : :
    : : Good luck.
    : :
    : : Actor
    : :
    : :
    : [color=Green]Thanks buddy the program worked but got some test cases
    : right and most wrong this is what it said:
    :
    : "Integers don't have to be positive ;)
    :
    : Also you seem to have a limit of 255 - but as I said above the
    : strings might be up to 100,000 characters long. (I know this won't
    : work for Pascal strings; you have to read() into a char array.)"
    :
    : and by the way im really thankfull to you even if i dont get this
    : right thanks alot for helping me! :)
    : [/color]
    [color=Green]
    so any idea what i should do now?[/color]

  • : : [color=Green]Thanks buddy the program worked but got some test cases
    : : right and most wrong this is what it said:
    : :
    : : "Integers don't have to be positive ;)
    : :
    : : Also you seem to have a limit of 255 - but as I said above the
    : : strings might be up to 100,000 characters long. (I know this won't
    : : work for Pascal strings; you have to read() into a char array.)"
    : :
    : : and by the way im really thankfull to you even if i dont get this
    : : right thanks alot for helping me! :)
    : : [/color]
    : [color=Green]
    : so any idea what i should do now?[/color]
    :
    :
    What were the test cases?

    You said that there was a hint that the solution uses string and arrays. If it uses strings then a limit of 255 characters is probably acceptable. To go beyond that I'd use a linked list instead of strings (or arrays). Are you comfortable with pointers and linked lists?

    I'll see what I can come up with as far as handling negative integers.

    Take care.

    Actor
Sign In or Register to comment.

Howdy, Stranger!

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

Categories