2 Replies - 27607 Views - Last Post: 23 March 2008 - 08:19 AM Rate Topic: -----

#1 jvoorhees  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 23-March 08

Multiplication Table using "For" loops

Posted 23 March 2008 - 12:35 AM

I have an assignment to write a program that uses at least one for loop to print a section of the multiplication table. The user should be able to specify the section of the table to be displayed. The program will also display a count of the number of rows printed, the count of the columns printed and the sum of all calculated values.
The user input will appear as such:

**Create a Multiplication Table**
Enter the starting value: 2
Enter the ending value: 5

The output will be:
X 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25

Rows printed = 4
Columns printed = 4
Sum: 196

Here is what I have so far:

#include <iostream> 

using namespace std;

int main() {	

	// declare variables
	int startNumber = 0;
	int lastNumber = 0;
	int columnCounter = 0;
	int rowCounter = 0;
	int accumulator = 0;
	int currentRow = 1;

	cout << "Create a Multiplication Table\n" << endl;

	//The user enters the starting and ending values
	cout << "Enter the starting value: ";
	cin >> startNumber;
	cout << endl;
	cout << "Enter the ending value: ";
	cin >> lastNumber;
	cout << endl;

	int rowNumber = lastNumber;

	//display the table
	cout << " " << " X ";  //display the 'X'

	for (int i = startNumber; i <= lastNumber; i++ ){
		cout << " " << i << " ";	// display the column headings
		columnCounter = columnCounter + 1;
	}
	cout << endl;

	//the table body and row headings
	//outside loop
	while (currentRow != rowNumber){
		cout << "  " << (currentRow + 1) << "  "; // display row headings
		// inside loop
		for (int i = startNumber; i <= lastNumber; i++ )
		{
			cout << (i + i) << "  ";  // output products (row * col)
			accumulator = accumulator + i;  // accumulate the product
		}
		currentRow +=1;
		rowCounter = rowCounter + 1;
		cout << endl; // go to the next row
	}

	cout << endl;

	cout << "Rows printed = " << rowCounter << endl;  // output row number
	cout << "Columns printed = " << columnCounter << endl;  //output column number
	cout << "Sum: "<< accumulator << endl << endl;  // output sum

return 0;
}



Using the previous example, the output looks like this:

X 2 3 4 5
2 4 6 8 10
3 4 6 8 10
4 4 6 8 10
5 4 6 8 10

Rows printed = 4
Columns printed = 4
Sum: 56

Can you please help me figure out how to get rows 3-5 to display correctly?

Is This A Good Question/Topic? 0
  • +

Replies To: Multiplication Table using "For" loops

#2 MorphiusFaydal  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 43
  • View blog
  • Posts: 1,376
  • Joined: 12-May 05

Re: Multiplication Table using "For" loops

Posted 23 March 2008 - 12:40 AM

A pair of for loops, one inside the other.
Was This Post Helpful? 0
  • +
  • -

#3 Sepanto  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 97
  • Joined: 20-March 08

Re: Multiplication Table using "For" loops

Posted 23 March 2008 - 08:19 AM

I didn't actually check the code so there may be some syntax errrors (; missing or somthing)
#include <iostream>
using namespace std;
void mult(int x,int y)
{
int i,j;
	cout<< "X";
	for (i=x;i<=y;i++)
		cout <<" "<<i;
	cout << endl;
	for (i=x;i<=y;i++){
		  cout<<i<<" ";
		  for (j=x;j<=y;j++) 
			 cout<<i*j<<" ";
	  cout << "\n";
	  }
}
int main()
{
int x,y;
cin>>x>>y;
if (x<y)
mult(x,y);
else 
mult (y,x);
return 0;
}


This post has been edited by Sepanto: 23 March 2008 - 08:20 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1