Finding the highest value inside a vector
Oct 17, 2015 at 10:01pm UTC
Hi, so I have a vector with a few double values and I need to find the highest of them. When I run this, the console freezes:
1 2 3 4 5 6 7 8
double highest(std::vector<double >& A)
{
double highest = A[0];
for (unsigned int i=1;i<A.size();i++)
if (A[i] > highest)
highest = A[i];
return highest;
}
If I do the same with normal arrays, it works just fine (n is the size of the array):
1 2 3 4 5 6 7 8
double highest(double A[], int n)
{
double highest = A[0];
for (int i=1;i<n;i++)
if (A[i] > highest)
highest = A[i];
return highest;
}
Where did I go wrong while doing it with a vector?
Oct 17, 2015 at 10:08pm UTC
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
#include <iostream>
#include <vector>
double highest(std::vector<double >);
int main()
{
std::vector<double > A = { 52.5, 9.2, 4.7, 97.3, 8.1 };
double largest = highest(A);
std::cout << "The highest is " << largest << std::endl;
return 0;
}
double highest(std::vector<double > A)
{
double highest = A[0];
for (unsigned int i = 1; i < A.size(); i++)
{
if (A[i] > highest)
{
highest = A[i];
}
}
return highest;
}
Notice carefully the function declaration/definition. No
&
reference needed.
Last edited on Oct 17, 2015 at 10:09pm UTC
Oct 17, 2015 at 10:19pm UTC
The reference is so that the whole vector isn't copied each time and doesn't take up as much memory. Your code works just as well with a reference.
Also your code is exactly the same as my code, I think there's a problem with how I write the values from a .txt file to the vector instead:
1 2 3 4 5 6
void read(std::vector<double >& A)
{
std::ifstream fd(Cfd);
for (unsigned int i=0;i<A.size();i++)
fd >> A[i];
}
Is this a valid way of writing from a file to a vector? Works well with arrays...
EDIT:
The .txt file would contain values separated by spaces such as:
4.99 6 7 42.1 964.99 0.99
Last edited on Oct 18, 2015 at 7:32am UTC
Oct 18, 2015 at 8:05am UTC
Absolutely nothing I can think of works:
1 2 3 4 5 6 7 8 9 10
void read(std::vector<double >& A)
{
std::ifstream fd(Cfd);
for (unsigned int i=0;i<A.size();i++)
{
double x;
fd >> x;
A[i] = x;
}
}
1 2 3 4 5 6 7 8 9 10
void read(std::vector<double >& A)
{
std::ifstream fd(Cfd);
for (unsigned int i=0;i<A.size();i++)
{
double x;
fd >> x;
A.push_back(x);
}
}
Can someone please tell me what am I doing wrong?..
Oct 18, 2015 at 8:37am UTC
Your code in function highest seems fine.
Post the entire working program. Please cut out all the unnecessary parts.
Oct 18, 2015 at 8:58am UTC
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
#include <iostream>
#include <fstream>
#include <iomanip>
#include <vector>
const char Cfd[] = "Data.txt" , Cfr[] = "Results.txt" ;
void read(std::vector<double >&), print(const std::vector<double >&, double ), descending(std::vector<double >&);
double highest(std::vector<double >&);
int main()
{
std::vector<double > A;
read(A);
descending(A);
print(A, highest(A));
return 0;
}
void read(std::vector<double >& A)
{
std::ifstream fd(Cfd);
for (unsigned int i=0;i<A.size();i++)
fd >> A[i];
}
double highest(std::vector<double >& A)
{
double highest = A[0];
for (unsigned int i=1;i<A.size();i++)
if (A[i] > highest)
highest = A[i];
return highest;
}
void descending(std::vector<double >& A)
{
for (unsigned int i=0;i<A.size();i++)
{
for (unsigned int j=i+1;j<A.size();j++)
if (A[i] < A[j])
{
double t = A[i];
A[i] = A[j];
A[j] = t;
}
}
}
void print(const std::vector<double >& A, double x)
{
std::ofstream fr(Cfr);
fr << "Highest value is " << std::fixed << std::setprecision(2) << x << std::endl;
for (unsigned int i=0;i<A.size();i++)
fr << A[i] << " " ;
}
In theory, if the Data.txt contains values
4.99 6 7 42.1 964.99 0.99
, the Results.txt should contain
1 2
Highest value is 964.99
964.99 42.10 7.00 6.00 4.99 0.99
Oct 18, 2015 at 9:19am UTC
1 2 3 4
void read(std::vector<double >& A)
{
std::ifstream fd(Cfd);
for (unsigned int i=0;i<A.size();i++)
The size of A is zero here.
Either define A to have 6 elements, or rewrite your read function to read until end of file.
Last edited on Oct 18, 2015 at 9:20am UTC
Oct 18, 2015 at 9:33am UTC
How can I make the function read until the end of file? Could you give an example please?
EDIT:
Also, if I use 6 instead of A.size(), it still freezes the console...
1 2 3 4 5 6
void read(std::vector<double >& A)
{
std::ifstream fd(Cfd);
for (unsigned int i=0;i<6;i++)
fd >> A[i];
}
Last edited on Oct 18, 2015 at 9:41am UTC
Oct 18, 2015 at 10:12am UTC
1 2 3
int main()
{
std::vector<double > A(6) ;
The aboove is unnecessary if you read until EOF:
1 2 3
double input;
while (fd>>input)
A.push_back(input);
Last edited on Oct 18, 2015 at 10:12am UTC
Oct 18, 2015 at 10:20am UTC
1 2 3 4 5 6 7
void read(std::vector<double >& A)
{
std::ifstream fd(Cfd);
double input;
while (fd>>input)
A.push_back(input);
}
Still the console freezes...
If I put the read function like it is above AND I add the (6) to the vector, it doesn't freeze, but the ouput file is
1 2
Highest value is 0.00
0.00 0.00 0.00 0.00 0.00 0.00
EDIT:
It wasn't reading the file properly for some odd reason, I deleted the old one and created a new one from scratch and now it works perfectly even without the (6). Thank you for the help!
Last edited on Oct 18, 2015 at 10:23am UTC
Topic archived. No new replies allowed.