seminare_c++_2011-2012.cpp

Shkarkuar 1113 herë. Ngarkuar më Shkurt 2012

seminare
c++
2011-2012
Shkarko Kthehu ne C++
Ushtrim 1: deklarim variabli
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int mosha;
    cout<<"jepni moshen ";
    cin>>mosha;
    if(mosha>=18)
    {
                 int x;
                 x=mosha;
                 cout<<"ju keni te drejten te votoni \n";
                 cout<<"mosha juaj eshte "<<x<<"\n";
    }
    else
                 cout<<"ju nuk keni te drejten te votoni \n";
   
    cout<<"mosha juaj eshte "<<x<<"\n";
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

Ushtrim 2 : konstante direkte ne sistem oktal dhe hekzadecimal

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int a,b;
    a=2;
    b=4;
    a=a*017;
    b=b*0xF;
    
    cout<<a<<"\n";
    cout<<b<<"\n";
  
    cout<<"pas kesaj nje sinjal zanor beeepp \a \n";
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

Ushtrim 3 : karakteret speciale

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    cout<<"rresht i ri \n";
    cout<<"Kjo eshte \t nje hapsire tab \n";
    cout<<"Fillimi i rreshtit \r kthe ne fillim \n";
    cout<<"Do jemi pak me poshte me rreshta \v nje tab me poshte \n";
    cout<<"Mos harroni apostrofet ne c\'do dhe cdo \n";
    cout<<"ckemi \? \n";
    cout<<"fshije nje shkronje\bE \n";
    cout<<"Sinjal zanor \a \n";
    cout<<"Ky eshte nje backslash \\ \n";
    cout<<"Ushtrim \"i veshtire\" shume \n";
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

Ushtrim 4 : vleredhenie komplekse

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int a=14;
    a+=15;
    cout<<a<<" mbledhje \n";
    a-=8;
    cout<<a<<" zbritje \n";
    a*=20;
    cout<<a<<" shumezim \n";
    a/=3;
    cout<<a<<" pjesetim\n";
    a%=130;
    cout<<a<<" mbetje\n";
    a<<=1;
    cout<<a<<" zhvendosje majtas \n";
    a>>=1;
    cout<<a<<" zhvendosje djathtas \n";
    a&=15;
    cout<<a<<" dhe bitesh\n";
    a|=15;
    cout<<a<<" ose bitesh \n";
    a^=10;
    cout<<a<<" XOR bitesh \n";
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

Ushtrim 5 : Konstante char

#include <cstdlib>
#include <iostream>
# define a "*"
using namespace std;

int main(int argc, char *argv[])
{
    cout<<a<<"\n";
    cout<<a<<a<<"\n";
    cout<<a<<a<<a<<"\n";
    cout<<a<<a<<"\n";
    cout<<a<<"\n";
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

Ushtrim 6 : sip�rfaqe dhe perimet�r te rrethit

#include <cstdlib>
#include <iostream>
# define pi 3.141
using namespace std;

int main(int argc, char *argv[])
{
    float rreze,sip,per;
    
    
    cout<<"jep rrezen e rrethit ne cm \n";
    cin>>rreze;
    per=2*pi*rreze;
    sip=pi*rreze*rreze;
    cout<<"siperfaqja eshte : "<<sip<<" cm2 \n";
    cout<<"perimetri eshte : "<<per<<" cm \n";
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}


Ushtrim 7: calculator

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int a,b,c;
    cout<<"jepni dy numrat:";
    cin>>a;
    cin>>b;
    cout<<"shuma eshte : "<<a+b<<"\n";
    cout<<"diff eshte : "<<a-b<<"\n";
    cout<<"shumezimi eshte : "<<a*b<<"\n";
    cout<<"pjestimi eshte : "<<a/b<<"\n";
    cout<<"moduli eshte : "<<a%b<<"\n";
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}


Ushtrim 8 operatoret e krahasimit 

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    bool b;
    int x,y;
    x=125;
    y=452;
    cout<<"------afishime 1------\n";
    cout<<x<<" me e madhe se "<<y<<" ? (0 per false, 1 per true) "<<(x>y)<<"\n";
    cout<<(x*7)<<" me e madhe se "<<y<<" ? (0 per false, 1 per true) "<<((x*7)>y)<<"\n";
    cout<<104<<" e barabarte me "<<y<<" ? (0 per false, 1 per true) "<<(104==y)<<"\n";
    cout<<(x+y)<<" me e vogel e barabarte me "<<y*3<<" ? (0 per false, 1 per true) "<<((x+y)<=(y*3))<<"\n";
    cout<<(54)<<" e ndryshme me "<<54<<" ? (0 per false, 1 per true) "<<(54!=54)<<"\n";
    cout<<"------afishime 2------\n";
    b=(x==y);
    cout<<x<<" e barabarte me "<<y<<"? "<<b<<"\n";
    b=(x+100)<y;
    cout<<x+100<<" me e vogel se "<<y<<"? "<<b<<"\n";
    
    system("PAUSE");
    return EXIT_SUCCESS;
}


Ushtrim 9  Ushtrim me prioritetet

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int i=0, j=1, k=-1;
    int n; bool b=false;
    float x=2.5, y=0.0;

   	n = i++;  
    cout<<"n= 0  "<<n<<"\n";				
	n = (i--); 	
    cout<<"n= 1  "<<n<<"\n";				
	n = --k;
    cout<<"n= -2 "<<n<<"\n";					
	//n = 5--;	
    cout<<"n= gabim"<<"\n";		
	n = i = k = 3; 	
    cout<<"n= 3  "<<n<<"\n";			
	n =  j  &&  i;
	cout<<"n= 1  "<<n<<"\n";	
	k = j << i;
	cout<<"k= 4  "<<k<<"\n";	
	k = j + n || ! j;
	cout<<"k= 1  "<<k<<"\n";	
	k = x * 5 && 5 || i / j;
	cout<<"k= 1 "<<k<<"\n";	
	n = j <= 10 && x >= 1 && j;
	cout<<"n= 1  "<<n<<"\n";	
    n = j <= x == i;
    cout<<"n= 0 "<<n<<"\n";	
	n =  ++j == i != y * 2;
	cout<<"n= 0 "<<n<<"\n";	
	//n = i--++--j;
	cout<<"n= gabim"<<"\n";	       
	b = (j%2==0)?-1:1;
	cout<<"b= -1  "<<b<<"\n";	
	n = ((i && j) || (j%k)) && (~i);
    cout<<"n= 0 "<<n<<"\n";	
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

Ushtrim 10 Madhesia e tipeve
 
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    cout<<"Madhesia e tipeve \n \n";
    cout<<"Tipi char :"<<sizeof(char)<<" \n";
    cout<<"Tipi short :"<<sizeof(short)<<" \n";
    cout<<"Tipi long :"<<sizeof(long)<<" \n";
    cout<<"Tipi int :"<<sizeof(int)<<" \n"; 
    cout<<"Tipi float :"<<sizeof(float)<<" \n";
    cout<<"Tipi double :"<<sizeof(double)<<" \n";
    cout<<"Tipi long double :"<<sizeof(long double)<<" \n";
    cout<<"Tipi bool :"<<sizeof(bool)<<" \n";
    cout<<"Tipi wchar_t :"<<sizeof(wchar_t)<<" \n";
     
    system("PAUSE");
    return EXIT_SUCCESS;
}

Ushtrim 11 : nderrimi i tipit te variablave

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    float xf=4.23568f;
    int xi=478;
    double xd=0.258963146;
    short xs=123;
    long xl=458796321;
    
    cout<<"numri float eshte :"<<xf<<"\n";
    xi=(int)xf;
    cout<<"konvertimi nga float ne int : nga "<<xf<<" ne "<<xi<<"\n";
    
    xf=(float)xi;
    cout<<"konvertimi nga int ne float : nga "<<xi<<" ne "<<xf<<"\n";
    
    xf=(float)xd;
    cout<<"numri double eshte :"<<xd<<"\n";
    cout<<"konvertimi nga double ne float : nga "<<xd<<" ne "<<xf<<"\n";
    
    xd=(double)xf;
    cout<<"konvertimi nga float ne double : nga "<<xf<<" ne "<<xd<<"\n";
    
    xi=3265863;
    xs=(short)xi;
    cout<<"konvertimi nga int ne short : nga "<<xi<<" ne "<<xs<<"\n";
    
    xi=326;
    xs=(short)xi;
    cout<<"konvertimi nga int ne short : nga "<<xi<<" ne "<<xs<<"\n";
    
    xl=(long)xi;
    cout<<"konvertimi nga int ne long : nga "<<xi<<" ne "<<xl<<"\n";
    
    xl=452469463L;
    xs=(short)xl;
    cout<<"konvertimi nga long ne short : nga "<<xl<<" ne "<<xs<<"\n";
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

Ushtrimi nr 12 : Te perdoren kanalet e komunikim me konsolen

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int x,y,z,v;
    cout<<"jepni tre numra te plote \n";
    cin>>x>>y>>z;
    cout<<"shuma e tyre eshte "<<x+y+z<<endl;
    cout<<"moduli i te parit me te dytin eshte : "<<x%y<<endl;
    cout<<"pjestimi i te dytit me te tretin : "<<y/z<<endl;
    cout<<"jepni edhe nje numer tjeter \n";
    cin>>v;
    cout<<"shuma ne total eshte : "<<x+y+z+v<<endl;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}
Shembull ekzekutimi:
jepni tre numra te plote
1 3 4
shuma e tyre eshte 8
moduli i te parit me te dytin eshte : 1
pjestimi i te dytit me te tretin : 0
jepni edhe nje numer tjeter
7
shuma ne total eshte : 15



Shembull ekzekutimi kur jepen qe ne fillim 5 nr, si nr i katert eshte marre vlera 4

jepni tre numra te plote
1 2 3 4 5
shuma e tyre eshte 6
moduli i te parit me te dytin eshte : 1
pjestimi i te dytit me te tretin : 0
jepni edhe nje numer tjeter
shuma ne total eshte : 10

Ushtrim nr 13: te shk�mbehen vlerat e variablave a dhe b,duke perdore nje variabel te perkohshem

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int a,b,temp;
    a=14;
    b=78;
    cout<<"vlerat fillestare: "<<a<<"  "<<b<<endl;
    temp=a;
    a=b;
    b=temp;
    cout<<"vlerat pas shkembimit: "<<a<<"  "<<b<<endl;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

Shembull ekzekutimi:
vlerat fillestare: 14  78
vlerat pas shkembimit: 78  14


