12 Replies - 6194 Views - Last Post: 11 January 2011 - 01:14 PM Rate Topic: -----

#1 thesamy  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 54
  • Joined: 12-July 10

Multiplication table

Posted 10 January 2011 - 07:58 AM

hi guys i have a problem i need to solve that i have been working on serveral of hours and i just cant solve it..

i did a Multiplication table with 2 loops and a Console.Write();
and it work fine.

but now i need to do it only with Console.WriteLine();
and i just cant find a way to do it.

can somone help me "get there"
without showing me any code so my stupid brain will get it?

p.s
sorry for my english

Is This A Good Question/Topic? 0
  • +

Replies To: Multiplication table

#2 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4529
  • View blog
  • Posts: 7,899
  • Joined: 08-June 10

Re: Multiplication table

Posted 10 January 2011 - 08:04 AM

You're saying you need to print out a multiplication table without loops? Start writing each individual 1 x 1, 1 x 2, 1 x 3, etc...because you need loops to do this right.
Was This Post Helpful? 1
  • +
  • -

#3 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5576
  • View blog
  • Posts: 11,916
  • Joined: 02-June 10

Re: Multiplication table

Posted 10 January 2011 - 08:11 AM

insertAlias is right. You can't do it with *just* console.WriteLine().
Go back to the homework instructions and read them again.
If that doesn't help, go to the teacher who assigned this and get a better explanation of the assignment.
Was This Post Helpful? 1
  • +
  • -

#4 thesamy  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 54
  • Joined: 12-July 10

Re: Multiplication table

Posted 10 January 2011 - 08:16 AM

oops i didnt explain my self good.

i can use loops
but i cant use the command
Console.Write();

and
i can do any thing i want like using string and stuff
Was This Post Helpful? 0
  • +
  • -

#5 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4529
  • View blog
  • Posts: 7,899
  • Joined: 08-June 10

Re: Multiplication table

Posted 10 January 2011 - 08:18 AM

So this is a school project then?

Use a StringBuilder to create a buffer for each line. Then when you're done with the line, print the whole thing. Alternatively, you can just use a string and keep adding to it, but that's less efficient.
Was This Post Helpful? 1
  • +
  • -

#6 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5576
  • View blog
  • Posts: 11,916
  • Joined: 02-June 10

Re: Multiplication table

Posted 10 January 2011 - 08:23 AM

View PostinsertAlias, on 10 January 2011 - 07:18 AM, said:

So this is a school project then?

Use a StringBuilder to create a buffer for each line. Then when you're done with the line, print the whole thing. Alternatively, you can just use a string and keep adding to it, but that's less efficient.


Quote

but i cant use the command Console.Write();


Buffering it isn't going to matter if the OP can't use Console.Write() to output the results.
What's the alternative?
Was This Post Helpful? 1
  • +
  • -

#7 thesamy  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 54
  • Joined: 12-July 10

Re: Multiplication table

Posted 10 January 2011 - 08:23 AM

i still dont know about buffer and string builders

can u expline me the
use string and keep addint to it part?
(without showing me the answer to my specific problem)
becuse i want to understand to way and apply it by my self.
Was This Post Helpful? 0
  • +
  • -

#8 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4529
  • View blog
  • Posts: 7,899
  • Joined: 08-June 10

Re: Multiplication table

Posted 10 January 2011 - 08:26 AM

View PosttlhIn, on 10 January 2011 - 08:23 AM, said:

View PostinsertAlias, on 10 January 2011 - 07:18 AM, said:

So this is a school project then?

Use a StringBuilder to create a buffer for each line. Then when you're done with the line, print the whole thing. Alternatively, you can just use a string and keep adding to it, but that's less efficient.


Quote

but i cant use the command Console.Write();


Buffering it isn't going to matter if the OP can't use Console.Write() to output the results.
What's the alternative?


According to the initial post, he can use Console.WriteLine. I assume the lesson is in buffering values and outputting them all at once.

@thesamy
The general idea is that inside the loop where you normally would do your Console.Writes, you create an empty string as a buffer. Then every place you would have Console.Write(someString), you instead add what you would have outputted to the buffer (use the += operator). Then, at the point where you're ready to go to the next line, you Console.WriteLine the entire buffer.

This post has been edited by insertAlias: 10 January 2011 - 08:27 AM

Was This Post Helpful? 1
  • +
  • -

#9 thesamy  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 54
  • Joined: 12-July 10

Re: Multiplication table

Posted 10 January 2011 - 08:36 AM

thank u i think i get it
i will post qustion about it if i get in trouble agian

agian thank u both for fast reply!
Was This Post Helpful? 0
  • +
  • -

#10 thesamy  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 54
  • Joined: 12-July 10

Re: Multiplication table

Posted 11 January 2011 - 09:33 AM

got another problem that i need somone to expline it to me

i need to make a program that write line of stars with the user input.
exmple.
user type in 3
he get
*
**
***

now at first i need to do it with 2 loops and i did and everything work fine
but now i need to do it with 1 loop but i got a problem that i dont know how to
like restrt the string i buffer afther i Console.WriteLine() it.

here is the code:
namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Please enter the number of lines");
            int line = int.Parse(Console.ReadLine());

            string stars = "*";
            
            for (int i = 1 ; i <= line ; i++)
            {
                Console.WriteLine(stars);
                stars += stars;
   
            }

        }
    }
}


i just start to use the buffer in my last program so i realy not sure how to do it.
can somone expline it to me without show in the sulotion?
Was This Post Helpful? 0
  • +
  • -

#11 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4529
  • View blog
  • Posts: 7,899
  • Joined: 08-June 10

Re: Multiplication table

Posted 11 January 2011 - 09:40 AM

Your logic is only slightly wrong here. You don't want to reset the buffer, in this case. The best thing you can do is take a pen and paper, and write down everything that is happening. Draw a long box for your variable stars and a box for line and i.

Then trace your execution. Change the content of the boxes as you go. You should notice that you're adding stars to stars, in effect doubling the number of stars you put on each line rather than increasing them by one.

This post has been edited by insertAlias: 11 January 2011 - 09:41 AM

Was This Post Helpful? 1
  • +
  • -

#12 thesamy  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 54
  • Joined: 12-July 10

Re: Multiplication table

Posted 11 January 2011 - 09:48 AM

wow i get it now its working!
thank u!!
Was This Post Helpful? 0
  • +
  • -

#13 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 992
  • View blog
  • Posts: 972
  • Joined: 30-September 10

Re: Multiplication table

Posted 11 January 2011 - 01:14 PM

You can construct the triangle using only one line of code. You would use the constructor of the string class. Here is an example:

Console.WriteLine(new String('*',5);



That would print 5 *'s. You can use the initalizer variable of the for loop ('i') in place of the 5 in the above code.

Think about it this way, your looping through each line in your for loop aren't you? So, on the first loop through, 'i' is 1 (representing line 1), on the second loop through, it's 2 (representing line 2) etc. The line number is equal to the number of stars you have to print (line 1 = 1 stars, line 2 = 2 stars.

Now, can you use the code above, in a for loop, to print out the triangle by replacing the 5 with i? :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1