Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <cstdlib>
- using namespace std;
- void minmax1(int tab[], int n, int &MIN, int &MAX)
- {
- int i = 2;
- if(n>=2)
- {
- if(tab[0]>tab[1])
- {
- MIN = tab[1];
- MAX = tab[0];
- }
- else
- {
- MIN = tab[0];
- MAX = tab[1];
- }
- while(i+2<=n)
- {
- if(tab[i]>tab[i+1])
- {
- if(tab[i]>MAX)
- MAX = tab[i];
- if(tab[i+1]<MIN)
- MIN = tab[i+1];
- }
- else
- {
- if(tab[i+1]>MAX)
- MAX = tab[i+1];
- if(tab[i]<MIN)
- MIN = tab[i];
- }
- i+=2;
- }
- if(n%2==1)
- {
- if(tab[i] > MAX)
- MAX = tab[i];
- if(tab[i] < MIN)
- MIN = tab[i];
- }
- }
- else
- {
- MIN = MAX = tab[0];
- }
- }
- void minmax2(int tabx[], int nx, int &MINx, int &MAXx)
- {
- int i = 2;
- if(nx>=2)
- {
- if(tabx[0]>tabx[1])
- {
- MINx = tabx[1];
- MAXx = tabx[0];
- }
- else
- {
- MINx = tabx[0];
- MAXx = tabx[1];
- }
- while(i+2<=nx)
- {
- if(tabx[i]>tabx[i+1])
- {
- if(tabx[i]>MAXx)
- MAXx = tabx[i];
- if(tabx[i+1]<MINx)
- MINx = tabx[i+1];
- }
- else
- {
- if(tabx[i+1]>MAXx)
- MAXx = tabx[i+1];
- if(tabx[i]<MINx)
- MINx = tabx[i];
- }
- i+=2;
- }
- if(nx%2==1)
- {
- if(tabx[i] > MAXx)
- MAXx = tabx[i];
- if(tabx[i] < MINx)
- MINx = tabx[i];
- }
- }
- else
- {
- MINx = MAXx = tabx[0];
- }
- }
- int main()
- {
- int MIN, MAX, tab[100];
- int n = 100;
- fstream plik1;
- plik1.open("pliki/plik.txt", ios::in) ;
- if (plik1.good() )
- {
- int x = 0;
- while (!plik1.eof())
- {
- plik1 >> tab[x];
- cout << tab[x] << " ";
- x++;
- }
- }
- cout << "----------------------------------------------------------------" <<endl;
- minmax1(tab, n, MIN, MAX);
- cout << "Wartosc MIN: " << MIN << endl;
- cout << "Wartosc MAX: " << MAX << endl;
- cout << " ------------------------------------------- " <<endl;
- int MINx, MAXx, tabx[100];
- int nx = 100;
- fstream plik2;
- plik2.open("pliki/plik2.txt", ios::in) ;
- if (plik2.good() )
- {
- int c = 0;
- while (!plik2.eof())
- {
- plik2 >> tabx[c];
- cout << tab[c] << " ";
- c++;
- }
- }
- cout << "----------------------------------------------------------------" <<endl;
- minmax2(tabx, nx, MINx, MAXx);
- cout << "Wartosc MIN: " << MINx << endl;
- cout << "Wartosc MAX: " << MAXx << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement