Piszę program który tworzy tablicę losowo stworzonych elementów i ma odszukać największą wartość w tej tablicy.
Założenie było takie, że funkcja searching_highest w pętli for po kolei porównuje ze sobą elementy [i] i [i+1] i jeśli pierwszy jest większy od drugiego, to nadpisuje go do zmiennej highest.
Program bardzo dziwnie się zachowuje, poniżej kod:
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
void show_table(int table[], int table_size);
void searching_highest(int table_size, int* table);
int main ()
{
srand(time(NULL));
int table[50];
for (int i=0; i<50; i++)
{
table[i] = rand() % 100;
}
show_table(table, 50);
cout<<endl;
cout<<"Highest table element: ";
searching_highest(50, table);
}
void searching_highest(int table_size, int* table)
{
int highest;
for (int i=0; i<table_size; i++)
{
if (table[i]>table[i+1])
{
int buffer=table[i];
highest=table[i];
table[i]=buffer;
}
cout<<highest<<endl;
}
}
void show_table(int table[], int table_size)
{
cout<<"{";
for (int i=0; i<table_size; i++)
{
if (i !=0)
{
cout<<", ";
}
cout<<table[i];
}
cout<<"}";
}