Ushtrim nr 14: te shk�mbehen vlerat e kater variablave a, b, c dhe d,duke perdore nje variabel te perkohshem temp.

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int a,b,c,d,temp;
    a=14;
    b=78;
    c=2;
    d=51;
    cout<<"vlerat fillestare: \t"<<a<<"  "<<b<<"  "<<c<<"  "<<d<<endl;
    temp=d;
    d=c;
    c=b;
    b=a;
    a=temp;
     cout<<"vlerat pas shkembimit: \t"<<a<<"  "<<b<<"  "<<c<<"  "<<d<<endl;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

Shembull ekzekutimit :
vlerat fillestare:      14  78  2  51
vlerat pas shkembimit:  51  14  78  2

Ushtrim 14: te shk�mbehen vlerat e dy variablave pa p�rdorur nje variabel te trete te perkohshem

#include <cstdlib>
#include <iostream>

using namespace std;
int main(int argc, char *argv[])
{
    int a, b;
    cout<<"jepni numrin e pare \n";
    cin>>a;
    cout<<"jepni numrin e dyte \n";
    cin>>b;
    a=a+b;
    b=a-b;
    a=a-b;
    cout<<"vlerat e reja jane : "<<a<<" dhe "<<b<<endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}
Ushtrim 15: shkruani nje program qe konverton temperaturen nga celsius ne fahrenheit dhe anasjelltas

Versioni 1

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
   float c,f,temp;
   int opsion;
   cout<<"Jepni temperaturen \n";
   cin>>temp;
   cout<<"Shtypni 1 per temperaturen ne celcius, ose 2 per fahrenheit \n";
   cin>>opsion;
   if(opsion==1){
                f=9.0/5.0*temp+32;
                cout<<"temperatura e konvertuar ne fahrenheit eshte : "<<f<<endl;
   }
   else
       if(opsion==2){
                    c=5.0/9.0*(temp-32);
                    cout<<"temperatura e konvertuar ne celcius eshte : "<<c<<endl;
       }
       else
                     cout<<"opsion i pavlefshem, provoni perseri \n";
    system("PAUSE");
    return EXIT_SUCCESS;
}

Versioni 2
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
   float c,f,temp;
   char opsion;
   cout<<"Jepni temperaturen \n";
   cin>>temp;
   cout<<"Shtypni c per temperaturen ne celcius, ose f per fahrenheit \n";
   cin>>opsion;
   if(opsion=='c'){
                f=9.0/5.0*temp+32;
                cout<<"temperatura e konvertuar ne fahrenheit eshte : "<<f<<endl;
   }
   else
       if(opsion=='f'){
                    c=5.0/9.0*(temp-32);
                    cout<<"temperatura e konvertuar ne celcius eshte : "<<c<<endl;
       }
       else
                     cout<<"opsion i pavlefshem, provoni perseri \n";
    system("PAUSE");
    return EXIT_SUCCESS;
}


Versioni 3

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
   float c,f,temp;
   char opsion;
   cout<<"Jepni temperaturen ne celcius \n";
   cin>>temp;
   f=9.0/5.0*temp+32;
   cout<<"Kjo temperature ne fahrenheit eshte : "<<f<<endl;
   cout<<"Jepni temperaturen ne fahrenheit \n";
   cin>>temp;
   c=5.0/9.0*(temp-32);
   cout<<"Kjo temperature ne celcius eshte : "<<c<<endl;
   
    system("PAUSE");
    return EXIT_SUCCESS;
}
Versioni 4
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
   float c,f,temp;
   char opsion;
   cout<<"Jepni temperaturen \n";
   cin>>temp;
   cout<<"Shtypni c per temperaturen ne celcius, ose f per fahrenheit \n";
   cin>>opsion;
   switch (opsion)
   {
    case 'c':
   
                f=9.0/5.0*temp+32;
                cout<<"temperatura e konvertuar ne fahrenheit eshte : "<<f<<endl;
                break;
    case 'f':             
       
                c=5.0/9.0*(temp-32);
                cout<<"temperatura e konvertuar ne celcius eshte : "<<c<<endl;
                break;
    default:
                cout<<"opsion i pavlefshem, provoni perseri \n";
    }
    system("PAUSE");
    return EXIT_SUCCESS;
}

Ushtrim 16: Gjeni dallimet mes kodit me cikel for dhe atij me cikel while

	#include <iostream>
	using namespace std;

	int main()
	{
		int count = 1;
		while (count <= 5)
		{
			int count = 1;
			cout << count << "\n";
			count++;
		}
		return 0;
	}
	
	
	#include <iostream>
	using namespace std;
						
	int main()
	{
		int count = 1;
		for (; count <= 5 ; count++)
		{
			int count = 1;
			cout << count << "\n";
		}
		return 0;
	}

Ushtrim 17: Duke perdore case, gjeni sa dite ka muaji.

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    

	int muaj;
	cout<<"jepni numrin per muajin \n";
	cin>>muaj;

	switch(muaj)
	{
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
             cout<<" muaji ka 31 dite \n";
			 break;
        case 2:
             	int viti;
	            cout<<" ne kete rast jepni edhe vitin \n";
               	cin>>viti;
             switch(viti)
             {
                 case 2000:
                 case 2004:
                 case 2008:
                           cout<<" muaji ka 29 dite\n";
		                   break;
                 case 2012:
                 case 2016:
                           cout<<" muaji do te kete 29 dite \n";
		                   break;
                  default :
                           cout<<" muaji ka 28 dite \n";     
             }
             break;
	    case 4:
		case 6:
		case 9:
		case 11:
			 cout<<" muaji ka 30 dite \n";
		     break;
		default:
			 cout<<" 12 muaj ka viti, jo me shume..\n"; 
    }

    system("PAUSE");
    return EXIT_SUCCESS;
}

Ushtrim 18 Shkruani nje program i cili kerkon perdoruesit te shtype nje numer te plote dhe shkruan "Ju fituat" nqs vlera eshte mes 56 dhe 87 ( te dy keto numra perfshihen ). Nqs numri i dhene nuk eshte mes ketyre vlerave atehere afishohet " Ju keni humbur".

Zgjidhje
#include<iostream>
using namespace std;
 
int main() 
{
   int value;
   cout << "please enter any integer : ";
   cin >> value;
   if ( (value >= 56) && (value <= 78) )
      cout << "YOU WIN" << endl;
   else
      cout << "YOU LOSE" << endl;
}


Zgjidhje alternative :
#include<iostream>
using namespace std;
 
int main(){
    int input;
    cout << "enter a number through 1-100\n";
    cin>> input;
    if( input <56 || input >78){
        cout<< "YOU LOSE"<<endl;
        }
        else{
             cout<<"YOU WIN"<<endl;
 
             }
        return main();
}


Shikoni kete zgjidhje.

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int nr;
    char option;
start:    cout<<"\nJepni numrin tuaj te fatit: ";
    cin>>nr;
    if(nr>=56&&nr<=78)
    {
		cout<<"\nJu fituat!";
	}
    else
    {
		cout<<"\nJu keni humbur!";
	}
	pyetje: cout<<"\n\nDoni te provoni fatin perseri? [Y][N]";
	cin>>option;
	switch(option)
	{
		case 'Y' : goto start;
		case 'y' : goto start;
		case 'n' : return 0;
		case 'N' : return 0;
		default: cout<<"\nInput i pavlefshem";
		goto pyetje;
	}
    
    system("PAUSE");
    return EXIT_SUCCESS;
}


Shikoni edhe kete variant :

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
   int input = 0;
   cout<<"Jepni nje numer \n";
    cin >> input;
    
    while(!((input >= 56) && (input <= 78)))
    {
         cout << "Ju keni humbur. Provo perseri me nje nr tjeter " << endl;
         cin >> input;
    }
    cout << "Ju keni fituar" << endl;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

Variant alternativ :

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
   int num;
        char again = 'y';
        while (again == 'y') {
                cout << "Ju lutem jepni nje numer nga 1 deri ne 100: ";
                cin >> num;
                if (num >= 56 && num <= 78) {
                        cout << "Urime.Ju keni fituar.\n";
                        again = 'n';
                }else {
                        cout << "Ju keni humbur.Doni te provoni perseri ? [y][n]: ";
                        cin >> again;
                }
        }
    system("PAUSE");
    return EXIT_SUCCESS;
}

Shikoni dhe kete variant
Provoni te jepni qe ne fillim nr 60

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
   cout<<"Ju lutem shtypni nje numer te rastit...";
    int num;
    cin>>num;
    if (78>=num>=56)//kushti eshte gabim duhet if((78>=num)&&(num>=56))
       cout<<"Ju keni fituar me numrin e pare!"<<endl;
    else{
         while (num>78 || num<56){
               cout<<"Ju keni humbur!!!"<<endl;
               cout<<"Provoni perseri: ";
               cin>>num;
         }
    }
    cout<<"Ju keni fituar!"<<endl;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}



Shikoni dhe kete variant :

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
   int nr;
   char zgjedhje;
 start:
   cout<<"Shtypni nr tuaj: ";
   cin>>nr;
   if ((nr>=56)&&(nr<=78)){
      cout<<"Ju keni fituar\n";
   }
   else
      cout<<"Ju keni humbur\n";
      cout<<"Doni te provoni perseri? [y][n]";
      cin>>zgjedhje;
      if ((zgjedhje=='y')||(zgjedhje=='Y')){
         goto start;
      }
      else
         goto end;
 end:
    
    system("PAUSE");
    return EXIT_SUCCESS;
}


Ushtrimi nr 19 : Shkruani nje program qe kerkon nga perdoruesi te jape te gjithe numrat mes 8 dhe 23 duke perdorur nje cikel for.


#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
   for (short int i=8, input; i <= 23; i+=(i == input) ) {
        cout << "Shtypni numrin " << i << ": ";
        cin >> input;
    }
 
    system("PAUSE");
    return EXIT_SUCCESS;
}


Zgjidhje alternative

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
  int num;
    cout<<"Shkruani te gjithe numrat nga 8 deri te 23\n";
    cout<<"Fillo tani: ";
    for(int i=8;i<=23;){
        cin>>num;
        cout<<"tjetri: ";
        if (num!=i){
           cout<<"Numer i pasakte, tjetri eshte : "<<i<<endl;
           cout<<"tjetri: ";
        }
        else
           i++;
    }
    cout<<"Urime!!\n";
    system("PAUSE");
    return EXIT_SUCCESS;
}


