Arrays

There are so many topics on arrays and I've read up some of the articles and forum questions, but I just can't seem to understand it.

I have to create a program that will input a line from a file one at a time. Then get the highest/lowest number.
Meaning the file looks like this:
45 67 34 78 89
34 12 67 32 78
12 45 76 89 34

I'm trying to figure out how to read it line by line. In which I read one line, (45, 67, 34, 78, 89) Print out the highest/lowest number and then move on down the row.

I'm a complete beginner at this, so please be gentle with me.

You'd need a loop that reads the file line by line, does stuff, and moves on down the row:

1
2
3
4
5
6
7
8
9
10
11
12
#include <fstream>
#include <string>
#include <iostream>
int main()
{
    std::ifstream f("filename.txt"); // <- your filename here
    std::string line; // we'll store each line here
    while(getline(f, line)) // this is the loop that reads line by line
    {
        std::cout << line << '\n'; // do stuff here
    }
}


If you run this, you will see that it prints out the lines. Always test the code as you write it.

Now, let's see what should the "do stuff here" line be replaced with: you have your line, such as "45 67 34 78 89" stored in the variable "line". You want those characters to become numbers, so you need to convert line from string to stream, and read numbers from the stream:

1
2
3
4
5
6
7
8
    // "do stuff here" becomes
    std::istringstream buf(line); // convert string to stream
    int n; // we'll store the integers here
    while(buf >> n)
    {
        std::cout << n << '\n'; 
    }
    std::cout << " done with the line! TODO: print min and max here\n";


again, compile and test to see that the numbers are printed. You will need an #include <sstream> at the beginning to use istringstream.

Now, you want min and max for each pass: you can remember the largest and the smallest integer seen in each loop... can you do that?

PS: Noting that you bring up arrays, arrays are not suitable for this task because you don't know the number of integers in the line beforehand. You could store the integers that are read from the istringstream into a vector and use the standard functions min_element() and max_element() (or minmax_element() if your compiler is recent) to obtain the results, but it's a waste of storage.
Last edited on
Hi! Thank you for the fast reply.

I tested out your program and it worked perfectly. However, I have to use arrays in this assignment.
Is the amount of numbers in the file always going to be the same? Is the amount of numbers in each row consistent? If you have to do it for any amount of numbers and you 'have' to use arrays then you will have to get into dynamically allocating memory. (Which seems odd for an assignment like this...).
Last edited on
There's actually a little more to the assignment. I was thinking that if I could get the basics, everything else would fall in place, but it seems I can't even get that right.

The numbers in the file is consistent, five in a row and five in a column.

I have to input the line in the file one at a time and print out the highest, lowest and the average of each row. Then I have to print out the highest among all the numbers.

I'm just studying up on arrays and it's kicking my butt.
you can do that easily without using array.
If you are not allowed to use string class or stringstream then there is alternative but that too don't require array.
Assuming you don't actually 'have' to get it 1 line at a time you can do it this way. Hopefully you can find max/min after that.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
	int Array[25];  // Creates an array with 25 spaces (as it's a 5x5 grid).  

	fstream File;
	File.open("Filename.txt");
	
	if(File.is_open()) // Makes sure file exists
	{
		for(int i = 0; i < 25; i++)  // Loops through 25 numbers
		{
			File >> Array[i];  //Reads next number in file and puts in array
			cout << Array[i] << endl;   //Prints out number
		}
	}

	File.close();


If you need to do it line by line then you will need to use stringstreams similar to how it's shown above then insert each number into an array from that.
Last edited on
Akshit: Yes I know :( I was halfway done with the program when my professor had indicated he wanted us to use arrays. It was really saddening.

Thank you James2250! I'm just going to play around with the program using this basis.

Topic archived. No new replies allowed.