Read file, find min and max of numbers in array

boah.. das is kein Deutsch ;)
Also ich habs immernoch nicht verstanden.
Dein Programm findet doch den größten und kleinsten Wert, oder?
Versuchs mal auf English... aber kein Mix-Deutsch...
Oder lass dir in einem englischen Forum helfen, warum schreibst du eigentlich in ein Deutsches? Zum Lernen der Sprache?
 
So wie ich das verstanden habe, möchte er, dass sein Programm Zeile für Zeile das Maximum / Minumum herausfindet.
In seinem jetzigen Code wird das gesamte File berücksichtigt.

Ich liefer die Code-Veränderung, wenn ich ein bissel Zeit gefunden habe.
Aber in C++ und nicht in C.

Gruß, Christian
 
moin


Dann muss er doch einfach statt bis EOF nur bis '\n' einlesen.
Wenn dann irgendwann mal EOF statt '\n' kommt isser fertig.


mfg
umbrasaxum
 
Antwort fur Beichtpfarrer:

Ja, ich wissen, das ist nicht gut geschrieben in Deutsch (Grammatik,Zeitform und so weiter), aber ich hoffen, das Sie wurde mich verstehen trotzdem.
Ich mochte Hilfe, und Deutsch ist, bei Englisch, Sprache, welcher ich gut verstehen, aber ich habe keine interesee and diese forum Sprache lehrnen, nur Hilfe suchen.

Die aufgabe in Englisch lauten:

Make suitable functions, that are using pointers, that find in handed realistic sequence (float array) largest and smallest element. If functions are successful, returns address of largest and smallest element in array, else returns value NULL.
Write and test programm for test of both functions.
Reading of data must be adapted over suitable entry file, readout of results let goes on screen.
Programm must allow calculation of any number of tasks.



Entry file (for example - test1.txt):

0 1.1 2.2 3.3 21.1 22.2 23.3 53.5 54.5 55.5 83.3 84.4 92.8 93.6 94.6 98 99 100.1
-10.1 -9.2 -8.3 0 20.02 200.468 41.9 42.8 69.88 77.777 82.28 88.707 89.89 90.09
15.51 14.41 13.31 22.22 55.55 2.202 5.05


And results on screen should be:

ADDRESS AND VALUE OF MAX AND MIN ELEMENT IN ARRAY

Enter file name : test1.txt


1. task:
The address of max element is 4420144, with value 100.10000
The address of min element is 4420020, with value 0.00000

2. task:
The address of max element is 4420656, with value 200.46800
The address of min element is 4420002, with value -10.10000

3. task:
The address of max element is 4425648, with value 55.55000
The address of min element is 4420802, with value 2.20200


The adresses I have just imagine, values are as they are.
 
Zuletzt bearbeitet:
Ok, also dein Problem ist, dass du Zeilenweise einlesen möchtest?
In C++ könntest du das in etwa folgendermaßen machen:
Code:
#include<string>
#include<iostream>
#include<sstream>
#include<fstream>
#include<algorithm>
//#include<vector>
using namespace std;

int main(){
 string filename,line;
 int asize=1000;
 float* array = new float[asize];  //you could also use vector<float> to have a dynamic array, so you 
                                         //wouldnt have to care about buffer-overflows and could do an easier input

 cout << "Dateiname: ";
 getline(cin,filename);
 ifstream in(filename.c_str());

 while(getline(in,line)){
  istringstream istr(line);
  ival = 0;
  while(istr>>array[ival++]){
   if(ival==asize){  //buffer-overflow
     float* ptmp = array;
     array = new float[asize+=300];
     copy(ptmp,ptmp+ival,array);
     delete [] ptmp; } }
  if(ival==0)
   continue;
  cout << "\nnumber of elements: " << ival << endl;
  float* smallest = findmallest(array,ival),*biggest=findbiggest(array,ival);
  cout << "biggest: " << *biggest << " - address: " << biggest
         << "\nsmallest: " << *smallest << " - address: " << smallest << endl; }
 return 0; }
 
Thank you very much, Beichtpfarrer !

I do it this way (with some corections of yours code):
Code:
#include<string>
#include<iostream>
#include<sstream>
#include<fstream>
#include<algorithm>
//#include<vector>
using namespace std;


//function for max element in float array 
float *findbiggest(float array[],int ival) 
{ 
 int counter, counter_max; 
 float aretheysame=0; 

 for(counter=0; counter < ival; counter++) 
 { 
  if (*(array+counter) > aretheysame) 
  { 
   aretheysame = *(array+counter); 
   counter_max = counter; 
  } 
 } 
 return(&*(array+counter_max)); 
} 

//function for min element in float array 
float *findsmallest(float array[],int ival) 
{ 
 int counter, counter_max=0; 
 float aretheysame=array[0]; 

 for(counter=0; counter < ival; counter++) 
 { 
  if (*(array+counter) < aretheysame) 
  { 
   aretheysame = *(array+counter); 
   counter_max = counter; 
  } 
 } 
 return(&*(array+counter_max)); 
} 

int main()
{
 string filename,line;
 int asize=1000;
 float* array = new float[asize];  //you could also use vector<float> to have a dynamic array, so you 
                                         //wouldnt have to care about buffer-overflows and could do an easier input

 cout << "Dateiname: ";
 getline(cin,filename);
 ifstream in(filename.c_str());

 while(getline(in,line))
 {
  istringstream istr(line);
  int ival = 0;

  while(istr>>array[ival++])
  {
   if(ival==asize)
   {  //buffer-overflow
    float* ptmp = array;
    array = new float[asize+=300];
    copy(ptmp,ptmp+ival,array);
    delete [] ptmp; 
   } 
  }

    if(ival==0)
    continue;

    cout << "\nnumber of elements: " << ival << endl;

    float *smallest = findsmallest(array,ival);
    float *biggest = findbiggest(array,ival);

    cout << "biggest: " << *biggest << " - address: " << (int)biggest
         << "\nsmallest: " << *smallest << " - address: " << (int)smallest << endl; 
 }
 cin.ignore();
 return 0; 
}

The output of results on screen is:

Dateiname: test1.txt

number of elements: 19
biggest: 100.1 - address: 8061116
smallest: 0 - address: 8061048

number of elements: 15
biggest: 200.468 - address: 8061068
smallest: -10.1 - address: 8061048

number of elements: 8
biggest: 55.55 - address: 8061064
smallest: 2.202 - address: 8061068

I hope, that addresses are wright.
Besides of number of all elements in one line, i want, that programm count lines, and
each line is one of the tasks (see example output in last answer).

Could you solve this problem too ?
 
Well... counting lines doesn't require much complexity.
You just have to increment a variable each time at the beginning of the first whileloop.
...shouldn't be very hard...
 
Zurück