arrays

I have an txt file called arr.txt that looks like

4;3,2,5,6
2;1,5
3;4,6,8
.... (has an unknown amount of rows in the txt file)

The first value of the row is the array size and the values that follow is the values in the array. I would like to step through each row and do calculations with each array the row creates but I am unsure as to how because of that first value (size of the array). I want to use pointers to make these dynamic arrays of specific size as well.


1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

using namespace std;

int main()
{
	ifstream infile("arr.txt");
Last edited on
I want to use pointers to make these dynamic arrays of specific size as well.

It seems you are using C++ so don't use dynamic arrays.
The best solution is to use std::vector<std::vector<int>>
In this way you don't have to worry about memory management.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <fstream>
#include <sstream>
#include <valarray>
using namespace std;

int main()
{
// ifstream infile("arr.txt");
   istringstream infile( "4;3,2,5,6\n"
                         "2;1,5\n"
                         "3;4,6,8\n" );
   for ( int n; infile >> n; )
   {
      valarray<int> V(n);
      char c;
      for ( int i = 0; i < n; i++ ) infile >> c >> V[i];
      cout << V.sum() << '\n';    // Or whatever calculation
   }
}


16
6
18
Why dynamic arrays? Why not just a vector? Possibly - which doesn't rely upon the given array size but reads what's present:

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
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
#include <vector>

int main()
{
	std::ifstream ifs("arr.txt");

	if (!ifs)
		return (std::cout << "Cannot open file\n"), 1;

	for (std::string line; std::getline(ifs, line); ) {
		std::istringstream iss(line);
		size_t sz {};

		iss >> sz;
		iss.ignore();

		std::vector<int> vi;
		vi.reserve(sz);

		for (int no; iss >> no; vi.push_back(no), iss.ignore());

		// Process vector here
		for (const auto& v : vi)
			std::cout << v << ' ';

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



3 2 5 6
1 5
4 6 8

Last edited on
Petrus1234 wrote:
do calculations with


Tell us more: you might not need an array of any sort.
I would have liked to use arrays because I am unsure on how to do certain calculations or operations using vectors like how to find the highest value in the vector and if the highest value is odd to square all the values in the vector and then display it
Doing it with a vector is no different to doing it using an array - use the same syntax.
In mycode vi.size() gives the number read. Note that there are std functions to determine min/max etc. However, for things like min/max you'd probably determine this as part of reading the data - not as a separate process after the data has been read.
 if the highest value is odd to square all the values in the vector and then display it 


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
#include <iostream>
#include <fstream>
#include <sstream>
#include <valarray>
using namespace std;

template<typename T> ostream & operator << ( ostream & out, const valarray<T> &V )
{
   for ( T e : V ) out << e << " ";
   return out;
}

int main()
{
// ifstream infile("arr.txt");
   istringstream infile( "4;3,2,5,6\n"
                         "2;1,5\n"
                         "3;4,6,8\n" );
   for ( int n; infile >> n; )
   {
      valarray<int> V(n);
      char c;
      for ( int i = 0; i < n; i++ ) infile >> c >> V[i];
      int mx = V.max();
      cout << "Maximum is " << mx;
      if ( mx % 2 ) 
      {
         V *= V;
         cout << " - Odd. Squared values are  " << V << '\n';
      }
      else 
      {
         cout << " - Even. Original values are " << V << '\n';
      }
   }
}


Maximum is 6 - Even. Original values are 3 2 5 6 
Maximum is 5 - Odd. Squared values are  1 25 
Maximum is 8 - Even. Original values are 4 6 8 
Last edited on
Using dynamic memory - if you really want - then possibly something like:

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
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <memory>
#include <algorithm>

int main()
{
	std::ifstream ifs("arr.txt");

	if (!ifs)
		return (std::cout << "Cannot open file\n"), 1;

	for (size_t n; ifs >> n; ) {

		const auto vi {std::make_unique<int[]>(n)};
		char c {};

		for (size_t i = 0; i < n; ifs >> c >> vi[i++]);

		const auto mx {std::max_element(vi.get(), vi.get() + n)};

		std::cout << "Max is " << *mx << " - ";

		if (*mx % 2) {
			for (size_t i = 0; i < n; ++i)
				vi[i] *= vi[i];

			std::cout << "Odd.  Squared values are: ";
		} else
			std::cout << "Even. Original values are: ";

		for (size_t i = 0; i < n; ++i)
			std::cout << vi[i] << ' ';

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



Max is 6 - Even. Original values are: 3 2 5 6
Max is 5 - Odd.  Squared values are: 1 25
Max is 8 - Even. Original values are: 4 6 8

Last edited on
Topic archived. No new replies allowed.