Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

easy(?) array sorting question

Status
Not open for further replies.

yaids

Programmer
Apr 1, 2005
11
AU
Hi,
Sorry if this question seems really stupid. For this code I'm reading in a text file containing numbers and retrieving the minimum and maximum numbers. (Each number is on a separate line in random order). I can work out the maximum number but can't work out the minimum number correctly.
Any help would be appreciated.


#include <iostream>
#include <fstream>

using namespace std;

int main(int argc, char *argv[]){

char buffer[255];
ifstream input(argv[1]);
double num[100];
int i = 0;
double max = num[0];
double min;

while(!input.eof()){
input.getline(buffer, 100);
num = atof(buffer);
line++;
i++;
}

for (int j = 0; j<line; j++) {
if (num[j] > max) {
max = num[j];
}
}

//this loop doesnt seem to work
for (int k = 1; k<line; k++) {
if (max > num[k]) {
min = num[k];
}
}
}
 
1st: Start the min loop with k = 0;
2nd: You're comparing if ( max > num[k] ) when you should be comparing if ( num[k] < min ).
3rd: Initialize double min = num[0];
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top