954,483 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Index based Computation within a Loop

My assignment is to write a program that outputs the following columns of numbers using mathematical operators and the loop index.

This is what I want the program to output:

1       10      0       1       1       0
	2       20      1       4       2       0
	3       30      2       9       3       0
	4       40      3       16      4       0
	5       50      4       25      0       1
	6       60      5       36      1       1
	7       70      6       49      2       1
	8       80      7       64      3       1
	9       90      8       81      4       1
	10      100     9       100     0       2


*I couldn't get the top row aligned right. The top row SHOULD be moved over to the right to line up with the columns.

I've got the correct output for columns 1, 2, 3, and 4 but I'm not sure how to get the correct output for columns 5 and 6.

Here is my code:

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


int main()
{
	int a;
	int b;
	int c;
	int d;
	int e;
	for(int i = 1; i <11; i++)
	{
		
		a = i*10;
		b = i-1;	
		c = i * i;
		//d = Not Sure What to Do Here;
		//e = Not Sure What to Do Here;
		cout << left << setw (5) << i << left << setw (5) << a << left << setw (5) << b << left << setw (5) << c << left << setw (5) << d << left << setw (5) << e <<"\n";
	    
	}

}


If you run the code as-is, you'll get garbage from variables d and e because they haven't been initialized. So my question is what do I need to do to make variables d and e produce the correct numbers?


Thanks,
Nick

NickPatton
Light Poster
29 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

What are 5 and 6 supposed to compute?
5) maybe %5
6) maybe /5

frogboy77
Posting Pro in Training
481 posts since Aug 2010
Reputation Points: 100
Solved Threads: 39
 

I'm not sure about e , but for d , you will want to use the modulo operator , % .

For e , as I said I'm not sure, but I think it has to do with integer division by 5.

Schol-R-LEA
Posting Pro
556 posts since Oct 2010
Reputation Points: 254
Solved Threads: 85
 

OK, Thanks for the replies guys. @Frogboy77 Column 5 is supposed to compute 1234012340 and column 6 is 0000111112.

NickPatton
Light Poster
29 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

I gave you the answer dude.

frogboy77
Posting Pro in Training
481 posts since Aug 2010
Reputation Points: 100
Solved Threads: 39
 
I gave you the answer dude.


I'm sorry. Wasn't really paying attention. I got the program working though.


Thanks,
Nick

NickPatton
Light Poster
29 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: