Help with simple assignmnet

I'm trying to complete this assignment from my c++ class. I can't figure out how to set up the columns or where to start really. Your help is greatly appreciated.
http://s471.photobucket.com/albums/rr76/mk420v/?action=view&current=Assignment2.jpg

So far I have only managed to write this:
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int main ( )
{
int num, degree, columns;
degree=0;
num=2;

while (degree <15)
{
cout<< setw(5) << right << pow (num,degree) << endl;
++degree;
}


return 0;
}

You want to ask the user for amount of columns per line, and then print that amount of columns. Basically your while loop needs to count the amount of loops it's done, and if (amount % count == 0) cout << endl;. just make sure your count is never zero.

I'll leave the formatting to you, though it looks like you've got it I hate that stuff.
Last edited on
Still looking for help
hey guys, im in the same class as mk620t. I really have no idea even how to start. would a for loop be better than while? any advice or code would be greatly appreciated.
What's going on? After seeing someone else's work in class I decided to use a switch instead. It's more text but it works.
Just did this assignment for fun, post your code and I'll give you some tips/pointers.

Edit: Please use code tags

[ code ] [ / code ]
Last edited on
Do I need to switch the current arguments for modulus listed above?


#include <iostream> //includes c++ library

#include <cmath> //includes math functions

#include <iomanip> //allows manipulation



using namespace std; //allows use of cin and cout



int main() //start of main argument

{

int degree; //declare integer variable degree



loop: //This is the point that the goto loop jumps to

cout<<"Number of columns: "; //prints input prompt to screen

cin>>degree; //stores using input to variable degree

cout<<endl; //prints an extra blank line to the screen



switch (degree) //Beginning of switch based on variable degree

{

case 0: //Used when input is '0'

break; //Tells the program to break or end



case 1: //Used when input is '1'

for(int degree=0; degree<15; degree++) //Start of for loop. degree equals 0, when degree is less than 15, increase degree by 1

{

cout << setw(6) << right << pow(2,degree) << endl; //Sets the width of display to 6, right justifies and uses the pow function to calculate
// 2^0, 2^1, 2^2,...... after each value prints the line ends.
}

cout << endl; //Prints a blank line to the screen

goto loop; //Goes back to the beginning of the loop



case 2: //Used when input is '2'

for(int degree=0; degree<15; degree++) //Start of for loop. degree equals 0, when degree is less than 15, increase degree by 1

{

cout << setw(6) << right << pow(2,degree++); //Sets width of display to 6, right justifies, uses pow function

cout << setw(7) << right << pow(2,degree) << endl; //Sets width to 7, right justifies, uses pow function, ends line.
//Function loops until the desired values are printed
}

cout << endl; //Prints a blank line to the screen

goto loop; //Goes back to the beginning of the loop



case 3: //Used when input is '3'

for(int degree=0; degree<15; degree++) //Start of for loop. degree equals 0, when degree is less than 15, increase by 1

{

cout << setw(6) << right << pow(2,degree); //Sets width of display to 6, right justifies, uses pow function

cout << setw(7) << right << pow(2,++degree); //Sets width to 7, right justifies, uses pow function

cout << setw(8) << right << pow(2,++degree) << endl; //Sets width to 8, right justifies, used pow function, ends line.
//Function loops until the desired values are printed
}

cout << endl; //Prints a blank line to the screen

goto loop; //Goes back to the beginning of the loop



case 4:

for(int degree=0; degree<15; degree++) //Start of for loop. degree equals 0, when degree is less than 15, increase by 1

{

cout << setw(6) << right << pow(2,degree); //Sets width of display to 6, right justifies, uses pow function

cout << setw(7) << right << pow(2,++degree); //Sets width to 7, right justifies, uses pow function

cout << setw(8) << right << pow(2,++degree); //Sets width to 8, right justifies, uses pow function

cout << setw(9) << right << pow(2,++degree) << endl; //Sets width to 9, right justifies, uses pow function, ends line
//Function loops until the desired values are printed
}

cout << endl; //Prints a blank line to the screen

goto loop; //Goes back to the beginning of the loop



case 5:

for(int degree=0; degree<15; degree++) //Start of for loop. degree equals 0, when degree is less than 15, increase by 1

{

cout << setw(6) << right << pow(2,degree); //Sets width of display to 6, right justifies, uses pow function

cout << setw(7) << right << pow(2,++degree); //Sets width to 7, right justifies, uses pow function

cout << setw(8) << right << pow(2,++degree); //Sets width to 8, right justifies, uses pow function

cout << setw(9) << right << pow(2,++degree); //Sets width to 9, right justifies, uses pow function

cout << setw(10) << right << pow(2,++degree) << endl; //Sets width to 10, right justifies, uses pow function, ends line
//Function loops until the desired values are printed
}

cout << endl; //Prints a blank line to the screen

goto loop; //Goes back to the beginning of the loop



case 6:

for(int degree=0; degree<15; degree++) //Start of for loop. degree equals 0, when degree is less than 15, increase by 1

{

cout << setw(6) << right << pow(2,degree); //Sets width of display to 6, right justifies, uses pow function

cout << setw(7) << right << pow(2,++degree); //Sets width to 7, right justifies, uses pow function

cout << setw(8) << right << pow(2,++degree); //Sets width to 8, right justifies, uses pow function

cout << setw(9) << right << pow(2,++degree); //Sets width to 9, right justifies, uses pow function

cout << setw(10) << right << pow(2,++degree); //Sets width to 10, right justifies, uses pow function

cout << setw(11) << right << pow(2,++degree) << endl; //Sets width to 11, right justifies, uses pow function, ends line
//Function loops until the desired values are printed
}

cout << endl; //Prints a blank line to the screen

goto loop; //Goes back to the beginning of the loop




default: //The case that the switch defaults to is no recognizable input is available

cout << "Number of columns out of range [0-6]. Try again..." << endl; //Prints out of range prompt

goto loop; //Goes back to the beginning of the loop

}



return 0; //Ends program

}
I'm having trouble with it printing out more than 15 values. Happens when 15 isn't cleanly divisible by the number of rows. I understand that modulus fixes this, I just don't understand how to implement it.
Can you put [ code ] [ /code ] tags so I can read your code?

