Reputation: 2900
I'm writing a program for a number theory proof but I have very little experience in writing code.
What i want to do is display all the results of the formula:
Answer = sqrt [4*n]
where; n = 1,2,3,4,5,6,7,...50
But I want to display the results in 2 groups of columns, i.e.,
An example of what I'm trying to display is below*:
*(ignore the dots in the below example, they are only there for display purposes only)
n:............Answer:............n:...........Answer:
So far i have the code working but I just can't figure out how to split and display the n-values and Answer-values as I have shown above. All I can manage to do is display the values in only 2 columns as follows:
n:...........Answer:
This is what I've got so far:
#include <iostream>
#include <iomanip>
#include <cmath> //preprocesser directives
using namespace std; //avoids having to use std:: with cout/cin
int main (int argc, char* argv[])
{
int n;
float Ans;
cout << setw(4) << "n:" << "\t\t" << setw(4) << "Answer:" << "\n" << endl;
for (int n = 0; n<=50; n++)
{
Ans = sqrt ((4)*(n));
cout << setw(4) << n << "\t\t" << setprecision(4) << setw(4) << Ans << endl;
}
cout << "\n\nPress enter to end" << endl;
cin.get();
}
I really have no idea how to split it into 4 separate columns, but I know the \t function must have something to do with it!??
Any help is appreciated! Thanks
Upvotes: 0
Views: 5508
Reputation: 4470
This solution solves your problem and has the advantage of solving similar problems where you have to output (x,y) pairs to a four column table. You aren't locked into inputs increasing 1,2,3 etc.
Add this to top of file, with other includes
#include <vector>
Add this to body
cout << setw(4) << "n:" << "\t\t" << setw(4) << "Answer:" << "\n" << endl;
std::vector<double> inputs;
std::vector<double> answers;
for (int n = 0; n<=50; n++){
inputs.push_back(n);
inputs.push_back(sqrt(4*n));
}
for (int n = 1; n<=25; n++){
cout << setw(4) << Inputs[n] << "\t\t" << setprecision(4) << setw(4) << Answers[n] << "\t\t" << setw(4) << Inputs[n+25] << "\t\t" << setprecision(4) << setw(4) << Answers[n+25] << endl;
}
Upvotes: 0
Reputation: 27548
Loop from 1 to 25 and in the loop body, calculate two results: the one for n and the one for n + 25.
Here is a very simple compilable and runnable example showing you the basic idea:
#include <iostream>
#include <math.h>
int main()
{
for (int n = 1; n <= 25; ++n)
{
std::cout << n << "\t" << sqrt(n) << "\t"
<< (n + 25) << "\t" << sqrt(n + 25) << "\n";
}
}
This may already be sufficient for your needs. However, notice that I've used tabulators. That's easily written but makes correct output depend on tab size, i.e. you may get incorrectly aligned columns on some displays or editors.
One solution for this problem would be to create HTML output, use the <table>
element and let a web browser take care of displaying an actual table:
#include <iostream>
#include <math.h>
int main()
{
std::cout << "<table>\n"; // and other HTML stuff
for (int n = 1; n <= 25; ++n)
{
std::cout << "<tr>";
std::cout << "<td>" << n << "</td><td>" << sqrt(n) << "</td><td>"
<< (n + 25) << "</td><td>" << sqrt(n + 25);
std::cout << "</tr>\n";
}
std::cout << "</table>\n";
}
If you are on Windows, then compile this into, say, output.exe, and call it as follows to create an HTML file:
output.exe >output.html
If you want pure text output, things get harder. You'd have to calculate all results in advance, store them in a std::vector
, convert each of them to std::string
s, look at the strings' sizes, pick the longest, then output all items with appropriate padding spaces. In fact, you'd have to learn a lot of new things for that, which would probably result in one or two additional, more specific questions on Stack Overflow.
Edit: You are using setw(4)
. Actually, that's another reasonable solution to the problem if you are happy with a fixed width.
Upvotes: 0
Reputation: 1572
Change your for loop to:
for (int n = 1; n<=25; n++)
{
Ans = sqrt ((4)*(n));
cout << setw(4) << n << "\t\t" << setprecision(4) << setw(4) << Ans << "\t\t";
Ans = sqrt ((4)*(n + 25));
cout << setw(4) << n + 25 << "\t\t" << setprecision(4) << setw(4) << Ans << endl;
}
Upvotes: 1