Shikoni kete variant jo te plote dhe gjeni problemin
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
  int a,b;
  start: 
         
  cout<<"\nShkruani numrat nga 8-23";
  cin>>b;
  if((b<8)||(b>23))
  {
         cout<<"pasakte";
         goto start;
  }
  else
  {
      for(a=8;a<23;a++) 
       {
            cin>>b;
            if(b>23)
            {
                 cout<<"\nKalon vlerat e inputit";
                 goto start;
            }
       } 
   }
    system("PAUSE");
    return EXIT_SUCCESS;
}

Varianti me i pritshem per tu dhene :

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
  int typedInt = 0;
    cout << "Shkruani te gjithe numrat nga 8 deri te 23: " << endl;
    for(int i = 8; i <= 23; i++)
    {
            cin >> typedInt;
            if (typedInt != i)
            {
               cout << "Gabim, numri i rradhes eshte : " << i << endl;             
            }
    }    
    system("PAUSE");
    return EXIT_SUCCESS;
}


Varianti alternativ, ngjason me variantin me lart..gjeni ndryshimin..

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
   int num, next(8);
        for (; next < 25;) {
                cout << "jepni numrin: ";
                cin >> num;
                if (num == next) {
                        next++;
                }else {
                        cout << "Numer i pasakte, vlera e rradhes eshte: " << next << endl;
                        
                       
                }
        }
    cout << "Urime\n";
    system("PAUSE");
    return EXIT_SUCCESS;
}

Variant alternativ 

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
  int i=0;
        int askNum =0;
        for(int i=8; i <= 23 ; i++)
        {
goUp:
                cout << endl;
                cout << "Shkruani numra mes 8 dhe 23 ;numri i rradhes :" << i << endl;
                cin >> askNum;
 
                if(8 <= askNum && 23 >= askNum)
                {
                        cout << "Numri i rradhes : " << askNum << endl; 
                }else{
                        cout << "Ju dhate nje numer gabim.." << endl;
                        goto goUp;
                }
        }
    cin >> i;
    system("PAUSE");
    return EXIT_SUCCESS;
}

Shikoni dhe kete variant alternativ:

  #include <iostream>
using namespace std;
 
int main()
{
  int a = 8;
  int number;
  cout << "Shkruani te gjithe numrat nga  8 - 23.\n";
  for(; a < 24;)
  {
    cin >> number; 
    if(number == a)
    {
      a++; 
      cout << "Numri i rradhes:\n";
    }
    else
      cout << "?";
  }
    system("PAUSE");
    return EXIT_SUCCESS;
}

Ushtrim 20
Te njejten kerkese zgjidheni me nje cikel while

#include <iostream>
using namespace std;
 
int main() {
    short int input, i(8); 
 
    while (i <= 23) {
        cout << "Enter the number " << i << ": ";
        cin >> input;
        i += (input == i); // hyn dhe njehere ne cikel vetem nqs numri eshte i sakte
    }
 
    return(0);
}
 
 Zgjidhje alternative
 
 #include <iostream>
using namespace std;
 
int main()
{
  int a = 8, number;
  cout << "Jepni numra nga 8 deri te 23\n";
  while(a < 24)
  { 
    cin >> number;
    if(a == number)
    {
      cout << "Numri i rradhes: ";
      a++;
    }
    else
      cout << "?";
  }
  return 0; 
}

Zgjidhje alternative

#include<iostream>
using namespace std;
 
int main(){
    int num, i=8;
    cout<<"Shtypni te gjithe numrat nga 8 deri te 23\n";
    cout<<"fillo: ";
    while (i<=23){
        cin>>num;
        cout<<"numri: ";
        if (num!=i){
           cout<<"Numer i pasakte, numri i rradhes eshte: "<<i<<endl;
           cout<<"numri: ";
        }
        else
           i++;
    }
    cout<<"Urime!!\n";
return 0;
}

Zgjidhje alternative

#include <iostream>
using namespace std;
 
int main()
{
    int typedInt = 0;
    int i = 8;
    cout << "Shkruani numrat mes  8 dhe 23: " << endl;
    while(i <= 23)
    {
       cin >> typedInt;
       if(typedInt != i)
       {
          cout << "Keni ngaterruar rradhen e numrave" << i << endl;            
       }
       else
       {
          i++;
       }
    }    
}

Zgjidhje alternative 

#include <iostream>
using namespace std;
 
void main() {
        int num, next(8);
        while (next < 25) {
                cout << "Ju lutem jepni nje numer: ";
                cin >> num;
                if (num == next) {
                        next++;
                }else {
                        cout << "Vlere e pasakte, numri i rradhes: " << next << endl;
                        system("pause");
                        return;
                }
        }
        cout << "Urime\n";
        system("pause");
}


Ushtrimi 21:Shkruani nje program qe kerkon nga perdoruesi te shtype 10 numra te plote dhe afishon shumen e ketyre numrave.


#include <iostream>
using namespace std;
 
int main()
{
    int i, nr, shuma=0;
    cout<<"jepni 10 numra qe doni te mblidhni\n";
    for(i=1;i<=10;i++){
          
          cout<<"\njepni numrin e "<<i<<"-te ";
          cin>>nr;
          shuma=shuma+nr;
    }   
    cout<<"shuma e ketyre numrave eshte : "<<shuma<<endl;
              
    system("PAUSE");
    return EXIT_SUCCESS;
}

Ushtrimi 22: Shkruani nje program qe konverton shifrat e nje numri ne menyre te tille qe shifrat e pare eshte e fundit, etj. Psh numrin 371 e konverton ne 173.

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int n,n1,sh;
    n1=0;
    sh=0;
    cout<<"jep nje numer te plote \n";
    cin>>n;
    while(n>0)
    {
      sh=n%10;
      n1=n1*10+sh;
      n=n/10;            
    }
    cout<<"numri pas konvertimit eshte : "<<n1<<endl;
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}


Ushtrimi 23: Shkruani nje program qe kerkon nga perdoruesi te jape nje nr te plote nga 0 deri te 20 (te dyja te perfshira). Programi afishon N+17. Nqs perdoruesi ka dhene nje vlere jashte ketij intervali, atehere programi afishon GABIM dhe kerkon nje tjeter vlere nga perdoruesi, derisa ai te jape vleren e sakte brenda intervalit 0..20.Te perdoret cikli do..while.


#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int n;
    cout<<"jepni nje numer nga 0 ne 20 \n";
   do{
       cin>>n;
       if((n<0)||(n>20)) 
           cout<<"provo perseri \n";
   }  
   while(!((n>=0)&&(n<=20)));
   n=n+17;
    cout<<"numri i mbledhur me vleren 17 eshte : "<<n<<endl;
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

Ushtrimi 24: te njejten kerkese por me ciklin while.

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int n;
    cout<<"jepni nje numer nga 0 ne 20 \n";
    cin>>n;
    while(!((n>=0)&&(n<=20)))
    { 
           cout<<"provo perseri \n";
           cin>>n;
    }  
    n=n+17;
    cout<<"numri i mbledhur me vleren 17 eshte : "<<n<<endl;
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}


Ushtrimi 25: Te ndertohet nje program qe merr dy numra nga tastiera per rreshtat dhe shtyllat dhe ne varesi te tyre afishon figuren e meposhtme.Psh per rreshta dhe shtylla = 4 eshte figura meposhte :

* * * *
 * * * *
* * * *
 * * * *


#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int rreshta,shtylla;
    cout<<"jepni numrin e rreshtave dhe shtyllave \n";
    cin >> rreshta;
    cin >> shtylla;
    for(int i=1;i<=rreshta;i++){
       for(int j=1;j<=shtylla;j++){
               if((i%2==0)&&(j==1))
                  cout<<" ";
               cout<<(" *"); 
       }
       cout<<endl;
    }
       
    system("PAUSE");
    return EXIT_SUCCESS;
}

Ushtrimi 26: Te ndertohet nje program qe merr nje vlere numerike nga p�rdoruesi, si dhe nje zgjedhje per opsionet :
1: mbledhje me 20
2: shum�zime me 3
3: pjes�tim me 4
4: dalje nga programi

#include<iostream>
using namespace std;
 
int main()
{

int x=0,zgjedhje;
cout<<"jep nje numer te plote "<<endl;
cin>>x;
do
    {
    cout<<"Ju dhate nr  "<<x<<endl;
    cout<<"1 : Mbledhje me 20"<<endl;
    cout<<"2 : Shumezim me 3"<<endl;
    cout<<"3 : Pjesetim me 4"<<endl;
    cout<<"4 : Dalje nga programi "<<endl;
    cout<<"Zgjedhja juaj eshte: ";
    cin>>zgjedhje;
 
    switch(zgjedhje)
        {
        case 1: cout<<"Vlera e x eshte "<<x+20<<endl; break ;
        case 2: cout<<"Vlera e x eshte "<<x*3<<endl; break;
        case 3: cout<<"Vlera e x eshte "<<x/4<<endl;break;
        
        }
}while(zgjedhje!=4);
cout<<"Vlera perfundimtare e x eshte : "<<x<<endl;
return 0;
}

Shembull Leksioni me thirrje me reference

#include <cstdlib>
#include <iostream>

