Skip to content

Instantly share code, notes, and snippets.

@pbrewczynski
Created January 10, 2015 23:30
Show Gist options
  • Save pbrewczynski/5ef7fe93f52ae9d6f626 to your computer and use it in GitHub Desktop.
Save pbrewczynski/5ef7fe93f52ae9d6f626 to your computer and use it in GitHub Desktop.
// uzywanie a1 a2 poza funkcją -- przesłanie tablicy i
#include<iostream>
using namespace std;
float *f(float *(tab[]), int length, float **pMin, float **pMax);
int main() {
//float tab[] = {9, 2, 5, 2, 5, 21};
//cout << tab[0] << endl << *(tab+0) << endl << *(tab+1) << endl;
float *tab = new float[6];
tab[0] = 9;
tab[1] = 2;
tab[2] = 5;
tab[3] = 2;
tab[4] = 5;
tab[5] = 21;
float *a1; // &min
float *a2; // &max
// float *f1 = f(&tab, 6, a1, a2);
f(&(tab), 6, &a1, &a2);
cout << "Pokaz co pod a1: " << *a1 << "\nPokaz co pod a2 " << *a2;
return 0;
}
float *f(float *(tab[]), int length, float **pMin, float **pMax) {
float min = (*tab)[0], max = (*tab)[0];
float *pm;
for (int i = 1; i<length; i++) {
if ((*tab)[i]>max) {
// *pMax wskazuje na pointer do floata
*pMax = ((*tab) + i);
//max = (*tab)[i];
}
if ((*tab)[i]<min) {
// tablice
*pMin = (*tab)+i;
min = (*tab)[i]; // *((*tab)+i);
//pm = &((*tab)[i]);
pm = (*tab)+i;
}
}
//pMin = &min;
//pMax = &max;
cout << "To co pod pMin : " << *pMin << "\n To co pod pMax : " << *pMax << endl;
return *pMax;
}*/
// ---- Po pierwszych poprawkach
#include<iostream>
using namespace std;
float *f(float tab[], int length, float *pMin, float *pMax);
int main() {
float tab[] = {9, 2, 5, 2, 5, 21};
float *a1;
float *a2;
float *f1 = f(tab, 6, a1, a2); // f1 na *f1
cout<< *f1; // f1 na *f1 bo chcemy NIE wskaźnik, tylko to co jest pod wskaźnikiem (f1).
return 0; // na początek nie zapominamy
}
float *f(float tab[], int length, float *pMin, float *pMax) {
float min = tab[0], max = tab[0];
float *pm;
for (int i = 1; i<length; i++) {
if (tab[i]>max)
max = tab[i];
if (tab[i]<min) {
min = tab[i];
pm = &tab[i];
}
}
pMin = &min;
pMax = &max;
return pm;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment