beginners question

Hello ,
I just started programing with c++ , I'm learning it at home , it's kinda hard and I have a small task to do , I need to write a program to creat a multiplication table from 1 to 10 , can anybody help?
You mean to produce as an output something like:
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
...
10 20 30 40 50 60 70 80 90 100
???

Then you just need 2 nested loops. One will change the base numbers from 1-10 and the other will make the multiplications and produce the output.
Your use of the word "need" instead of "want" or "would like to" tips me off that this is in fact a homework assignment.

Homework help is fine but people will only help you with specific problems you are having. So you should post what you already have and state what you're problem is and someone will definitely help you. NO ONE WILL WRITE YOU'RE CODE FOR YOU ESPECIALLY IF ITS HOMEWORK!

If you are honestly learning this by yourself then maybe you should take a few steps back. Check out the tutorial here maybe too

http://www.cplusplus.com/doc/tutorial/

Mitsakos is right, use nested for loops.
Last edited on
it's not my homework , it's not for school or for university , I just bought a book about c++ and it ask to make multiplication table , I don't wanna go foward without completing every task that is asked.
Yeah , in the book it is writen that I need to use 2 nested loops , but somehow it doesn't work , so thats why I asked for help.
Can you post the code you have already...? Use the # and copy and paste your code in between [c0de] [/c0de] then someone will be able to help you. "It doesn't work" doesn't cut it.
Last edited on
I think mikeb570's responses are worded a little more roughly than they need to be, but he is right in the essentials.

While we don't want to just give away answers (we'll make you sweat for them, and when you know enough to help others you'll make them sweat for them too), the main reason is that we want to tailor our responses to best suit your needs.

In order to do that, and as part of organizing the problem better in your own brain, you need to be as explicit as possible and show us what you've tried. It would be very easy for us to answer in a way that is both or either entirely incomprehensible and useless to your task.

Are you using arrays or are you still just writing output to the screen with cout?
Is the purpose of the exercise to familiarize you with a loop construct (aka for or while or do..while)?

Do what you can, post it, and we'll help correct and refine.

[code]
#include <iostream>
using namespace std;

int main()
{
cout << "Hello world!\n";
return 0;
}
[/code]

becomes:
1
2
3
4
5
6
7
8
#include <iostream>
using namespace std;

int main()
  {
  cout << "Hello world!\n";
  return 0;
  }


@mikeb570
I tend to not use the [c0de] example because many times people will actually type "[c0de]" and still get it wrong. You can break the bbcode parser and show the actual tag used by inserting other tags immediately after the opening bracket:

[[b][/b]code] --> [code]
[[b][/b]/code] --> [/code]

Cool, no?


Hope this helps.
Last edited on
1
2
3
4
5
6
7
#include <stdio.h>
main ()
{
for(int x= 1; x <= 10; x++);
for(int i = 1; i <= 10; i++);
printf ("Sum \t" , i*x);
}

I don't know , somehow it shows no errors , but doesn't work .
^m4k4v3l1
Put in a
1
2
cin.get();
return 0;
at the end, it should show the output.
Your loops are empty.

for (;;) ;
is the same as "do nothing forever".

for (int n = 0; n < 5; n++) ;
is the same as "do nothing five times".

Remember that in C++ the semicolon terminates a statement. Get rid of the semicolons at the end of lines 4 and 5 and you should be good to go.

Also, the printf() format string needs to be "%d\t"...


However, if your C++ book is teaching you to use <stdio.h> and printf() it isn't very good. You should be using the standard streams right at the start:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;

int main()
  {
  for (int y = 1; y <= 10; y++)
    {
    for (int x = 1; x <= 10; x++)
      cout << (x * y) << '\t';
    cout << '\n';
    }

  return 0;
  }

All your C++ programs should have the same kind of format as the two examples I've posted. Notice also that I added a newline in there so that it prints ten lines of ten numbers.

Good job!
Last edited on
You could use an array, then a couple of nested for loops to use it.

I'll post it in a little while.
Hold off, don't just give him answers. Let him work it out on his own first.
Sorry for being harsh, I'm a bit moody =P
It's aight.
I don't wanna return to multiplication table and it works , now I need to do is this table: http://img53.imageshack.us/img53/6671/tablekf5.jpg
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;

int main()
{
    for(x=1;x<=10;x++)

                      cout << (x^2, x^3, 1/x) << '\t';
                      cout << '\n';
                      {
                      return 0;
                      }
                      }      

      

it shows error in for(x=1;x<=10;x++) , I don't know whats the problem
Last edited on
You forgot to declare x before you use it.
Also the "^" operator is the bitwise exclusive or (XOR). I think you need the powe. The pow() function can do it for you: http://www.cplusplus.com/reference/clibrary/cmath/pow.html
but with the pow , i'll have to write lot's of lines if I want to do the same thing as it is shown in the table.
You can build long lines with cout
EG

 
cout << "\t" << pow(x,2) << "\t" << pow(x,3) << "\t" << 1/x << "\n";

cout << "\t" << pow(x,2) << "\t" << pow(x,3) << "\t" << 1/x << "\n";
so how I need to write this into c++ ? I'm a lil bit confused.
That is the C++ :-)
cout allows you to 'string together' outputs onto a single line of code.
So the above line will do
<tab>x2<tab>x3<tab>1/x

Use it in place of lines 8 & 9 of the code you posted.

See http://www.cplusplus.com/doc/tutorial/basic_io.html fro more info on cout, etc on the sites tutorial.
Topic archived. No new replies allowed.