using namespace std;
void rritMeN(int x,int y)
{
     int n;
     cout<<"jep nje vlere";
     cin>>n;
     x=x+n;
     y=y+n;
     
    cout<<"ktej nga anet tona jemi keshtu "<<endl;
    cout<<"x = "<<x<<" y= "<<y<<endl;
}
int main(int argc, char *argv[])
{
    int a,b;
    a=4;
    b=17;
    cout<<"para thirrjes"<<endl;
    cout<<"a = "<<a<<" b= "<<b<<endl;
    rritMeN(a,b);
    cout<<"pas thirrjes"<<endl;
    cout<<"a = "<<a<<" b= "<<b<<endl;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

Ne console:

para thirrjes
a = 4 b= 17
jep nje vlere2
ktej nga anet tona jemi keshtu
x = 6 y= 19
pas thirrjes
a = 4 b= 17
Press any key to continue . . .


Ushtrimi nr 1: te shkruhet nje program ne C++ qe llogarit dhe afishon Shumen e n termave te pare te vargut : 1-5+9-13�.
#include <cstdlib>
#include <iostream>

using namespace std;
//shuma=1-5+9-13....
int main(int argc, char *argv[])
{
    int sh,s,i,j,n;
    cout<<"jepni nje numer n kufizash \n";
    cin>>n;
    s=0;
    sh=1;
    i=1;
    for(j=1;j<=n;j++){
       s=s+sh*i;
       sh=-sh;
       i=i+4;
    }      
    cout<<"\nShuma e "<<n<<" termave te pare do jete"<<endl;
    cout<<s<<endl;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

Shembuj per tabelat


Shembull 1: nje variable i vetem

#include <iostream>
 
 int main()
  {
     short mosha;
     mosha=23;
     cout << mosha << endl;
     return 0;
   }




Shembull 2: Nje tabele me moshat
#include <iostream>
   
   int main()
   {
     short mosha[4]; 
     mosha[0]=23;
     mosha[1]=34;
     mosha[2]=65;
     mosha[3]=74;
    return 0;
  }

Shembull 3: Variabli mosha tregon adresen e elementin te pare

#include <iostream>
   
   int main()
   {
     short mosha[4]; 
     mosha[0]=23;
     mosha[1]=34;
     mosha[2]=65;
     mosha[3]=74;
  
    cout << mosha << endl;
    return 0;
  }
Ne console :
0x22ff70
Press any key to continue . . .

Shembull 4 : Afishohen elementet e tabeles

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
   short mosha[4]; 
     mosha[0]=23;
     mosha[1]=34;
     mosha[2]=65;
     mosha[3]=74;
    cout << mosha[0] << endl;
    cout << mosha[1] << endl;
    cout << mosha[2] << endl;
    cout << mosha[3] << endl;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

Ne console:
23
34
65
74
Press any key to continue . . .

Shembull 5: Barazim mes tabelash nuk me lejon kompilatori

#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
   short mosha[4]; 
     short nr_viteve[4];
     mosha[0]=23;
     mosha[1]=34;
     mosha[2]=65;
    mosha[3]=74;
  
    nr_viteve=mosha;
  
   cout << nr_viteve[0] << endl;
    cout << nr_viteve[1] << endl;
    cout << nr_viteve[2] << endl;
    cout << nr_viteve[3] << endl;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

Shembull 6: Shembull kopjimi i tabelave 

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
   short mosha[4]; 
     short nr_viteve[4];
     
     mosha[0]=23;
     mosha[1]=34;
    mosha[2]=65;
    mosha[3]=74;
  
    nr_viteve[0]=mosha[0];
    nr_viteve[1]=mosha[1];
    nr_viteve[2]=mosha[2];
    nr_viteve[3]=mosha[3];
  
    cout << nr_viteve[0] << endl;
    cout << nr_viteve[1] << endl;
    cout << nr_viteve[2] << endl;
    cout << nr_viteve[3] <<endl;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}
Ne console 
23
34
65
74
Press any key to continue . . .

Shembull 7 : me cikel for, e njejta gje eshte me shkurt

#include <iostream>
   
   int main()
   {
     short mosha[4]; 
     short nr_viteve[4];
     int i, j;
     mosha[0]=23;
     mosha[1]=34;
    mosha[2]=65;
    mosha[3]=74;
  
    for(i=0; i<4; i++)
      nr_viteve[i]=mosha[i];
  
    for(j=0; j<4; j++)
      cout << nr_viteve[j] <<endl;
    return 0;
  }

Shembull 8: Tabele me numra double

#include <iostream>
using namespace std;

int main()
{
	double distanca[] = {44.14, 720.52, 96.08, 468.78, 6.28};

	cout << "elementi 2 = " << distanca[1] << endl;
	cout << "elementi 5 = " << distanca[4] << endl;

	return 0;
}

Shembull 9: afishohen te gjithe elementet e tabeles 
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
   double distanca[] = {44.14, 720.52, 96.08, 468.78, 6.28};

	cout << "Distanca 1: " << distanca[0] << endl;
	cout << "Distanca 2: " << distanca[1] << endl;
	cout << "Distanca 3: " << distanca[2] << endl;
	cout << "Distanca 4: " << distanca[3] << endl;
	cout << "Distanca 5: " << distanca[4] << endl;

    
    system("PAUSE");
    return EXIT_SUCCESS;
}


Distanca 1: 44.14
Distanca 2: 720.52
Distanca 3: 96.08
Distanca 4: 468.78
Distanca 5: 6.28
Press any key to continue . . .

Shembull 10 : vini re deklarimin me konstante te deklaruar per numrin e elementeve

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
   const int nr = 5;
	double distanca[nr] = {44.14, 720.52, 96.08, 468.78, 6.28};

	cout << "Distance 1: " << distanca[0] << endl;
	cout << "Distance 2: " << distanca[1] << endl;
	cout << "Distance 3: " << distanca[2] << endl;
	cout << "Distance 4: " << distanca[3] << endl;
	cout << "Distance 5: " << distanca[4] << endl;

    
    system("PAUSE");
    return EXIT_SUCCESS;
}

Shembull 11 : Kjo konstante perdoret dhe si gjatesi tabele ne for

#include <iostream>
using namespace std;

int main()
{
	const int nr_elementeve = 5;
	double distanca[nr_elementeve] = {44.14, 720.52, 96.08, 468.78, 6.28};

	cout << "elementet ne tabele \n";
	for(int i = 0; i < nr_elementeve; ++i)
		cout << "Distanca " << i + 1 << ": " << distanca[i] << endl;

	return 0;
}

Shembull 12: Llogaritje e numrit te elementeve 

#include <iostream>
using namespace std;

int main()
{
	double distanca[] = {44.14, 720.52, 96.08, 468.78, 6.28};
	int index = sizeof(distanca) / sizeof(double);

	cout << "Elementet ne tabele \n";
	for(int i = 0; i < index; ++i)
		cout << "Distanca : " << i + 1 << distanca[i] << endl;

	return 0;
}

Shembull 13 : pamje pertej limitit

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
  const int nr_elementeve = 5;
	double distanca[nr_elementeve] = {44.14, 720.52, 96.08, 468.78, 6.28};
	cout << "Distance 1: " << distanca[0] << endl;
	cout << "Distance 2: " << distanca[1] << endl;
	cout << "Distance 3: " << distanca[2] << endl;
	cout << "Distance 4: " << distanca[3] << endl;
	cout << "Distance 5: " << distanca[4] << endl;
	cout << "Distance 6: " << distanca[5] << endl;
	cout << "Distance 7: " << distanca[6] << endl;
	cout << "Distance 8: " << distanca[7] << endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}
Ne console :
Distance 1: 44.14
Distance 2: 720.52
Distance 3: 96.08
Distance 4: 468.78
Distance 5: 6.28
Distance 6: 1.6978e-313
Distance 7: 5.21242e+291
Distance 8: 1.1642e-313
Press any key to continue . . .

Shembull 14 : vlerat e tabeles I jep perdoruesi

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
 const int nr = 5;
	int faqe[nr];

	cout << "jep numrin e faqeve te librit \n";
	cout << "Libri 1: ";
	cin >> faqe[0];
	cout << "Libri 4: ";
	cin >> faqe[3];

	cout << "\n permbledhje :";
	cout << "\nLibri 1: " << faqe[0] << " faqe";
	cout << "\nLibri 4: " << faqe[3] << " faqe\n";

    system("PAUSE");
    return EXIT_SUCCESS;
}

Ne console :
jep numrin e faqeve te librit
Libri 1: 20
Libri 4: 40

 permbledhje :
Libri 1: 20 faqe
Libri 4: 40 faqe
Press any key to continue . . .
Shembull 15 : elementet e tabeles jepet nga perdoruesi

Llogaritet shuma e elementeve te tabeles
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
 const int max = 10;
	int nr[max];

	int shuma = 0;

	cout << "Jepni 10 nr te plote.\n";

	for( int i = 0; i < max; i++ )
	{
		cout << "Nr " << i + 1 << ": ";
		cin >> nr[i];
		shuma += nr[i];
	}

	cout << "\n\nShuma e ketyre numrave eshte: " << shuma << "\n\n";

    system("PAUSE");
    return EXIT_SUCCESS;
}
Jepni 10 nr te plote.
Nr 1: 4
Nr 2: 8
Nr 3: 2
Nr 4: 3
Nr 5: 4
Nr 6: 1
Nr 7: 2
Nr 8: 5
Nr 9: 3
Nr 10: 1


Shuma e ketyre numrave eshte: 33

Press any key to continue . . .

Shembull 16 : ne tabele kerkohet per nje numer qe jep perdoruesi

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
 int numra[] = {11, 22, 33, 44, 55, 66, 77, 88};
	int gjej;
	int i, m = 8;
	cout << "Jep numrin qe do kerkosh: ";
	cin >> gjej;
	for (i = 0; (i < m) && (numra[i] != gjej); ++i)
	// cikli vazhdon per aq kohe sa elementi nuk u gjet, ose
	// deri ne fund te tabeles
		continue;
	if (i == m)
		cout << gjej << " nuk eshte ne liste" << endl;
	else
		cout << gjej << " eshte elementi i : " << i + 1
		        << " ne liste " << endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}

Ne console :
Jep numrin qe do kerkosh: 77
77 eshte elementi i : 7 ne liste
Press any key to continue . . .

Shembull 17 : kerkohet per minimumin e tabeles

Hap pas hapi afishohet min local

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
 int numra[] = {8, 25, 4, 44, 52, 1, 75, 0};
	int minimum = numra[0];
	int a = 8;

	for (int i = 1; i < a; ++i) {
		if (numra[i] < minimum)
			minimum = numra[i];
	       cout << "minimumi aktual eshte: "
	       << minimum << "." << endl;
	}
	
	cout << "Numri me i vogel ne tabele eshte: "
	       << minimum << "." << endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}
 Ne console :
minimumi aktual eshte: 8.
minimumi aktual eshte: 4.
minimumi aktual eshte: 4.
minimumi aktual eshte: 4.
minimumi aktual eshte: 1.
minimumi aktual eshte: 1.
minimumi aktual eshte: 0.
Numri me i vogel ne tabele eshte: 0.
Press any key to continue . . .

Shembull 18 : Maximumi I tabeles

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
 int numra[] = {8, 25, 36, 44, 52, 60, 75, 89};
	int maximum = numra[0];
	int a = 8;

	for (int i = 1; i < a; ++i) {
		if (numra[i] > maximum)
			maximum = numra[i];
	}

	cout << "Numri me i madh ne tabele eshte : "
	        << maximum << "." << endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}
Ne console :
Numri me i madh ne tabele eshte : 89.
Press any key to continue . . .


Shembull 19 : procedure qe afishon tabelen

#include <iostream>
using namespace std;

void AfishoTabelen(double element[5]);

int main()
{
	const int nr_elementeve = 5;
	double distanca[nr_elementeve] = {44.14, 720.52, 96.08, 468.78, 6.28};
    AfishoTabelen(distanca[nr_elementeve]);
	cin.get();
	return 0;
}