cout << "Readable.\n";
cout<<"Not really...\n";
Sorry, this should be better

#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

int main()
{
int degree;

loop:
cout<<"Number of columns: ";
cin>>degree;

switch (degree)
{
case 0:
break;

case 1:
for(int degree=0; degree<15; degree++)
{
cout << right << pow(2,degree) << endl;
}
goto loop;

case 2:
for(int degree=0; degree<15; degree++)
{
cout << setw(9) << right << pow(2,degree++);
cout << setw(10) << right << pow(2,degree) << endl;
}
goto loop;

case 3:
for(int degree=0; degree<15; degree++)
{
cout << setw(5) << right << pow(2,degree++);
cout << setw(6) << right << pow(2,degree++);
cout << setw(7) << right << pow(2,degree++);
}
goto loop;

case 4:
for(int degree=0; degree<15; degree++)
{
cout << setw(5) << right << pow(2,degree++);
cout << setw(6) << right << pow(2,degree++);
cout << setw(7) << right << pow(2,degree++);
cout << setw(8) << right << pow(2,degree++);
}
goto loop;

case 5:
for(int degree=0; degree<15; degree++)
{
cout << setw(5) << right << pow(2,degree++);
cout << setw(6) << right << pow(2,degree++);
cout << setw(7) << right << pow(2,degree++);
cout << setw(8) << right << pow(2,degree++);
cout << setw(9) << right << pow(2,degree++);
}
goto loop;

case 6:
for(int degree=0; degree<15; degree++)
{
cout << setw(5) << right << pow(2,degree++);
cout << setw(6) << right << pow(2,degree++);
cout << setw(7) << right << pow(2,degree++);
cout << setw(8) << right << pow(2,degree++);
cout << setw(9) << right << pow(2,degree++);
cout << setw(10) << right << pow(2,degree++);
}
goto loop;

default:
cout << "Number of columns out of range [0-6]. Try again...";
goto loop;
}

return 0;
}
This is actually what I have as of now... that was a little while ago in textedit


