Velocity Reviews > C++ > Request for help

# Request for help

gpy5504@hotmail.com
Guest
Posts: n/a

 10-18-2011
1. Write a program which prompts the user to enter an integer between
1 and 9.
Based on the inputted number, the program outputs a triangular textual
shape using digits
(see below).
Sample Execution:
Enter N: 5
12345
1234
123
12
1

2.1. The Fibonacci sequence is a sequence of numbers beginning
1, 1, 2, 3, 5, 8, 13, 21, ...
The first two elements are defined to be 1. Each of the other elements
is the sum of its
two predecessors. Write a program that takes a value for N in the
range 1 to 30 and
displays the first N elements of the Fibonacci sequence.
Sample Execution:
Enter N: 9
Fibonacci sequence: 1 1 2 3 5 8 13 21 34

2. An integer P is called a prime number if the only positive integers
that divide P are
1 and P itself. Write a program which inputs a number N between 2 and
100 and then
loops through integers 2 to N and displays only those numbers which
are prime.
Sample Execution:
Enter N: 15
2 3 5 7 11 13

Ian Collins
Guest
Posts: n/a

 10-18-2011
On 10/18/11 06:55 PM, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 1. Write a program which prompts the user to enter an integer between
> 1 and 9.
> Based on the inputted number, the program outputs a triangular textual
> shape using digits
> (see below).
> Sample Execution:
> Enter N: 5
> 12345
> 1234
> 123
> 12
> 1
>
> 2.1. The Fibonacci sequence is a sequence of numbers beginning
> 1, 1, 2, 3, 5, 8, 13, 21, ...
> The first two elements are defined to be 1. Each of the other elements
> is the sum of its
> two predecessors. Write a program that takes a value for N in the
> range 1 to 30 and
> displays the first N elements of the Fibonacci sequence.
> Sample Execution:
> Enter N: 9
> Fibonacci sequence: 1 1 2 3 5 8 13 21 34
>
> 2. An integer P is called a prime number if the only positive integers
> that divide P are
> 1 and P itself. Write a program which inputs a number N between 2 and
> 100 and then
> loops through integers 2 to N and displays only those numbers which
> are prime.
> Sample Execution:
> Enter N: 15
> 2 3 5 7 11 13

Where's you code?

--
Ian Collins

gpy5504@hotmail.com
Guest
Posts: n/a

 10-18-2011
i need to write out the program by myself.
for question 3. i m writting the system as :
if (N_fibonacci == 1 )
current = previous1;

else if (N_fibonacci ==1)
current = previous2;