void AfishoTabelen(double element[5])
{
	for(int i = 0; i < 5; ++i)
		cout << "\nDistance " << i + 1 << ": " << element[i];
	cout << endl;
}

Shembull 20 : I njejti ushtrim, vini re prototipin


#include <iostream>
using namespace std;

void AfishoTabelen(double element[]);

int main()
{
	const int nr_elementeve = 5;
	double distanca[nr_elementeve] = {44.14, 720.52, 96.08, 468.78, 6.28};

	return 0;
}

void AfishoTabelen(double element[]){
	for(int i = 0; i < 5; ++i)
		cout << "\nDistance " << i + 1 << ": " << element[i];
	cout << endl;
}

Shembull 21 : Afishim i pjesshem i tabeles


#include <iostream>
using namespace std;


void AfishoTabelen(double el[], int nisje, int fund);

int main()
{
	double distanca[] = {44.14, 720.52, 96.08, 468.78, 6.28, 68.04, 364.55, 6234.12};

	cout << "Elementet e tabeles";
	AfishoTabelen(distanca, 2, 6);

	return 0;
}
void AfishoTabelen(double element[], int nisje, int fund)
{
	for(int i = nisje; i < fund; ++i)
		cout << "\nDistance " << i + 1 << ": " << element[i];
	cout << endl;
}
Ne console 

Elementet e tabeles
Distance 3: 96.08
Distance 4: 468.78
Distance 5: 6.28
Distance 6: 68.04
Press any key to continue . . .




Shembull 22 : afishim matrice

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
double distanca[2][4] = {44.14, 720.52, 96.08, 468.78, 6.28, 68.04, 364.55, 6234.12};

	cout << "Elementet e tabeles";
	cout << "\nDistance [0][0]" << ": " << distanca[0][0];
	cout << "\nDistance [0][1]" << ": " << distanca[0][1];
	cout << "\nDistance [0][2]" << ": " << distanca[0][2];
	cout << "\nDistance [0][3]" << ": " << distanca[0][3];
	cout << "\nDistance [1][0]" << ": " << distanca[1][0];
	cout << "\nDistance [1][1]" << ": " << distanca[1][1];
	cout << "\nDistance [1][2]" << ": " << distanca[1][2];
	cout << "\nDistance [1][3]" << ": " << distanca[1][3];

	cout << endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}

Ne console:

Elementet e tabeles
Distance [0][0]: 44.14
Distance [0][1]: 720.52
Distance [0][2]: 96.08
Distance [0][3]: 468.78
Distance [1][0]: 6.28
Distance [1][1]: 68.04
Distance [1][2]: 364.55
Distance [1][3]: 6234.12
Press any key to continue . . .

Shembull 23: Ose deklarim dhe inicializim me dy rreshta dhe kater shtylla

#include <iostream>
using namespace std;
int main()
{
	double distanca[][4] = {
				{ 44.14, 720.52,  96.08,  468.78 },
				{  6.28,  68.04, 364.55, 6234.12 }
			     };

	cout << "Elementet e tabeles";
	for(int i = 0; i < 2; ++i)
		for(int j = 0; j < 4; ++j)
			cout << "\nDistance [" << i << "][" << j << "]: " << distanca[i][j];

	cout << endl;
	return 0;
}

Ushtrim 24: Ndertoni nje program qe deklaron dhe inicializon nje matrice dhe qe afishon kete matrice

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int i,j;
    int matrica[3][4]={
                       {11,74,23,77},
                       {77,44,11,66},
                       {14,22,10,12} };
    cout<<"Matrica e dhene eshte :"<<endl;
    for(i=0;i<3;i++)
    {
        for(j=0;j<4;j++)
        { 
           cout<<matrica[i][j]<<"   ";
        }   
        cout<<endl;
    }                            
    
    system("PAUSE");
    return EXIT_SUCCESS;
}


Ne console :
Matrica e dhene eshte :
11   74   23   77
77   44   11   66
14   22   10   12
Press any key to continue . . .

Ushtrim 25 : ndertoni nje program qe gjen shumen e elementeve te nje vektori, gjatesia e te cilit dhe vlerat jepen nga perdoruesi.Gjithashtu afishon max dhe min per elementet.
 
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int i,shuma,n,max,min;
    cout<<"jep nje numer per gjatesine e vektorit"<<endl;
    cin>>n;
    
    int vektori[n];
    for(i=0;i<n;i++)
    {
     cout<<"jep elementin "<<(i+1)<<endl;
     cin>>vektori[i];                
    }
    shuma=0;
    max=vektori[0];
    min=vektori[0];
    for(i=0;i<n;i++)
    {
         shuma=shuma+vektori[i];
         if(vektori[i]>max)
            max=vektori[i];
         if(vektori[i]<min)
            min=vektori[i];
    }                            
    cout<<"shuma totale e elementeve te vektorit eshte : "<<shuma<<endl;
    cout<<"maksimumi i elementeve te vektorit eshte : "<<max<<endl;
    cout<<"minimumi i elementeve te vektorit eshte : "<<min<<endl;
  
    system("PAUSE");
    return EXIT_SUCCESS;
}

Ne console :
jep nje numer per gjatesine e vektorit
5
jep elementin 1
-9
jep elementin 2
6
jep elementin 3
7
jep elementin 4
1
jep elementin 5
2
shuma totale e elementeve te vektorit eshte : 7
maksimumi i elementeve te vektorit eshte : 7
minimumi i elementeve te vektorit eshte : -9
Press any key to continue . . .


Ushtrime me stringat

Ushtrim 1 : Dallimi mes cin>>tekst  dhe cin.getline(tekst,100)

Me cin :

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    char tekst [100];
    cout<<"shkruani nje tekst "<<endl;
    cin>>tekst;
    cout<<"teksti eshte : "<<tekst<<endl;
     system("PAUSE");
    return EXIT_SUCCESS;

}

Ne console :
shkruani nje tekst
stringa 1
teksti eshte : stringa
Press any key to continue . . .

Me cin nuk merret pertej hapsires boshe (space)

Me cin.getline merret stringa edhe me hapsirat boshe qe mund te permbaje :

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    char tekst [100];
    cout<<"shkruani nje tekst "<<endl;
    cin.getline(tekst,100);
    cout<<"teksti eshte : "<<tekst<<endl;
     system("PAUSE");
    return EXIT_SUCCESS;

}

Ne console :
shkruani nje tekst
stringa 1 me hapsira ndermjet fjaleve
teksti eshte : stringa 1 me hapsira ndermjet fjaleve
Press any key to continue . . .



Ushtrim 2 : Me strcat bej lidhjen ndermjet stringave

#include <cstdlib>
#include <iostream>
#include <string.h>
using namespace std;

int main(int argc, char *argv[])
{
    char liste[100]="";
    char tekst [20]="";
    cout<<"Lista e ushqimeve tuaja te preferuara: "<<endl;
    cout<<"1-";
    cin.getline(tekst,100);
    strcat(liste,tekst);
    strcat(liste," & ");
    cout<<"2-";
    cin.getline(tekst,100);
    strcat(liste,tekst);
    strcat(liste," & ");
    cout<<"3-";
    cin.getline(tekst,100);
    strcat(liste,tekst);
    cout<<"lista e plote e ushqimeve tuaja te preferuara eshte : "<<endl;
    cout<<liste<<endl;
     system("PAUSE");
    return EXIT_SUCCESS;

}

Ne console :

Lista e ushqimeve tuaja te preferuara:
1-gjelle me patate
2-peshk
3-pizza
lista e plote e ushqimeve tuaja te preferuara eshte :
gjelle me patate & peshk & pizza
Press any key to continue . . .

Shembull me strncat

Ushtrim 3 : Strncat lidh me stringen destinacion n-elementet e para qe percaktohen ne funksion

#include <cstdlib>
#include <iostream>
#include <string.h>
using namespace std;

int main(int argc, char *argv[])
{
    char inicialet[5]="";
    char tekst [20]="";
    cout<<"Shkruani emrin tuaj"<<endl;
    cin.getline(tekst,20);
    strncat(inicialet,tekst,1);
    strcat(inicialet,".");
    cout<<"Shkruani mbiemrin tuaj"<<endl;
    cin.getline(tekst,20);
    strncat(inicialet,tekst,1);

    cout<<"Inicialet e emrit tuaj jane : "<<inicialet<<endl;

     system("PAUSE");
    return EXIT_SUCCESS;

}
Ne console :
Shkruani emrin tuaj
sidita
Shkruani mbiemrin tuaj
duli
Inicialet e emrit tuaj jane : s.d
Press any key to continue . . .

Ushtrim 4 : Strcmp krahason dy stringa, nqs jane te njejta kthehet vlera 0

#include <cstdlib>
#include <iostream>
#include <string.h>
using namespace std;
int main(int argc, char *argv[])
{
    char user1[20]="";
    char user2 [20]="";
    int result;
    cout<<"Shkruani username"<<endl;
    cin.getline(user1,20);
    cout<<"Perseriteni username"<<endl;
    cin.getline(user2,20);

    result=strcmp(user1,user2);
    if(result==0)
        cout<<"Te dhenat tuaja jane te sakta "<<endl;
    else
        cout<<"Te dhenat gabim "<<endl;
     system("PAUSE");
    return EXIT_SUCCESS;

}
Ne console :
Shkruani username
admin
Perseriteni username
user
Te dhenat gabim
Press any key to continue . . .
Shembull 5 me strcpy ku stringa destinacion do jete si stringa e dhene burim. Ky shembull lejon te log-ojme tre here ne sistem me emra te ndryshem perdoruesish

#include <cstdlib>
#include <iostream>
#include <string.h>
using namespace std;

int main(int argc, char *argv[])
{
    char emri[20]="";
    char tekst [20]="";

    cout<<"Shkruani emrin tuaj"<<endl;
    cin.getline(tekst,20);
    strcpy( emri,tekst);// kopjohet stringa tekst ne stringen emri
    cout<<"Pershendetje Z/Znj "<<emri<<endl;
    cout<<"Log-out nga sistemi.Tjeter perdorues"<<endl;
    cout<<"Shkruani emrin tuaj "<<endl;
    cin.getline(tekst,20);
    strcpy( emri,tekst);
    cout<<"Pershendetje Z/Znj "<<emri<<endl;
    cout<<"Log-out nga sistemi.Tjeter perdorues"<<endl;
    cout<<"Shkruani emrin tuaj "<<endl;
    cin.getline(tekst,20);
    strcpy( emri,tekst);
    cout<<"Pershendetje Z/Znj "<<emri<<endl;
     system("PAUSE");
    return EXIT_SUCCESS;
}
Ne console:
Shkruani emrin tuaj
nerina
Pershendetje Z/Znj nerina
Log-out nga sistemi.Tjeter perdorues
Shkruani emrin tuaj
pranvera
Pershendetje Z/Znj pranvera
Log-out nga sistemi.Tjeter perdorues
Shkruani emrin tuaj
rexhina
Pershendetje Z/Znj rexhina
Press any key to continue . . .


