Homework Problem: I am lost

I'm struggling with this class and cannot quite grasp some of these concepts. I am supposed to print the lowest value using these functions. I think I may have begun partially correct but I am coming across errors somewhere.

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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
  TODO: Write the declaration (prototype) for the function

	- Name: GetLeast
	  Parameter(s): an ifstream called infile (pass by reference - uses the &)
	  Returns: an int, the lowest value found in the file

	  HINT: The ifstream should be opened in main, before calling this function
*/

int getLeast(ifstream, int);

//------------------------------------------------------------------------------

int main()
{
	int smallestValue = INT_MAX;		// Initialize smallest to maximum range
	ifstream dataFile;									// File input stream
	const string filename = "data.txt";					// Input file name

	cout << "Opening file...";

	dataFile.open(filename.c_str());					// Open file

	if (dataFile)										// File opened OK?
	{
		cout << "file opened." << endl
			<< "Begin searching for lowest value...";

		/*	TODO:	Call function GetLeast, passing the dataFile ifstream variable
		and assign the return value to the smallestValue variable.
		*/

		getLeast(dataFile, smallestValue);



		cout << "done.\n" << endl;						// Print result

		cout << "The lowest value found was "
			<< smallestValue << endl;
	}
	else												// Problem opening file
	{
		cout << "could not find or open file: " << filename
			<< endl;
	}

	dataFile.close();

	cout << "\nEnd Program.\n"
		<< endl;

	return 0;
}

//------------------------------------------------------------------------------

/*	TODO: Define GetLeast(<params>)

    Refer to the function prototype above for parameter and
	return type info.
	
	PSEUDOCODE:
	     Declare two int variables (you decide on appropriate variable names):
	     * 1 variable to hold each value as it is read from file
		 * 1 variable to hold the lowest value read so far (initialize to INT_MAX)

		 Read the first value from the file into the appropriate local variable
		 Repeat while not at end of file
		    - Test to see if the new value is smaller than the lowest read so far
			     If yes, save the new value to the lowest so far variable
			Read the next value from the file
		End the repeat

		Return the smallest value that was read

*/

int getLeast(ifstream & in)
{
	int value;
	int lowest;

	in >> lowest;  

	while (in >> value)
	{
		
	}

	return lowest;
}


//------------------------------------------------------------------------------

/*	Sample program output:

Opening file...file opened.
Begin searching for lowest value...done.

The lowest value found was -9122

End Program.

Press any key to continue . . .

*/
You prob have a syntax error cause your prototype and implementation don't match -- one is ifstream with two(?) parameters and the other is just ifstream reference. Likely they both need to look like
int GetLeast(ifstream& ifs)
Next, once it compiles, you'll probably have a logical error, cause, well, you do nothing in the while loop. Read the comments more carefully:
1. Says function should be called GetLeast , with capital G
2. Says to initialize your 'lowest' variable to INT_MAX (Not in main; in the function!)
Last edited on
I thank you for the quick response. I corrected the more blatant errors that I had made. The program was able to compile but I had gotten a much larger number than what is expected in the sample.

This was what I inputted for the while loop but I am unsure on what to do here really.

int getLeast(ifstream & in)
{
int value;
int lowest = INT_MAX;

in >> lowest;

while (in >> value)
{
value = lowest;
}

return lowest;
}
Hello AlexPlanks,

Starting at the top:

Note that line does not start with "/*", so everything that follows ia not a comment.

Line 10 is wrong. The instructions say it should be int GetLeast(ifstream, int);. The "ifstream" is being passed by value not by reference as instructed. And the second parameter is optional.

When you compare lines 10, 33 and 79 all need to match and they do not. The prototype and the function both have two parameters, but the function definition only has one parameter.

Lines 10 and 79 need to match both in the return value, which is correct, and in the parameters which do not match.

Line 33. The function call returns a value that you never make use of, so on line 40 "smallestValue" still has the value of "INT_MAX".

For the function "GetLeast" the instructions say define two variables which you have done. I would suggest initializing all the variables in the program when they are defined. Line 82 should be initialized to "INT_MAX". Defining the two variables inside the function tends to negate the need for a second parameter in the function definition because the return value will take care of what you need.

Line 84 is redundant and will skip using the first number read. The while loop is all you need.
Inside the while you will need to:
- Test to see if the new value is smaller than the lowest read so far
If yes, save the new value to the lowest so far variable

Short of having an input file to work with I will have to fix the problems and come up with an input file to test it.

Hope that helps,

Andy
I appreciate the replies and I believe I am beginning to understand my mistakes now. However, my output still does not match the sample.

The output I get is:


Opening file...file opened.
Begin searching for lowest value...done.

The lowest value found was 2147483647

End Program.

Press any key to continue . . .
Hello AlexPlanks,

Line 33. The function call returns a value that you never make use of, so on line 40 "smallestValue" still has the value of "INT_MAX".

So where in main do you change the value of "smallestValue"??

Andy
Handy Andy wrote:
Line 10 is wrong. The instructions say it should be int GetLeast(ifstream, int);. The "ifstream" is being passed by value not by reference as instructed. And the second parameter is optional.

@Handy Andy -- I disagree. Looks like the instructions are in the comments. The prototype was AlexPlanks' attempt; he had two parameters but that was wrong, because instructions only want the one ifstream reference, and the function returns (not takes) an int.

Self-contained example, running at https://repl.it/repls/SlightSnivelingChapter , using istringstream, a stream for strings. This is very similar to how the ifstream works:

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

// Replace with ifstream
int GetLeast(istringstream& iss);

// ...

int GetLeast(istringstream& iss)
{
    int value;              // Holds your temporary value
    int lowest = INT_MAX;   // Initialized to a big number

    // While there are are still integers in the stream, 
    //   write one at a time to 'value'
    while (iss >> value)
    {
        // We found something lower; update our lowest
        if (value < lowest)
            lowest = value;
    }
    return lowest;
}

int main()
{
    const char* numbers_text = R"LITERAL(
21220 13603 3431  16651 19949
4776  20718 18824 15625 23199
16676 18895 381   22755 5721
23974 20662 4905  13770 5922
14561 20704 12173 12934 8633
9811  23609 20722 10756 23077
751   9381  950   451   2520
12303 9652  12881 12905 14202
4093  2685  15000 18929 5236
12649 6323  9701  2711  22700
14093 19960 19221 13499 19093
2286  14518 18163 23496 16379
22271 13104 18503 19331 22123
19739 1789  1518  18997 15651
7618  3779  4215  13592 1115
16664 13477 7421  5220  7688
556   865   8920  10265 4548
11413 22536 4834  7857  4378
12308 5423  2096  14767 20866
178   21877 11623 15615 22064
)LITERAL";

    // Imitate file stream
    istringstream iss(numbers_text);
    int low = GetLeast(iss);
    cout << "lowest found is "<<low<<endl;

    return 0;
}


Output:
lowest found is 178


I should note also that if the input contains a non-integer, e.g. random letters, the loop will stop and it would only find the lowest from the start until those characters were found.

Edit: Added comments to clarify what is happening.
Last edited on
Topic archived. No new replies allowed.