else {
counter= 3;
while (counter <= N fibonacci)
{
current = previous1 + previous2
previous1 = previous2
previous2 = current;
counter ++;
}

but the answer that program gave me is 1 3 4 5 1 5 3 7 4 and not the
answer that i want which is Fibonacci sequence: 1 1 2 3 5 8 13 21 34.
May i know what are the program.
and the other two questions i do not have any ideas to do the
programming ..

Juha Nieminen
Guest
Posts: n/a

 10-18-2011
(E-Mail Removed) wrote:
> i need to write out the program by myself.

Then why are you asking here?

Nick Keighley
Guest
Posts: n/a

 10-18-2011
On Oct 18, 6:55*am, (E-Mail Removed) wrote:
> 1. Write a program which prompts the user to enter an integer between
> 1 and 9.
> Based on the inputted number, the program outputs a triangular textual
> shape using digits
> (see below).
> Sample Execution:
> Enter N: 5
> 12345
> 1234
> 123
> 12
> 1

pseudo code for the problem

(define (print-row n)
(let loop ((i 1))
(if (not (zero? i))
(begin
(display i)
(loop (- i 1)) )))
(print #\newline) )

(define (print-triangle n)
(if (not (zero? n))
(print-row n)
(print-triangle (- n 1)) ))

> 2.1. The Fibonacci sequence is a sequence of numbers beginning
> 1, 1, 2, 3, 5, 8, 13, 21, ...
> The first two elements are defined to be 1. Each of the other elements
> is the sum of its
> two predecessors. Write a program that takes a value for N in the
> range 1 to 30 and
> displays the first N elements of the Fibonacci sequence.
> Sample Execution:
> Enter N: 9
> Fibonacci sequence: 1 1 2 3 5 8 13 21 34
>
> 2. An integer P is called a prime number if the only positive integers
> that divide P are
> 1 and P itself. Write a program which inputs a number N between 2 and
> 100 and then
> loops through integers 2 to N and displays only those numbers which
> are prime.
> Sample Execution:
> Enter N: 15
> 2 3 5 7 11 13

Nick Keighley
Guest
Posts: n/a

 10-18-2011
On Oct 18, 8:30*am, (E-Mail Removed) wrote:

> i need to write out the program by myself.

woop-i-doop what will the universities think of next!

When posting code to usenet (ie. here), post a
1. short
2. complete
3. compilable (unless you are having compilation problems)
program that illustrates your problem. You have not posted a complete
program and what you have posted is syntactically incorrect.

> for question 3. i m writting the system as :
> if (N_fibonacci == 1 )
> current = previous1;
>
> else if (N_fibonacci ==1)
> current = previous2;
>
> else {
> counter= 3;
> while (counter <= N fibonacci)
> {
> current = previous1 + previous2
> previous1 = previous2
> previous2 = current;
> counter ++;
>
> }

the layout is hard to follow. better (but still wrong) would have been
this:-

int main ()
{
if (N_fibonacci == 1 )
current = previous1;
else if (N_fibonacci == 1)
current = previous2;
else
{
counter = 3;
while (counter <= N fibonacci)
{
current = previous1 + previous2;
previous1 = previous2;
previous2 = current;
counter ++;
}
}
}

there is no output, none of the variables are defined and there is no
loop. You test if N_fibonacci is equal to 1 twice.

Get a piece of paper and draw 5 colums write "N_fibonacci" at the top
of the first column "previous1" at the head of the second column etc.
Put a question mark under each variable to show they are unitialised.
Then go through the program one line at time and update the columns as
appropriate. Ensure you and the progarm get the same answer. Put extra
printf(0s in to see values as they change. This should give you
insight.

Come back here if you still have problems. And don't post program
fragments again!

> but the answer that program gave me is 1 3 4 5 1 5 3 7 4 and not the
> answer that i want which is Fibonacci sequence: 1 1 2 3 5 8 13 21 34.
> May i know what are the program.
> and the other two questions i do not have any ideas to do *the
> programming ..

write a progarm to print the numbers 1 to 5

red floyd
Guest
Posts: n/a

 10-18-2011
On 10/17/2011 10:55 PM, (E-Mail Removed) wrote:
> [redacted]

http://www.parashift.com/c++-faq-lit...t.html#faq-5.2

Nick Keighley
Guest
Posts: n/a

 10-23-2011
On Oct 18, 10:51*am, Nick Keighley <(E-Mail Removed)>
wrote:
> On Oct 18, 6:55*am, (E-Mail Removed) wrote:
>
> > 1. Write a program which prompts the user to enter an integer between
> > 1 and 9.
> > Based on the inputted number, the program outputs a triangular textual
> > shape using digits
> > (see below).
> > Sample Execution:
> > Enter N: 5
> > 12345
> > 1234
> > 123
> > 12
> > 1

>
> pseudo code for the problem
>
> (define (print-row n)
> * * (let loop ((i 1))
> * * * * (if (not (zero? i))
> * * * * * * (begin
> * * * * * * * * (display i)
> * * * * * * * * (loop (- i 1)) )))
> * * (print #\newline) )
>
> (define (print-triangle n)
> * * (if (not (zero? n))
> * * * * (print-row n)
> * * * * (print-triangle (- n 1)) ))

a version that actuallyworked wouldlook more like this:-

(define (print-row n)
(let loop ((i 1))
(if (<= i n)
(begin
(display i)
(loop (+ i 1)) )))
(display #\newline) )

(define (print-triangle n)
(if (not (zero? n))
(begin
(print-row n)
(print-triangle (- n 1)) )))