Shembull 6 me strncpy, vini re inicializimin e stringes boshe

#include <cstdlib>
#include <iostream>
#include <string.h>
using namespace std;

int main(int argc, char *argv[])
{
    char emri[20]="";
    char tekst [20]="";
    char username[4]={'\0'};
    cout<<"Shkruani emrin tuaj.\n";
    cout<<"Username per kete program do jete 3 germat te emrit tuaj "<<endl;
    cin.getline(tekst,20);
    strcpy(emri,tekst);
    strncpy(username,emri,3);
    cout<<"Pershendetje Z/Znj "<<emri<<endl;
    cout<<"Username juaj eshte "<<username<<endl;
    cout<<"Heren tjeter logoni me kete username "<<endl;

     system("PAUSE");
    return EXIT_SUCCESS;

}
Ne console:
Shkruani emrin tuaj.
Username per kete program do jete 3 germat te emrit tuaj
nerina
Pershendetje Z/Znj nerina
Username juaj eshte ner
Heren tjeter logoni me kete username
Press any key to continue . . .

Shembull 7 me strlen
Kontrollohet per gjatesine e emrit, nqs eshte me pak se 6 shkronja, quhet jo i vlefshem

#include <cstdlib>
#include <iostream>
#include <string.h>
using namespace std;



int main(int argc, char *argv[])
{
    char emri[20]="";
    char tekst [20]="";
    int gjatesi;
    cout<<"Shkruani emrin tuaj.\n";
    cout<<"Username per kete program do jete i njejte me emrin tuaj "<<endl;
    cin.getline(tekst,20);
    strcpy(emri,tekst);
    gjatesi=strlen(emri);
    if(gjatesi<6)
          cout<<"Nuk eshte nje username i vlefshem"<<endl;
    else
    {
     cout<<"Pershendetje Z/Znj "<<emri<<endl;
     cout<<"Username juaj eshte "<<emri<<endl;
    }
     system("PAUSE");
    return EXIT_SUCCESS;

}

Ne console :
Shkruani emrin tuaj.
Username per kete program do jete i njejte me emrin tuaj
admin
Nuk eshte nje username i vlefshem
Press any key to continue . . .

Ushtrime me shenjuesa

Ushtrim 1 : ndertoni dy tabela me karaktere dhe nje shenjues
dhe afishoni tabelat me ane te shenjuesit

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    char s1[8]={'A','B','C','D','\0'};
    char s2[8]={'q','w','e','r','\0'};
    
    char *sh;
    sh=s1;
    for(int i=0;i<strlen(s1);i++)
    {
       cout<<*sh<<"  ";
       sh++;     
    }
    sh=s2;
    cout<<"\n";
    for(int i=0;i<strlen(s2);i++)
    {
       cout<<*sh<<"  ";
       sh++;     
    }
    system("PAUSE");
    return EXIT_SUCCESS;
}

Ne console

A  B  C  D
q  w  e  r  Press any key to continue . . .


Ushtrimi 2: Te ndertohet nje shenjues dinamik, qe shenon ne nje numer te plote, qe I jep vlere numrit te plote dhe qe ne fund duhet te lirohet memorja e zene. Kujdes, nqs nuk ka vend ne memorje, afishohet nje mesazh dhe perfundon program ne menyre jo normale.

#include <cstdlib>
#include <iostream>

using namespace std;
int main(int argc, char *argv[]){
   int nr;
   int *sh=new int;
   if(sh==NULL){
        cout<<"Nuk ka vend ne memorje";        
        exit(1);        
                }
   sh=&nr;
   //ne vend te nr=17; shkruaj
   *sh=17;
   // ne adresen e nr, qe eshte realisht vlera e sh
   // eshte permbatja e nr ... qe realisht eshte *sh
   cout<<"ne adresen "<<sh<<" eshte vlera"<<*sh<<endl;
   delete sh;
    system("PAUSE");
    return EXIT_SUCCESS;
}
Ne console:
ne adresen 0x22ff74 eshte vlera17
Press any key to continue . . .


Ushtrimi 3: Te ndertohet nje shenjues dinamik, qe shenon ne nje tabele me numra te plote, qe I jep vlere numrave te plote dhe qe ne fund duhet te lirohet memorja e zene. Kujdes, nqs nuk ka vend ne memorje, afishohet nje mesazh dhe perfundon program ne menyre jo normale.

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
   //int nr;
   int tab[10];
   int *sh=new int[10];
   if(sh==NULL){
        cout<<"Nuk ka vend ne memorje";        
        exit(1);        
   }
   for(int i=0;i<10;i++){
    sh[i]=i;          
   }
   for(int i=0;i<10;i++){
    cout<<"elementi i rradhes "<<sh[i]<<endl;
   }
   delete [] sh;
    system("PAUSE");
    return EXIT_SUCCESS;
}
Ne console :
elementi i rradhes 0
elementi i rradhes 1
elementi i rradhes 2
elementi i rradhes 3
elementi i rradhes 4
elementi i rradhes 5
elementi i rradhes 6
elementi i rradhes 7
elementi i rradhes 8
elementi i rradhes 9
Press any key to continue . . .

Ushtrime me Strukturat

Ushtrim 1: Te ndertohet nje structure per te dhenat e studentave

#include <stdio.h>
#include <iostream.h>
#include  <cstdlib>
#include <string.h>
using namespace std;

struct student{
    char  emri[20];
    char  mbiemri [20];
    char  dega[50] ;
    int viti;
  } iliri, jesmira, dori;

 void listoStudentat(student st);  //prototipi

int main(int argc, char *argv[])
{

  strcpy(iliri.emri,"Ilir");	//ky funksion strcpy DUHET perdore
  strcpy(iliri.mbiemri,"Xhabiri");
  strcpy(iliri.dega,"informatike");
  iliri.viti=2;

  strcpy(jesmira.emri,"Jesmira");
  strcpy(jesmira.mbiemri,"Dibra");
  strcpy(jesmira.dega,"Informatike");
  jesmira.viti=2;

  strcpy(dori.emri,"Dorjan");
  strcpy(dori.mbiemri,"Troja");
  strcpy(dori.dega,"Informatike");
  dori.viti=2;

  listoStudentat(dori);        // thirret procedura per secilin student
  listoStudentat(jesmira);
  listoStudentat(iliri);


  system("PAUSE");
  return 0;
}

void listoStudentat(student st){
    cout<<"Te dhenat per kete student \n";
    cout<<"Emri : "<<st.emri<<"\t Mbiemri : "<<st.mbiemri<<endl;
    cout<<"Dega : "<<st.dega<<"\t Viti : "<<st.viti<<endl;
    cout<<"-----------------------------------------\n";
}

Ne console :
Te dhenat per kete student
Emri : Dorjan    Mbiemri : Troja
Dega : Informatike       Viti : 2
-----------------------------------------
Te dhenat per kete student
Emri : Jesmira   Mbiemri : Dibra
Dega : Informatike       Viti : 2
-----------------------------------------
Te dhenat per kete student
Emri : Ilir      Mbiemri : Xhabiri
Dega : informatike       Viti : 2
-----------------------------------------
Press any key to continue . . .

Ushtrim 2
I njejti ushtrim por te dhenat te futen nga vete perdoruesi

#include <stdio.h>
#include <iostream.h>
#include  <cstdlib>
#include <string.h>
using namespace std;

struct student{
    char  emri[20];
    char  mbiemri [20];
    char  dega[50] ;
    int viti;
  } stdReg;

 void listoStudentat(student st);

int main(int argc, char *argv[])
{
  char tekst [50];
  cout<<"Jepni te dhenat per studentin \n";
  cout<<"Emri : ";
  cin.getline(tekst,50);
  strcpy(stdReg.emri,tekst);
  cout<<"Mbiemri : ";
  cin.getline(tekst,50);
  strcpy(stdReg.mbiemri,tekst);
  cout<<"Dega : ";
  cin.getline(tekst,50);
  strcpy(stdReg.dega,tekst);
  cout<<"Viti : ";
  cin.getline(tekst,50);
  stdReg.viti=atoi(tekst);

  cout<<"Studenti sapo u regjistrua ne program : \n";
  listoStudentat(stdReg);

  system("PAUSE");
  return 0;
}

void listoStudentat(student st){
    cout<<"Te dhenat per kete student \n";
    cout<<"Emri : "<<st.emri<<"\t Mbiemri : "<<st.mbiemri<<endl;
    cout<<"Dega : "<<st.dega<<"\t Viti : "<<st.viti<<endl;
    cout<<"-----------------------------------------\n";
}
Ne console :
Jepni te dhenat per studentin
Emri : Ylber
Mbiemri : Hasmujaj
Dega : Informatike
Viti : 2
Studenti sapo u regjistrua ne program :
Te dhenat per kete student
Emri : Ylber     Mbiemri : Hasmujaj
Dega : Informatike       Viti : 2
-----------------------------------------
Press any key to continue . . .



Ushtrim 3 : ne te njejtin shembull te perdoren pointerat per te marre te dhenat e structures

#include <stdio.h>
#include <iostream.h>
#include  <cstdlib>
#include <string.h>
using namespace std;

struct student{
    char  emri[20];
    char  mbiemri [20];
    char  dega[50] ;
    int viti;
  } dori;


 void listoStudentat(student st);

int main(int argc, char *argv[])
{
    student * pstud;    // deklarimet ketu per incializim te pointerit
    pstud = &dori;

  char tekst [50];
  cout<<"Jepni te dhenat per studentin \n";
  cout<<"Emri : ";
  cin.getline(tekst,50);
  strcpy(pstud->emri,tekst);    // vini re operatorin ->
  cout<<"Mbiemri : ";
  cin.getline(tekst,50);
  strcpy(pstud->mbiemri,tekst);
  cout<<"Dega : ";
  cin.getline(tekst,50);
  strcpy(pstud->dega,tekst);
  cout<<"Viti : ";
  cin.getline(tekst,50);
  pstud->viti=atoi(tekst);

  cout<<"Studenti sapo u regjistrua ne program : \n";
  listoStudentat(*pstud);


  system("PAUSE");
  return 0;
}

