Multiplication Table

I am trying to creat a multiplication table that show the rows printed

Page 1 of 1

9 Replies - 6113 Views - Last Post: 16 May 2009 - 01:07 PM Rate Topic: -----

#1 Myro   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 15-May 09

Multiplication Table

Posted 15 May 2009 - 11:05 PM

I am trying to create a c++ console application that shows a multiplication table with the amount of rows printed, columns printed and the sum. This is my code so far and it shows the multiplication table, rows and columns printed. I can't figure out how to code the sum into all of this and have been at it for days now. PLEASE HELP!!!! :)



#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	int startNumber;
	int endNumber;
	int colCounter = 0;
	int rowCounter = 0;

	cout << "Multiplication Table\n"
		<< "Enter starting number: ";
	cin >> startNumber;
	cout << "Enter ending number: ";
	cin >> endNumber;

   cout << setw ( 4 ) << right << "|";
   for ( int colNumber = startNumber; colNumber <= endNumber; ++colNumber ) 
  
	  cout << left << setw ( 5 ) << colNumber;
	  cout << "\n------------------------------------------------------\n";

   for ( int colNumber = startNumber; colNumber <= endNumber; ++colNumber )
   {
	   cout << setw ( 3 ) << left << colNumber << '|';

	for ( int currentRow = startNumber; currentRow <= endNumber; ++currentRow )

	  cout << setw ( 5 ) << left << colNumber*currentRow;
	  cout << "\n   |\n";
	  colCounter = colCounter + 1;
	  rowCounter = rowCounter + 1;
   }
   cout << "Rows printed: " << rowCounter << endl;
   cout << "Columns Printed: " << colCounter << endl;
   cout << "Sum: "; //<< theSum << endl;
  
   return 0;
}


Is This A Good Question/Topic? 1

Replies To: Multiplication Table

#2 aceofspades686   User is offline

  • D.I.C Regular
  • member icon

Reputation: 6
  • View blog
  • Posts: 334
  • Joined: 08-October 07

Re: Multiplication Table

Posted 15 May 2009 - 11:27 PM

What exactly are you looking for the sum of? All numbers in the table? Sum of each row/column?
Was This Post Helpful? 0
  • +
  • -

#3 Myro   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 15-May 09

Re: Multiplication Table

Posted 16 May 2009 - 03:21 AM

View Postaceofspades686, on 15 May, 2009 - 10:27 PM, said:

What exactly are you looking for the sum of? All numbers in the table? Sum of each row/column?


Yes. All the numbers in the table.
Was This Post Helpful? 0
  • +
  • -

#4 aceofspades686   User is offline

  • D.I.C Regular
  • member icon

Reputation: 6
  • View blog
  • Posts: 334
  • Joined: 08-October 07

Re: Multiplication Table

Posted 16 May 2009 - 03:43 AM

Just keep a running total as you add them in then.

Declare and initialize a new variable somewhere before you get into your loops.
int sum = 0;



Then just add the numbers to it as you put them in the table.
sum += colNumber*currentRow;


Was This Post Helpful? 0
  • +
  • -

#5 Myro   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 15-May 09

Re: Multiplication Table

Posted 16 May 2009 - 03:48 AM

View Postaceofspades686, on 16 May, 2009 - 02:43 AM, said:

Just keep a running total as you add them in then.

Declare and initialize a new variable somewhere before you get into your loops.
int sum = 0;



Then just add the numbers to it as you put them in the table.
sum += colNumber*currentRow;



I put it in the code where currentRow is initialized and I am getting an error saying currentRow is undeclared.
Was This Post Helpful? 0
  • +
  • -

#6 aceofspades686   User is offline

  • D.I.C Regular
  • member icon

Reputation: 6
  • View blog
  • Posts: 334
  • Joined: 08-October 07

Re: Multiplication Table

Posted 16 May 2009 - 03:55 AM

for ( int currentRow = startNumber; currentRow <= endNumber; ++currentRow )

	  cout << setw ( 5 ) << left << colNumber*currentRow;
	  sum += colNumber*currentRow;
	  cout << "\n   |\n";
	  colCounter = colCounter + 1;
	  rowCounter = rowCounter + 1;
   }



Try putting it inside this loop :P As I said, you add the numbers to it as you add them to the table.
Was This Post Helpful? 0
  • +
  • -

#7 Myro   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 15-May 09

Re: Multiplication Table

Posted 16 May 2009 - 04:31 AM

View Postaceofspades686, on 16 May, 2009 - 02:55 AM, said:

for ( int currentRow = startNumber; currentRow <= endNumber; ++currentRow )

	  cout << setw ( 5 ) << left << colNumber*currentRow;
	  sum += colNumber*currentRow;
	  cout << "\n   |\n";
	  colCounter = colCounter + 1;
	  rowCounter = rowCounter + 1;
   }



Try putting it inside this loop :P As I said, you add the numbers to it as you add them to the table.



Maybe I'm too dumb at C++ to get it, but now my code is completely whacked :) it only prints 1 row with a whole bunch of numbers after it. :( The code comes out like this:

2 4 2 468103 6912154 8121620Rows Printed: 3
Columns Printed: 3
Sum: 9

This post has been edited by Myro: 16 May 2009 - 04:37 AM

Was This Post Helpful? 0
  • +
  • -

#8 pdkharkar   User is offline

  • D.I.C Regular
  • member icon

Reputation: 65
  • View blog
  • Posts: 345
  • Joined: 19-January 09

Re: Multiplication Table

Posted 16 May 2009 - 07:28 AM

try this one!!!
maybe this could help it out...........

tell me if you want any modifications....

//A PROGRAM TO DISPLAY THE TABLE OF A NUMBER GIVEN BY USER

#include<stdio.h>
#include<conio.h>

main()
  {
	int num,i;
	clrscr();
	printf("Enter the Number\n\n\n\n");
	scanf("%d",&num);
	  for(i=1;i<=10;i++)
	printf("\n%d X %d=\t%d",num,i,num*i);
	getch();
  }




This post has been edited by pdkharkar: 16 May 2009 - 07:29 AM

Was This Post Helpful? 0
  • +
  • -

#9 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5239
  • View blog
  • Posts: 14,031
  • Joined: 18-April 07

Re: Multiplication Table

Posted 16 May 2009 - 09:08 AM

I already cover this solution in one of my blog entries. I show one way to do it, explain it and provide a screenshot of the results.

Martyr2's Programming Underground - Simple Multiplication Table in C++

Enjoy! :)
Was This Post Helpful? 0
  • +
  • -

#10 aceofspades686   User is offline

  • D.I.C Regular
  • member icon

Reputation: 6
  • View blog
  • Posts: 334
  • Joined: 08-October 07

Re: Multiplication Table

Posted 16 May 2009 - 01:07 PM

Well, Martyr's example is going to be about 10x better to follow, but I was bored this morning and decided to compile what you had to see the problem. Most of what was causing the issue was a lot of misplaced loops and logic errors when outputting the different cells (the big one being, you don't want a line break after each cell :P).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1