Program: losujący tablicę o wymiarach 5×5, a następnie zerującym ją.
Użytkownik wybiera przedział z którego losowane są wartości, tzn. minimum i maksimum.
Kompilator: Dev C++
Kod programu:
//Losowanie tablicy z przedziału oraz zerowanie - funkcje, wskaźniki
#include <cstdlib>
#include <iostream>
#include <time.h>
using namespace std;
void los(int &a, int &b, int *tb, int &s, int &z);
void wypisz(int *tb, int &s, int &z);
void zeruj(int *tb, int &s, int &z);
void los(int &a, int &b, int *tb, int &s);
void wypisz(int *tb, int &s);
void zeruj(int *tb, int &s);
int main(int argc, char *argv[])
{
srand(time(NULL));
int a,b;
cout<<"Podaj przedzial, w ktorym chcesz wylosowac liczby dla twojej tablicy <a, b>: \n";
cin>>a>>b;
int z=5,s=5;
if (z!=0)
{
int Tab[5][5];
int *tb;
tb=&Tab[0][0];
los(a,b,tb,s,z);
wypisz(tb,s,z);
zeruj(tb,s,z);
cout<<"\nWyzerowane\n\n";
wypisz(tb,s,z);
}
else
{
int Tab[5];
int *tb;
tb=&Tab[0];
los(a,b,tb,s);
wypisz(tb,s);
zeruj(tb,s);
cout<<"\nWyzerowane\n\n";
wypisz(tb,s);
};
cout<<"\n";
system("PAUSE");
return EXIT_SUCCESS;
}
void los(int &a, int &b, int *tb, int &s, int &z)
{
int licz=0;
for(int k=0;k<z;k++)
{
for(int i=0;i<s;i++)
{
*(tb+licz)=rand()%(b-a+1)+a;
licz++;
};
};
};
void los(int &a, int &b, int *tb, int &s)
{
for(int i=0;i<s;i++)
*(tb+i)=rand()%(b-a+1)+a;
};
void wypisz(int *tb, int &s, int &z)
{
int licz=0;
for(int k=0;k<z;k++)
{
for(int i=0;i<s;i++)
{
cout<<*(tb+licz)<<"\t";
licz++;
};
cout<<"\n";
};
};
void wypisz(int *tb, int &s)
{
for(int i=0;i<s;i++)
cout<<*(tb+i)<<"\t";
};
void zeruj(int *tb, int &s, int &z)
{
int licz=0;
for(int k=0;k<z;k++)
{
for(int i=0;i<s;i++)
{
*(tb+licz)=0;
licz++;
};
};
};
void zeruj(int *tb, int &s)
{
for(int i=0; i<s; i++) *(tb+i)=0;
};