Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 376
  • Last Modified:

Needs help in logic

Hi guys: Can any one please help me in the logic. I try first but stuck in the middle of it.


1   1   1     1       1     1
2   4   8   16      32   64
3   9   27   81   243   729
4 16   64   256 1024  4096


#include <iostream>
using namespace std;

int main()
{
      int row1 = 0;
      int col1 = 0;
      cout << "1" << "    " << "1" << "    " << "1" << "    " << "1" << "    " << "1" << "    " << "1" << endl;
      for(row1 = 1; row1 <= 4; row1 ++)
      {
                  for(col1 = 1; col1 <= 4; col1 ++)
                  {
                        cout << row1*col1;
                  



      system("pause");
      return 0;
}  
0
mustish1
Asked:
mustish1
  • 4
  • 3
1 Solution
 
TommySzalapskiCommented:
I'll show you how I would do row 2 and let you figure the others out.

const int ITERATIONS = 6;


int multiplier = 2;
int num;

num = multiplier;
for(int i = 0; i < ITERATIONS; ++i)
{
  cout<<num<<'\t';
  num *= multiplier;
}

Open in new window

0
 
TommySzalapskiCommented:
Then you just change multiplier for each row. Just make sure you do num = multiplier every time before you get into the loop. Note: the '\t' is the tab character to make the output look more pretty. You could also use the iomanip library and setw if you wanted to pick how wide each column should be.

Bonus: You don't need to (and shouldn't) do row 1 separately. Just set the multiplier to 1 for that row.
0
 
mustish1Author Commented:
Thanks. I did it try to avoid the use of tab thing and use "  " but it dont work. Is there any way i can arrange them without using \t

#include <iostream>
using namespace std;

int main()
{
const int ITERATIONS = 6;
int multiplier = 1;
int num=1;
int i = 0;
int k = 0;
for(k = 0; k < 4; k++)
      {
      num = multiplier;
      for(i = 0; i < ITERATIONS; ++i)
            {
                  cout << num << '\t';
                  num *= multiplier;
            }
      cout << endl;
      multiplier++;
      }
system("pause");
return 0;
}  
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
mustish1Author Commented:
Thanks.
0
 
TommySzalapskiCommented:
As I mentioned you could use setw (#include<iomanip>) to specify a width. You also could count the digits in the integer by finding the log base 10 and rounding down, but that's more complicated than necessary.
http://www.cplusplus.com/reference/iostream/manipulators/setw/
0
 
TommySzalapskiCommented:
So it would look something like this

#include<iostream>
#include<iomanip>

const int WIDTH = 10;
const int ITERATIONS = 6; //Note: consts usually go outside of main

int main()
{
  //your code
      cout << setw(WIDTH) << num;
  //rest of your code
}

Open in new window


0
 
mustish1Author Commented:
ok thanks.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now