Solved

Needs help in logic

Posted on 2011-02-21
7
347 Views
Last Modified: 2012-06-21
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
Comment
Question by:mustish1
  • 4
  • 3
7 Comments
 
LVL 37

Accepted Solution

by:
TommySzalapski earned 500 total points
ID: 34947149
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
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 34947166
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
 

Author Comment

by:mustish1
ID: 34947850
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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

Author Closing Comment

by:mustish1
ID: 34947853
Thanks.
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 34947893
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
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 34947902
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
 

Author Comment

by:mustish1
ID: 34947905
ok thanks.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
groupSum6 challenge 6 86
Magic Software info 18 130
Windows Service to Receive TCP Packets 4 144
Query Syntax 17 32
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
A short article about problems I had with the new location API and permissions in Marshmallow
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question