Hej!
Robiąc zadanie domowe z 11 odcinka podstawowego kursu c++ napotkałem problem. Przy pierwszym teście (https://youtu.be/63QjKBcIySs?t=847) program za liczbę najbliżej średniej podaje albo 11, albo -33. Wynik wydaje się być losowy i zmienia się przy ponownym uruchomieniu kodu/uruchomieniu i kompilacji.
Dla jasności powtórzę - te same dane, identyczny kod, raz działa, raz nie.
#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace::std;
float all=0, x;
int y;
int main()
{
float *table_1;
table_1 = new float [5];
float *pointer = table_1;
for (int i=0; i<5;)
{
cout << "Insert your " << ++i << " number: ";
cin >> *pointer;
all += *pointer;
pointer++;
system("cls");
}
pointer-=5;
all = all/5;
float *table_2;
table_2 = new float [5];
float *pointer2 = table_2;
for (int i=0; i<5; i++)
{
if (*pointer<all)
{
*pointer2=all-*pointer;
}
else if (*pointer>all)
{
*pointer2=*pointer-all;
}
else
{
x=*pointer;
break;
}
pointer++;
}
pointer2-=5;
x=*pointer2;
for (int i=0; i<5; i++)
{
if (x>*pointer2)
{
x=*pointer2;
y=i;
}
pointer2++;
}
cout << "Srednia: " << all << endl << "Najblizsza: " << table_1[y];
delete [] table_1;
delete [] table_2;
getch();
return 0;
}
PS Wiem, że kod nie przechodzi następnych testów, tym zajmę się po rozwiązaniu pierwszego problemu ;)