Arrays

Hi, anyone can help me, i work on this for two days but don't get it.
Work with Codeblocks
I need creat double array,
first column second column third column fourth fifth
X ... 1/X ... X^2 ... X^3 ... X^4
2.10
2.20
2.30
2.40
2.50
2.60
2.70
2.80
2.90
3.00
Last edited on
Please post what you have been able to do.
#include<conio.h>
#include<iomanip>
#include <iostream>
#include <cmath>
using namespace std;
int main () {

double mas[11][2];
cout<<" X" " 1/X"" X^2"" X^3"" X^4"<<endl;
cout<<"------"" ------"" ------"" ------"" ------"<<endl;
for (double i = 2.00; i <= 3.1; i+= 0.1){

cout.precision(2);
cout<<fixed;
cout << i<< endl;}


return 0;
}
What you have done so far doesn't require an array, you can produce output directly from the for loop.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <iomanip>

int main()
{
   std::cout << std::setprecision(2) << std::fixed;
   std::cout << " X\t 1/X\t X^2\t X^3\t X^4\n";
   std::cout << "------\t------\t------\t------\t------\n";

   for (double i = 2.00; i <= 3.1; i += 0.1)
   {
      std::cout << i << '\t';

      std::cout << 1 / i << '\t';

      std::cout << i * i << '\t';

      // the other columns go here

      std::cout << '\n';
   }
}

By convention creating a 2 dimensional array is # of rows first, then # of columns.
data_type array_name[num_of_rows][num_of_cols];

Your output is 11 rows, 5 columns. double arr[11][5];

Array element access is an integral type (int) std::cout << arr[3][4];

Your double type for loop would useful for filling the array, not printing it out.
Hello rominch,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



In your code "conio.h" is not a standard C++ header file and not all IDEs have this file available. It is best not to use this if you can.

salem c once wrote:

#include<conio.h>
Obsolete since 1990, when the world stopped using DOS as a primary operating system.


I am not sure what you want to do with the array, but this might help:
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include<iomanip>
#include <iostream>
#include <limits>
#include <cmath>

using namespace std;  // <--- Best not to use.
// The most recent post that is worth reading. http://www.cplusplus.com/forum/beginner/258335/

int main()
{
	constexpr int MAXROW{ 11 }, MAXCOL{ 5 };

	double mas[MAXROW][MAXCOL];

	std::cout << std::fixed << std::showpoint << std::setprecision(2); // <--- Only needs done once.

	cout << "     X     1/X   X^2   X^3   X^4" << endl;
	cout << "  ------  ------  ------  ------  ------" << endl;

	constexpr int WIDTH{ 7 };

	for (double i = 2.00; i <= 3.1; i += 0.1)
	{
		//cout << i << endl;
		std::cout << " "
			<< std::setw(WIDTH) << i
			<< std::setw(WIDTH) << 1 / i
			<< std::setw(WIDTH) << i * i
			<< std::setw(WIDTH) << pow(i, 3)
			<< std::setw(WIDTH) << pow(i, 4)
			<< '\n';
	}

	double X{ 2.0 };
	int col{};

	for (int row = 0; row < MAXROW; row++)
	{	
		mas[row][col] = X;
		mas[row][col + 1] = 1 / X;
		mas[row][col + 2] = X * X;
		mas[row][col + 3] = pow(X,3);
		mas[row][col + 4] = pow(X,4);

		X += 0.1;
	}

	std::cout << "\n\n";

	for (int row = 0; row < MAXROW; row++)
	{
		for (int col = 0; col < MAXCOL; col++)
		{
			std::cout << " " << std::setw(WIDTH) << mas[row][col];
		}

		std::cout << '\n';
	}
	// A fair C++ replacement for "system("pause")". Or a way to pause the program.
	// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
	//std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
	std::cout << "\n\n Press Enter to continue: ";
	std::cin.get();

	return 0;	
}

The first for loop shows you how you could just print out the table;

Lines 34 on show how you could use the array and store the information and then the next for loops will print it out.

Given you headings your original array does not have enough columns to hold everything that you need for each row.

Andy
Thank you, now I understand my mistakes!
Topic archived. No new replies allowed.