May 7, 2014 at 3:13pm UTC
I wonder if i could shorten the following code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, char ** argv)
{
int Tabela[10];
int a,b,c,d,e,f,g,h,i,j;
float vsota=0;
float povprecje;
cin>>a>>b>>c>>d>>e>>f>>g>>h>>i>>j;
cout<<"Tabela: " ;
Tabela[0]=a;
Tabela[1]=b;
Tabela[2]=c;
Tabela[3]=d;
Tabela[4]=e;
Tabela[5]=f;
Tabela[6]=g;
Tabela[7]=h;
Tabela[8]=i;
Tabela[9]=j;
int najv = 0 ;
for (int i=0;i<10;i++)
{
cout<<Tabela[i]<<", " ;
vsota=vsota+Tabela[i];
if (Tabela[i]>najv)
{
najv= Tabela[i];
}
}
cout<<endl;
cout<<"Tabela v obratnem vrstnem redu: " ;
for (int j=9;j>=0;j--)
{
cout<<Tabela[j]<<", " ;
}
cout<<endl;
povprecje=vsota/10;
cout<<"Povprecje: " <<povprecje<< " ." <<endl;
int najm = Tabela[0] ;
for ( int i=1; i < sizeof (Tabela)/sizeof (Tabela[0]); i++ )
if ( Tabela[i] < najm )
{
najm = Tabela[i] ;
}
cout<<"Najmanjse: " << najm << "\n" ;
cout<<" Najvecje: " <<najv <<endl;
system("pause" );
return 0;
}
So is there a shorter way and can someone explain it to me. The task is : the programm must have the output "tabela(10 random numbers), this same array in the reverse order and then the largest("najvecje") and smallest("najmanjse") number and the average of this array. Is there a simple way to do this.
Vsota("sum") povprecje("average")
Last edited on May 7, 2014 at 3:14pm UTC
May 7, 2014 at 5:20pm UTC
vsota = vsota + Tabela[i];
shrink it to
vsota += Tabela[i];
it's more faster and efficient (especially when it comes to large objects)