#include <iostream>
#include <cmath>
#include <iomanip>



using namespace std;



int main()

{

int degree;

loop:
cout<<"Number of columns: ";
cin>>degree;
cout<<endl;

switch (degree)

{

case 0:
break;


case 1: for(int degree=0; degree<15; degree++)
{
cout << setw(6) << right << pow(2,degree) << endl; }
cout << endl; goto loop;


case 2: for(int degree=0; degree<15; degree++)
{
cout << setw(6) << right << pow(2,degree++);
cout << setw(7) << right << pow(2,degree) << endl; }
cout << endl; //Prints a blank line to the screen

goto loop; //Goes back to the beginning of the loop


case 3: for(int degree=0; degree<15; degree++)
{
cout << setw(6) << right << pow(2,degree);
cout << setw(7) << right << pow(2,++degree);
cout << setw(8) << right << pow(2,++degree) << endl;
}
cout << endl;
goto loop;

case 4:

for(int degree=0; degree<15; degree++)
{
cout << setw(6) << right << pow(2,degree);
cout << setw(7) << right << pow(2,++degree);
cout << setw(8) << right << pow(2,++degree);
cout << setw(9) << right << pow(2,++degree) << endl;
}
cout << endl; //Prints a blank line to the screen

goto loop; //Goes back to the beginning of the loop

case 5:

for(int degree=0; degree<15; degree++)
{

cout << setw(6) << right << pow(2,degree);
cout << setw(7) << right << pow(2,++degree);
cout << setw(8) << right << pow(2,++degree);
cout << setw(9) << right << pow(2,++degree);
cout << setw(10) << right << pow(2,++degree) << endl; }
cout << endl;

goto loop;

case 6:

for(int degree=0; degree<15; degree++)
{

cout << setw(6) << right << pow(2,degree);
cout << setw(7) << right << pow(2,++degree); cout << setw(8) << right << pow(2,++degree); cout << setw(9) << right << pow(2,++degree); cout << setw(10) << right << pow(2,++degree); cout << setw(11) << right << pow(2,++degree) << endl;
}

cout << endl;

goto loop;

default: cout << "Number of columns out of range [0-6]. Try again..." << endl;

goto loop;
}



return 0;

}
Much better. Whenever you expect someone else to read your code (heck, even if no one will read it), it should be very neat and readable.

Now...

cout << setw(7) << right << pow(2,degree++);

Should be:

cout << right << setw(7) << pow(2,degree++);

If you want to pass non-double values to pow (since it wouldnt let me pass an int), then convert them to doubles, or initialize them as doubles in the first place, otherwise I myself, end up with a C2668:

cout << right << setw(7) << pow (2, double(degree++);

I'll be editing this post for more corrections - I'm using compiler Microsoft Visual C++ 2010 Express for this.

Edit: Without completely redesigning your program or giving you the answer, here's something to consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
case 1:
for (int i = 0; i < 15; i++) /* you actually don't need a switch statement for 
this, in my own program I just used a for statement, much simpler that way */
{
cout << right << setw(7) << pow(2,double(i));   // using the pow function
counting++;
	if (counting == columns)  /* if 6 numbers are printed, and columns = 6, 
this "if" statement will output to the console an endline */
	{
		cout << endl;
		counting = 0;  // reset the number counter to 0
	}
}
cout << endl;
goto loop;


Let me know when you get your program to work as shown in the example, if you're still having trouble, I'll be here.
Last edited on
Hi,

You may find something useful in my code, have a look.

Mike

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

int main()
{
	int col;

	do{
	
		cout<<"Number of columns: ";
		cin>>col;
		while(col<=0 || col>6){
			cout<<"Number of columns out of range []. Try again...";
			cin>>col;
		}
		for(int i=0;i<=15;i++){
			int sol=pow(2.0,i);
			if(i%col==0)
				cout<<endl;
			cout<<right<<setw(8)<<sol;
		}
		cout<<endl;	
	
		


	}while(col!=0);
	cout<<"Will terminate now, goodbey!";
	exit(1);

	
	_getch();	
    return 0;
}
Topic archived. No new replies allowed.