void listoStudentat(student st){
    cout<<"Te dhenat per kete student \n";
    cout<<"Emri : "<<st.emri<<"\t Mbiemri : "<<st.mbiemri<<endl;
    cout<<"Dega : "<<st.dega<<"\t Viti : "<<st.viti<<endl;
    cout<<"-----------------------------------------\n";
}

Ne console :
Jepni te dhenat per studentin
Emri : Ermiri
Mbiemri : Beqiraj
Dega : Informatike
Viti : 2
Studenti sapo u regjistrua ne program :
Te dhenat per kete student
Emri : Ermiri    Mbiemri : Beqiraj
Dega : Informatike       Viti : 2
-----------------------------------------
Press any key to continue . . .

Ushtrim 4 : te dhenat per ditelindjen te jete ne nje structure te nderfutur

#include <stdio.h>
#include <iostream.h>
#include  <cstdlib>
#include <string.h>
using namespace std;

struct data{
     int dita;
     int muaji;
     int viti;
} ;

struct student{
    char  emri[20];
    char  mbiemri [20];
    char  dega[50];
    int vitiStud;
    data ditelindje;         //strukture brenda strukture
  } stud;


void listoStudentat(student st);

int main(int argc, char *argv[])
{
  strcpy(stud.emri,"Indrit");
  strcpy(stud.mbiemri,"Callo");
  strcpy(stud.dega,"Informatike");
  stud.vitiStud=2;
  stud.ditelindje.dita=20;
  stud.ditelindje.muaji=2;
  stud.ditelindje.viti=1992;

  listoStudentat(stud);

  system("PAUSE");
  return 0;
}

void listoStudentat(student st){
    cout<<"Te dhenat per kete student \n";
    cout<<"Emri : "<<st.emri<<"\t Mbiemri : "<<st.mbiemri<<endl;
    cout<<"Dega : "<<st.dega<<"\t Viti : "<<st.vitiStud<<endl;
    cout<<"Ditelindja : "<<st.ditelindje.dita<<"/";
    cout<<st.ditelindje.muaji<<"/"<<st.ditelindje.viti<<endl;
    cout<<"-----------------------------------------\n";
}

Ne console :
Te dhenat per kete student
Emri : Indrit    Mbiemri : Callo
Dega : Informatike       Viti : 2
Ditelindja : 20/2/1992
-----------------------------------------
Press any key to continue . . .


Ushtrime Mixx

Ushtrim 1 : Te ndertohet nje program qe permban : nje variable int n, nje pointer ne int dhe nje pointer ne pointer ne int.
Te afishohet permbajtja e variablit n ne tre menyra.
Te afishohet adresa e n-se, adresa e pointerit , si dhe permbatja e te dy pointerave.

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int n; //var int
    int *p1;// pointer ne int
    int **p2;// pointer ne pointer ne int
    // inicializime
    n=107;
    p1=&n;
    p2=&p1;
    // permbatja e n-se ne tre menyra
    cout<<"n= "<<n<<endl;
    cout<<"n ndryshe eshte edhe *p1: "<<*p1<<endl;
    cout<<"ose dhe **p2 : "<<*(*p2)<<endl;
    cout<<"----------------\n";
    //adresa e n eshte permbatja e p1
    cout<<"adresa e n-se : "<<&n<<endl;
    cout<<"permbatja e p1 : "<<p1<<endl;
    cout<<"keto vlera jane njesoj sepse p1 shenjon ne n \n";
    cout<<"---------------- \n";
    //adresa e p1 eshte permbatja e p2
    cout<<"adresa e p1 : "<<&p1<<endl;
    cout<<"permbajtja e p2 : "<<p2<<endl; 
    cout<<"keto vlera jane njesoj sepse p2 shenjon ne p1 \n";
    cout<<endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}

Ne console :
n= 107
n ndryshe eshte edhe *p1: 107
ose dhe **p2 : 107
----------------
adresa e n-se : 0x22ff74
permbatja e p1 : 0x22ff74
keto vlera jane njesoj sepse p1 shenjon ne n
----------------
adresa e p1 : 0x22ff70
permbajtja e p2 : 0x22ff70
keto vlera jane njesoj sepse p2 shenjon ne p1

Press any key to continue . . .


Ushtrimi 2  ---------------------------------------------------------------------------------------------------------------
Te shkruhet nje program qe lexon nga tastjera dy numra, n dhe k, ku n<1000 dhe k<n, i tille qe per cdo nr nga 1 ne n qe plotpjestohet me k ose mbaron me k te afishoje ne ekran fjalen bumm.
psh, per n=15 dhe k=3, afishon 
1,2,bumm, 4,5,bumm,7,8,bumm,10,11,bumm,bumm,14,bum

Zgjidhje
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int nr_max,k;
    cout<<"vendosni numrin maximal : ";
    cin>>nr_max;
    cout<<"vendosni numrin e shenderrimit me te vogel se "<<nr_max<<" : ";
    cin>>k;
    //nje cikel for per numerimin ;
    for(int i=1;i<=nr_max;i++){
        if((i%k==0)||(i%10==k))
           cout<<"bum , ";                              
        else
           cout<<i<<" , ";                       
                               }
    cin.get();
    cin.get();
    return EXIT_SUCCESS;
}
Ushtrim nr 3  ------------------------------------------------------------------------------------------------------------
Gjeni vleren e funksionit f 
f=x*x-3 per x<5
f=x+1 per 5<=x<=25
f=x*x-5*x+6 per x>25
	zgjidhje
#include <cstdlib>
#include <iostream>
   /*
  duhet te keni parasysh qe vlerat 
  e kthyera nga aplikacioni do te jene ne
  varesi te numrit te vendosur perkatasisht :
    f=x*x-3 per x<5
    f=x+1 per 5<=x<=25
    f=x*x-5*x+6 per x>25
  */  
  
using namespace std;

int main(int argc, char *argv[])
{
    int nr,f,g;
        cout<<"jepni nje vlere variablit : ";
        cin>>nr;
    if(nr<5){
        g=(nr*nr)-3;
        cout<<nr<<" * "<<nr<<" - "<<3<<" = "<<(nr*nr)-3;     
             }
    if(nr>25){
        f=(nr*nr)-(5*nr)+6;
        cout<<nr<<" * "<<nr<<" - "<<5<<" * "<<nr<<" + "<<6<<" = "<<f;      
              }         
    else{
        f=nr+1;
        cout<<f;          
        }
    cin.get();
    cin.get();
    return EXIT_SUCCESS;
}
Ushtrim nr 4  ------------------------------------------------------------------------------------------------------------
Shkruani nje program qe realizon produktin e dy numrave nepermjet mbledhjes se perseritur
Psh 3*4=3+3+3+3

	Zgjidhje
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int x,y,perfundimi=0;
    cout<<"vendosni nje numer te plote : ";
    cin>>x;
    cout<<"vendosni numrin me te cilin doni ta shumezoni : ";
    cin>>y;
    for(int i=1;i<=y;i++){
       perfundimi=perfundimi+x;
                          
                          }
    cout<<"\nrrezultati = "<<perfundimi;                      
    cin.get();
    cin.get();
    return EXIT_SUCCESS;
}

Ushtrim nr 5  ------------------------------------------------------------------------------------------------------------
Te lexohen dy numra natyrore te formuar nga kater shifra. Cili eshte numri i shifrave te perbashket i ketyre numrave.
Psh n1=1234 dhe n2=3456 eshte 2 shifra
	Zgjidhje   menyra 1 (me tabele)  :
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int nr1,n2,n1,shifra=0,perbashket=0,temp;
    cout<<"vendosni numrin e pare : ";
    cin>>nr1;
    n1=nr1;
    cout<<"vendosni numrin e dyte : ";
    cin>>n2;
    
    while(nr1>0){                                //nje cikel while per te gjetur numrin e shifrave 
         nr1=nr1/10;        
         shifra++;
                 }
    int tab[shifra];                             //deklarimi i tabeles             
    
    for(int i=0;i<shifra;i++){                   //inicializimi i tabeles me numrin e pare te ndare sipas shifrave
           tab[i]=n1%10;
           n1=n1/10;
                              }
    while(n2>0){                                //pjestojme numrin e dyte derisa te behet zero
       temp=n2%10;
       for(int j=0;j<shifra;j++){               //kontrollojme cdo shifer te tije nese eshte e barabarte me ndojnje pre j te parave  
          if(temp==tab[j])
             perbashket++;                       //nese po (++)
                                 }         
       n2=n2/10;                                 
                
                }                      
    cout<<"numra te perbashket : "<<perbashket;  //afishimi                        
    
    cin.get();
    cin.get();
    return EXIT_SUCCESS;
}

	Zgjidhje menyra 2 :

/*
jepet n1 dhe n2;
te gjendet numri i shifrave te perbashketa
*/
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int n1,n2,temp,perbashket=0,nr2;
    cout<<"vendosni numrin e pare : ";
    cin>>n1;
    cout<<"vendosni numrin e dyte : ";
    cin>>n2;
    
    nr2=n2;
    while(n1>0){
         temp=n1%10;
         while(n2>0){
              if(temp==n2%10)
                 perbashket++;       
                 n2=n2/10;    
                     }          
         n1=n1/10;           
         n2=nr2;           
                    }
    if(perbashket==0)
            cout<<"numrat qe vendoset nuk kane asnje shifer te perbashket \n";
    else     
         if(perbashket==1)
            cout<<"numrat qe vendoset kane "<<perbashket<<" sifer te perbashket \n";
         else                 
            cout<<"nurat qe vendoset kane "<<perbashket<<" shifra te perbashketa ";                
    cin.get();
    cin.get();
    return EXIT_SUCCESS;
}
Ushtrim nr 6   ------------------------------------------------------------------------------------------------------------
Te lexohen dy numra n dhe m. Te llogariten n ne fuqi m.
	Zgjidhje
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int n,m,p=1;
    cout<<"jepni numrin : ";
    cin>>n;
    cout<<"jepni exponentin : ";
    cin>>m;
    for(int i=1;i<=m;i++){
        p=p*n;                            
                         }
       cout<<p<<endl;                  
    system("PAUSE");
    return EXIT_SUCCESS;
}
Ushtrim nr 7   ------------------------------------------------------------------------------------------------------------
Lexohet nje vektor me numra natyrore. Te afishohet numri i shifrave zero qe ka prodhimi i elementeve te vektorit
	Zgjidhje
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
    int tab[3]={100,50,20};
    int prodhimi=1;
    for(int i=0;i<3;i++){
        prodhimi=prodhimi*tab[i];                 
                         }
    cout<<"prodhimi : "<<prodhimi<<endl;
    int temp,zero=0;
    while(prodhimi>0){
         temp=prodhimi%10;
         if(temp==0)
         zero++;             
         prodhimi=prodhimi/10;             
                      }                                     
    cout<<"\nprodhimi ka "<<zero<<" zero \n";
    cin.get();
    cin.get();
    return EXIT_SUCCESS;
}
Ushtrim nr 8   ------------------------------------------------------------------------------------------------------------
Jepet nje vektor i perbere me n numra natyrore si dhe nje numer tjeter M. Programi duhet te shumzoje vektorin n me M.
	Zgjidhje
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int nr_max,m;
    cout<<"vendosni numrin e elementeve te vektorit : ";
    cin>>nr_max;
    cout<<"vendosni numrin qe do te shumezoni : ";
    cin>>m;
    int vektor[nr_max];
    for(int i=0;i<nr_max;i++){
        cout<<"vendosni elementin e "<<i+1<<" te vektorit : \n";
        cin>>vektor[i];                      
        m=m*vektor[i];                      
                              
                              }
    cout<<"prodhimi i elementeve te vektorit \nme numrin qe vendoset eshte "<<m;                          
    cin.get();
    cin.get();
    return EXIT_SUCCESS;
}
Ushtrim nr 9   ---------------------------------------------------------------------------------------------------------
te ndertoni nje program qe mbledh elementet poshte diagonales kryesore
Zgjidhje	
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int tab[3][4]={{1,2,3,4},
                   {5,6,7,8},
                   {9,8,7,6}};
    int s_p=0;               
    for(int i=0;i<3;i++){
       for(int j=0;j<4;j++){
          if(i>j){
            s_p=s_p+tab[i][j];           
                 }          
                            
                            }
                         }               
    cout<<s_p<<endl;                     
    system("PAUSE");
    return EXIT_SUCCESS;
}



