Przykład tablicy jednowymiarowej oraz losowania do niej elementów z małą statystyką. Kod działa na MacOS X, Linux na Windows także powinien.
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
| #include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <stdlib.h>
#define X 10
using namespace std;
int main(){
srand(time(NULL));
system("clear");
int i,T[X],elr=0,elw=0,elm=0;
float srednia, suma=0;
cout<<"Wprowadz elementy tablicy: "<<endl;
for(i=0; i<X; i++)
{
cout<<"Wprowadz element nr "<<i<<" : ";
//cin>>T[i];
T[i]=(rand()% 7501 - 2500);
cout<<T[i]<<endl;
suma=suma+T[i];
}
cout<<"Licze srednia..."<<endl;
srednia=suma/X;
cout<<srednia;
for(i=0; i<X; i++)
{
if(T[i]==srednia)
elr++;
else if(T[i]>srednia)
elw++;
else
elm++;
}
cout<<endl<<"Statystyka"<<endl;
cout<<"Elementy mniejsze od sredniej: "<<elm<<endl;
cout<<"Elementy wieksze od sredniej: "<<elw<<endl;
cout<<"Elementy rowne sredniej: "<<elr<<endl;
} |
Brak komentarzy »
Napisał Michał w kategorii Cpp, Studia, tags: Cpp, programowanie
Polecenie:
Narysuj ramkę znając x1,x2,y1,y2.
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
| #pragma hdrstop
#pragma argsused
#include <iostream .h>
#include <stdlib>
#include <stdio>
#include <time>
#include <conio>
void rysuj(x1,y1,x2,y2)
{
int i,j;
for(i=0,j=0; i< =x2-x1;j++,i++)
//for(i=0;i<=x2-x1;i++)
{
gotoxy(x1+i,y1);
cout<<"*";
gotoxy(x1+i,y2);
cout<<"*";
if(j>y1)
continue;
gotoxy(x1,y1+j);
cout< <"*";
gotoxy(x2,y2-j);
cout<<"*";
}
}
main () {
char button;
int x1,y1,x2,y2;
system("cls");
cout<<"Rysowanie: "<<endl;
x1=4;
x2=20;
y1=5;
y2=10;
rysuj(x1,y1,x2,y2);
getchar();
} |
Brak komentarzy »
Napisał Michał w kategorii Cpp, Studia, tags: Cpp, programowanie
Polecenie:
Policz silnię z n oraz wartość bezwzględna liczby.
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
| #include <iostream>
#include <stdlib .h>
#include <stdio .h>
using namespace std;
float bez(float c)
{
if (c<0)
c=-c;
return c;
}
int silnia(unsigned n)
{
long int i;
int s=1;
for (i=1;i< =n;i++)
s=s*i;
return s;
}
main () {
int a;
printf("Policzymy Silnie! \nPodaj liczbe: ");
cin>>a;
cout< <endl<<"Silnia z "<<a<<" wynosi: "<<silnia(a)<<endl;
printf("Policzymy wartosc bezwzgledna! \nPodaj liczbe: ");
cin>>a;
cout< <endl<<"Wartosc z "<<a<<" wynosi: "<<bez(a)<<endl;
system("PAUSE");
} |
Brak komentarzy »
Polecenie:
Testujemy dwie pętle while oraz for:
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
| #pragma hdrstop
#pragma argsused
#include <iostream .h>
#include <stdio .h>
main()
{
int i,min,max;
cout< <"Sprawdzamy Min i Max"<<endl;
cout<<endl<<endl;
cout<<"Testujemy dwie petle while oraz for: "<<endl;
do
{
cout<<"Podaj min: ";
cin>>min;
cout< <"Podaj max: ";
cin>>max;
if(min>max)
cout< <"\n\tBlad! Min musi byc < od Max!\n"<<endl;
}while(min>max)
cout< <"Podales min: "<<min<<", max: "<<max<<endl;
cout<<"Wyswietl co 2 od min do max:"<<endl<<endl;
cout<<"Wyswietl za pomoca petli for:"<<endl;
for(i=min;i<=max;i=i+2)
cout<<i<<endl;
cout<<endl<<"Wyswietl za pomoca petli while:"<<endl;
while(min<=max)
{
cout<<min<<endl;
min=min+2;
}
system("PAUSE");
} |
Brak komentarzy »