Skip to content

Instantly share code, notes, and snippets.

@pagenoare
Created December 11, 2012 00:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pagenoare/4254733 to your computer and use it in GitHub Desktop.
Save pagenoare/4254733 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <cstdlib>
#define N 100
using namespace std;
void wypelnij(int tab[])
{
for(int i = 0; i <= N; i++)
{
tab[i] = rand();
}
}
int sum(int tab[])
{
int n = 0;
for(int i = 0; i <= N; i++)
n += tab[i];
return n;
}
int max_min(int tab[], bool is_max=true)
{
int max = tab[0], min = tab[0];
for(int i = 0; i <= N; i++)
{
if(max < tab[i])
{
max = tab[i];
}
if(min > tab[i])
{
min = tab[i];
}
}
return is_max ? max : min;
}
int count_0(int tab[])
{
int n = 0;
for(int i = 0; i <= N; i++)
if(tab[i] == 0)
n++;
return n;
}
bool is_symetric(int tab[])
{
int j = 0;
for(int i = 0; i <= N / 2; i++)
if(tab[i] == tab[N-1-i])
j++;
return j == (N / 2) + 1 ? true : false;
}
bool ros(int tab[])
{
int l = 0;
for(int i = 0; i <= N; i++)
if(tab[i] < tab[i+1])
l++;
return l == N ? true : false;
}
int main()
{
int tab[N];
int act;
while(1)
{
cout << "=== MENU ===" << endl;
cout << "1. Wypelnienie. " << endl;
cout << "2. Sumowanie. " << endl;
cout << "3. Max i min. " << endl;
cout << "4. Ilosc zer. " << endl;
cout << "5. Sprawdz, czy tablica jest symetryczna. " << endl;
cout << "6. Sprawdz, czy tablica jest posortowana rosnaco. " << endl;
cin >> act;
switch(act)
{
case 1:
wypelnij(tab);
for(int i = 0; i <= N; i++)
{
cout << tab[i] << endl;
}
break;
case 2:
cout << sum(tab) << endl;
break;
case 3:
cout << "MAX: " << max_min(tab) << endl << "MIN: " << max_min(tab, false) << endl;
break;
case 4:
cout << "Ilosc 0: " << count_0(tab) << endl;
break;
case 5:
is_symetric(tab) ? cout << "symetryczna" : cout << "niesymetryczna";
cout << endl;
break;
case 6:
ros(tab) ? cout << "posortowana" : cout << "nieposortowana";
cout << endl;
break;
default:
cout << "Wybierz akcje!" << endl;
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment