Need help with powers/averages table

Here is my assignment:

{Print out a table of powers. Use a for loop that goes from 101 to 112 inclusive. Print out r, r squared, r cubed, square root of r, and cube root of r. Use two decimal places for the roots, and zero decimal places for the squares and cubes, and 2 decimal places for the averages. Use commas in numbers 1000 and above. After the loop, print out the average of the r squared and r cubed. Use columns for your data. Print column headings above your loop.

Also print the data to a file. Attach the file to your submission. Use a txt extension for your file so I can open it in notepad.

So, you will have output to the screen as usual, and also to the file. You can attach as many files as you need to an assignment, just don't click Submit until you have all the files attached.}

// I have figured a lot of it out. This seems all fairly simple, but I'm having a hard time with getting all this information to stick in my brain, and I don't have enough practice yet.

I'm struggling to figure out the best way to do the averages of the squared and cubed numbers, as well as putting the commas in... It seems like it should be so simple, but the solution I attempted led in a failed build without giving me exact error. The current cod I have works great and includes the majority of the data. Just need the averages and commas.

then I have to figure out how to print to a file.



My Code:

//he requests that we keep all these includes, I know they're not all required.

#include <iostream>

#include <string>

#include <cstring>

#include <cmath>

#include <iomanip>

#include <fstream>

#include <cassert>

#include <cstdlib>

#include <ctime>

#include <cctype>

#include <algorithm>

#include <vector>

#include<stdio.h>

using namespace std;

/*
*
*/
int main()
{
int i,n;



printf("No Square Cube Sq Rt Cube Rt\n",n);
for(i=101;i<=112;i++)
{
printf("%d \t %ld \t %ld \t %.2f\t %.2f\n",i,(i*i),(i*i*i),sqrt((double)i),cbrt((double)i));
}
;
printf("Average of Squared and Cubed Numbers");
return 0;
}




Thanks to anyone who has time to help!


What if you would compute and print in separate steps?
1
2
3
4
5
6
7
for (i=101; i<=112; i++ )
{
  int rs = i*i;
  int rc = rs*i;
  printf("%d \t %ld \t %ld \t %.2f\t %.2f\n", i, rs, rc, sqrt((double)i),
    cbrt((double)i) );
}


For an average, you need a sum, don't you?
Can you sum up the rs of each iteration?
That also works.

I need it to self generate as far as I've been told, so I can't do the sums separately.

I am still struggling trying to figure out how to format with commas.
What does "to self generate" mean?

I did not state how to sum. I did ask whether you know how to sum. Is the answer "no"?


You do use C's I/O functions. C++ has something like
http://www.cplusplus.com/reference/locale/numpunct/grouping/
but you don't include <locale>

See how print occurs in this:
http://www.cplusplus.com/reference/locale/numpunct/thousands_sep/
It does:
* print digits
* print separator
* print more digits
Topic archived. No new replies allowed.