Ushtrim nr 10  -----------------------------------------------------------------------------------------------------------
te ndertohet nje program qe permban nje matrice katrore m, dhe merr nga perdoruesi nje numer n<m.
Programi afishon min per rreshtin n dhe max per shtyllen n.
	Zgjidhje
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[]){
    int matrica[5][5]={{1,3,2,4,6},
                       {2,9,7,8,5},
                       {5,2,3,7,4},
                       {7,3,8,6,9},
                       {8,6,9,9,7}
                                  };
    int nr;
    cout<<"vendosni nje numer me te vogel se 6 : ";
    cin>>nr;
    int min=matrica[nr][0],max=0;
    for(int i=0;i<5;i++){
        for(int j=0;j<5;j++){
            if(i==(nr-1)){
               if(min>matrica[i][j])              
                  min=matrica[i][j];           
                             }                 
            if(j==(nr-1)){
               if(max<matrica[i][j])
                  max=matrica[i][j];        
                          }                 
                             }                 
                         
                         }          
    cout<<"\nminimumi per rreshtin qe vendoset eshte "<<min<<endl;
    cout<<"\nmaximumi per shtyllen eshte "<<max<<endl;                                                     
    system("PAUSE");
    return EXIT_SUCCESS;
}
Ushtrimi nr 11   �����
Jepet nje vektor me n numra te plote:
kerkohet:
a)mesatarja aritmetike
b)prodhimi i elementeve
c)sa jane numra pozitive, sa neg dhe sa zero
d)sa jane numra cift dhe sa jane nr qe plotpjestohen me 3
Zgjidhje
#include <cstdlib>
#include <iostream>
#define n 5
using namespace std;

int main(int argc, char *argv[])
{
    int tab[n]={1,2,3,4,5};
    int shuma=0,prodhimi=1,poz=0,neg=0,zero=0,cift=0,plotp=0;
    cout<<"ne kete tabele : \n";
    for(int i=0;i<n;i++){
       shuma=shuma+tab[i];
       prodhimi=prodhimi*tab[i]; 
       if(tab[i]>0)
          poz++;
       if(tab[i]<0)
          neg++;
       if(tab[i]==0)
          zero++;  
       if(tab[i]%2==0)
          cift++;
       if(tab[i]%3==0)
          plotp++;
       cout<<tab[i]<<" ";                       
                         }                  
    cout<<endl;
    cout<<"gjenden : \n";
    cout<<poz<<" numra pozitiv \n";
    cout<<neg<<" numra negativ \n";
    cout<<zero<<" numra zero \n";
    cout<<"mesatarja aritmetike e tyre eshte "<<(shuma/n)<<endl;
    cout<<"dhe prodhimi i tyre eshte "<<prodhimi;                     
    cin.get();
    cin.get();
    return EXIT_SUCCESS;
}

Ushtrim nr 12  -----------------------------------------------------------------------------------------------------------
jepet matrica NxM
kerkohet:
a)mesatarja aritmetike per cdo rresht
b)shuma e elementeve qe gjenden nen diagonalen kryesore
c)sa here gjendet vlera x e dhene nga perdoruesi si element matrice
d)te afishoje numrat pozitive dhe numrat negative
	zgjidhje
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int tab[3][4]={{1,2,3,4},
                   {5,6,7,8},
                   {9,8,3,1}};
    int shuma=0,shn=0,x,poz,neg,el=0;
    cout<<"jepni nje vlere : ";
    cin>>x;
    cout<<endl;
    for(int i=0;i<3;i++){
       for(int j=0;j<4;j++){
           shuma=shuma+tab[i][j];
           if(j==3){    
              cout<<"mesatarja per rreshtin "<<i+1<<" eshte "<<shuma/(j+1)<<endl;                 
              shuma=0;
                    }
           if(i>j){
              shn=shn+tab[i][j];     
                   }          
           if(tab[i][j]==x)
              el++;
                            }                  
                         }      
    cout<<"\nnumri qe vendoset u gjet "<<el<<"here ne tabele ";                     
    cout<<endl<<"shuma e elementeve nen diagonalen kryesore eshte "<<shn<<endl;                                       
    
    system("PAUSE");
    return EXIT_SUCCESS;
}
Ushtrim nr 13  -----------------------------------------------------------------------------------------------------------
Ndertoni nje program qe realizon proceduren e mbledhjes se elementeve te vektoreve a dhe b.
//Ndertoni nje program qe realizon proceduren e mbledhjes se elementeve te vektoreve a dhe b.
#include <cstdlib>
#include <iostream>

using namespace std;
void mbledhje(int tab1[],int tab2[],int tab3[],int nr){
     for(int i=0;i<nr;i++)
         tab3[i]=tab1[i]+tab2[i];                  
                                                     }
void afisho(int tab1[],int tab2[],int tab3[],int nr){
     cout<<"Tabela 1 : ";
     for(int i=0;i<nr;i++)
         cout<<tab1[i]<<" ";
     cout<<endl;
     cout<<"Tabela 2 : ";                                                    
     for(int i=0;i<nr;i++)
         cout<<tab2[i]<<" ";     
     cout<<endl;
     cout<<"Tabela 3 : ";
     for(int i=0;i<nr;i++)
         cout<<tab3[i]<<" ";         
     cout<<endl;    
                                                     }                                                     
int main(int argc, char *argv[])
{
    int nr;
    cout<<"Jepni numrin max te elementeve : ";
    cin>>nr;
    int tab1[nr];
    int tab2[nr];
    int tab3[nr];
    for(int i=0;i<nr;i++){
       cout<<"Jepni elementin "<<(i+1)<<" te tab[] 1 : ";
       cin>>tab1[i];                   
                          }
    for(int i=0;i<nr;i++){
       cout<<"Jepni elementin "<<(i+1)<<" te tab[] 2 : ";
       cin>>tab2[i];                   
                          }
    mbledhje(tab1,tab2,tab3,nr);
    afisho(tab1,tab2,tab3,nr);                      
    system("PAUSE");
    return EXIT_SUCCESS;
}Console : 
Jepni numrin max te elementeve : 5
Jepni elementin 1 te tab[] 1 : 1
Jepni elementin 2 te tab[] 1 : 2
Jepni elementin 3 te tab[] 1 : 3
Jepni elementin 4 te tab[] 1 : 5
Jepni elementin 5 te tab[] 1 : 6
Jepni elementin 1 te tab[] 2 : 6
Jepni elementin 2 te tab[] 2 : 5
Jepni elementin 3 te tab[] 2 : 3
Jepni elementin 4 te tab[] 2 : 2
Jepni elementin 5 te tab[] 2 : 1
Tabela 1 : 1 2 3 5 6
Tabela 2 : 6 5 3 2 1
Tabela 3 : 7 7 6 7 7
Press any key to continue . . .
Ushtrim nr 14  -----------------------------------------------------------------------------------------------------------
Ndertoni nje procedure qe afishon elementin max te vektorit
	Zgjidhje
#include <cstdlib>
#include <iostream>
#define rresht 5

using namespace std;
void mbledhje(int tab[rresht]){
     int max=tab[0];
     for(int i=0;i<rresht;i++){
        if(max<tab[i])
          max=tab[i];   
                               }    
     cout<<"elementi maximal ne tabele eshte eshte "<<max<<endl;                                                        
                                       }
int main(int argc, char *argv[])
{
    int vektor1[rresht]={0,2,30,4,5};
    mbledhje(vektor1);
    cin.get();
    cin.get();
    return EXIT_SUCCESS;
}
ushtrim 15   --------------------------------------------------------------------------------------------------------------
Shkruani proceduren qe afishon numrat tek me te vegjel se numri i dhene nga perdoruesi
	Zgjidhje
#include <cstdlib>
#include <iostream>
#define rresht 5

using namespace std;
void tek(int a){
    for(int i=0;i<a;i++){
       if(i%2==0)
          continue;
       else
          cout<<i<<" ";                     
                         }
                
                }
int main(int argc, char *argv[])
{
    int x;
    cout<<"jepni nje numer : ";
    cin>>x;
    tek(x);
    cin.get();
    cin.get();
    return EXIT_SUCCESS;
}

Ushtrim nr 16   ----------------------------------------------------------------------------------------------------------
Shkruani proceduren qe afishon numrat natyrore qe pjestohen me tre dhe me te vegjel se vlera N;
	Zgjidhje
#include <cstdlib>
#include <iostream>
#define rresht 5

using namespace std;
void plotpjestim(int a){
    for(int i=0;i<a;i++){
       if(i%3==0)
          cout<<i<<" ";                     
                         }
                
                }
int main(int argc, char *argv[])
{
    int x;
    cout<<"jepni nje numer : ";
    cin>>x;
    plotpjestim(x);
    cin.get();
    cin.get();
    return EXIT_SUCCESS;
}