C++ Help with homework! Hi! I am a beginner to C++ programming and I am completed stumped on one of my homework labs. I will post the homework instructions and what I have done so far. I appreciate any tips or guidance!

Instructions: Prompt the user for a number such as 5 and calculate the square by using pow(variable, 2.0), cube by using pow(variable, 3.0) and 4th power pow(variable, 4.0). Add 5 to the number and repeat the procedure 10 times. In this case up to 50.

What I have done so far:
do
{
cout << "Please enter a number to Square, Cube, and raise to 4th power: ";
cin >> variable;
cout << endl;
cout << "\t" << "Number\tSquare\tCube\t4th Power" << endl;
cout << "\t" << "------\t------\t------\t-------" << endl;
square = pow(variable, 2.0);
cube = pow(variable, 3.0);
fourthP = pow(variable, 4.0);
for (int a = 1; a <= 1; a++) // Outer loop
{
for (int b = 1; b <= 10; b++) // Inner loop
cout << "\t" << variable << "\t" << square << "\t" << cube << "\t" << fourthP << endl;
cout << endl;
}
cout << endl;

cout << "Would you like to continue (Y or N)? ";
system("cls"); You want to repeat the calculations 10 times in a single for loop. In the for loop you increment the input variable by 5 each time through the loop.

 PLEASE learn to use code tags, they make reading and commenting on source code MUCH easier. http://www.cplusplus.com/articles/jEywvCM9/ http://www.cplusplus.com/articles/z13hAqkS/ HINT: you can edit your post and add code tags. Some formatting & indentation would not hurt either

It also would help if your code snippet is complete, copy-able and ready-to-compile. That means including the headers and the main function.

 123456789101112131415161718192021222324252627282930 #include #include int main() { char repeat { 'n' }; do { std::cout << "Enter a number: "; double input { }; std::cin >> input; std::cout << "\n Number\tSquare\tCube\t4th Power\n"; std::cout << " ------\t------\t------\t-------\n"; for (int i { 1 }; i <= 10; ++i) { std::cout << ' ' << input << '\t' << std::pow(input, 2.0) << '\t' << std::pow(input, 3.0) << '\t' << std::pow(input, 4.0) << '\n'; input += 5; } std::cout << "\nCalculate a different number? "; std::cin >> repeat; } while (repeat != 'n'); }

 Enter a number: 5 Number Square Cube 4th Power ------ ------ ------ ------- 5 25 125 625 10 100 1000 10000 15 225 3375 50625 20 400 8000 160000 25 625 15625 390625 30 900 27000 810000 35 1225 42875 1.50062e+06 40 1600 64000 2.56e+06 45 2025 91125 4.10062e+06 50 2500 125000 6.25e+06 Calculate a different number? y Enter a number: 4 Number Square Cube 4th Power ------ ------ ------ ------- 4 16 64 256 9 81 729 6561 14 196 2744 38416 19 361 6859 130321 24 576 13824 331776 29 841 24389 707281 34 1156 39304 1.33634e+06 39 1521 59319 2.31344e+06 44 1936 85184 3.7481e+06 49 2401 117649 5.7648e+06 Calculate a different number? n But how do I get the output to look like this (when entering 2):
2 4 8 16
7 49 343 2401
12 144 1728 20736
17 289 4913 83521

(etc...) you can use int 64 to print the values as integers for a while. But it will overflow at some point, so you may not want that. much past 7100ish will overflow uint64_t to the 5th. you can adjust the double printing to look more like integers as well ... look at the cout format specifiers and play with them if you want. http://faculty.cs.niu.edu/~hutchins/csci241/output.htm

and I see furry already fixed the extra variables :)
Last edited on But how do I get the output to look like this (when entering 2)

Enter 2 at the prompt. I simply used 5 and 4 as a test.

 Enter a number: 2 Number Square Cube 4th Power ------ ------ ------ ------- 2 4 8 16 7 49 343 2401 12 144 1728 20736 17 289 4913 83521 22 484 10648 234256 27 729 19683 531441 32 1024 32768 1.04858e+06 37 1369 50653 1.87416e+06 42 1764 74088 3.1117e+06 47 2209 103823 4.87968e+06 Calculate a different number? n
Last edited on Thank you so much! I really appreciate the help. and I see furry already fixed the extra variables :)

Without code tags and an incomplete code snippet I didn't see the overuse of variables. I simply coded what I knew would work without the excess memory usage. Another approach for future consideration - using pow is not the best for integers, even though you probably aren't allowed to deviate. <iomanip> gives good control over columns hence <iomanip>, tabs are a student's nightmare and make beginners wonder why C++ is taught.

 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 #include #include // FOR FORMATTING - '\t' is hopelessly frustrating using namespace std; int main() { char answer{'Y'}; int col_width{11}; ONLY PLACE REQUIRED TO ADJUST THE WIDTH! while ( toupper(answer) == 'Y' ) { cout << "Please enter a number to Square, Cube, and raise to 4th power: "; int variable{0}; cin >> variable; cout << '\n' << setw(col_width) << right << "Number" << setw(col_width) << right << "Square" << setw(col_width) << right << "Cube" << setw(col_width) << right << "4th Power\n"; for(int i = 0; i < 4; i++) cout << ' ' + std::string(col_width - 1, '-'); cout << '\n'; long square = variable * variable; long cube = square * variable; long fourthP = cube * variable; for (int b = 0; b < 10; b++) // Inner loop { square = variable * variable; cube = square * variable; fourthP = cube * variable; cout << setw(col_width) << right << variable << setw(col_width) << right << square << setw(col_width) << right << cube << setw(col_width) << right << fourthP << '\n'; variable += 5; } cout << '\n'; cout << "Would you like to continue (Y or N)? "; cin >> answer; } return 0; }

 Please enter a number to Square, Cube, and raise to 4th power: 18 Number Square Cube 4th Power ---------- ---------- ---------- ---------- 18 324 5832 104976 23 529 12167 279841 28 784 21952 614656 33 1089 35937 1185921 38 1444 54872 2085136 43 1849 79507 3418801 48 2304 110592 5308416 53 2809 148877 7890481 58 3364 195112 11316496 63 3969 250047 15752961 Would you like to continue (Y or N)? 12345678910111213141516171819202122 #include #include using namespace std; int main() { #define FORMAT << " " << setw( 12 ) << right << int number; cout << "Please enter a number: "; cin >> number; for ( auto head : { "x", "x^2", "x^3", "x^4" } ) cout FORMAT head; cout << "\n\n"; for ( int last = number + 45; number <= last; number += 5 ) { unsigned long long x = 1; for ( int power = 1; power <= 4; power++ ) { x *= number; cout FORMAT x; } cout << '\n'; } }

 Please enter a number: 2 x x^2 x^3 x^4 2 4 8 16 7 49 343 2401 12 144 1728 20736 17 289 4913 83521 22 484 10648 234256 27 729 19683 531441 32 1024 32768 1048576 37 1369 50653 1874161 42 1764 74088 3111696 47 2209 103823 4879681
Topic archived. No new replies allowed.