Posts Tagged “Cpp”

Przykład tablicy wielowymiarowej. Losowanie wartości lub wprowadzanie z klawiatury. Użycie gotoxy (w Windows można funkcję wyrzuć, znajduje sie w nagłówku conio.h).

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
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define X 8
#define Y 8
#define MAX 120
using namespace std;
 
int gotoxy(int x, int y)
{
	char essq[MAX]={0}; 
	sprintf(essq, "\033[%d;%df", y,x);
	printf("%s", essq);
	return 0;
}
 
int main(){
srand(time(NULL));
system("clear");
 
	int i,j,T[X][Y],elr=0,elw=0,elm=0;
	float srednia, suma=0;
	cout<<"Wprowadz elementy tablicy: "<<endl; 
	for(j=0;j<Y;j++)
		for(i=0;i<X; i++)
	{
//		cin>>T[i][j];
		gotoxy((i+1)*7+2,j*2+2);
		T[i][j]=(rand()% 7501-2500);
		cout<<T[i][j];
		suma=suma+T[i][j];
	}
cout<<endl<<endl<<"Licze srednia..."<<endl;
srednia=suma/(X*Y);
cout<<srednia;
	for(i=0; i<X; i++)
		for(j=0;j<Y;j++)
		{
			if(T[i][j]==srednia)
				elr++;
			else if(T[i][j]>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;
 
 
}

Comments Brak komentarzy »

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;	
}

Comments Brak komentarzy »

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();
}

Comments Brak komentarzy »

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&lt;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");
}

Comments Brak komentarzy »

(c) 2007 by Michał Terbert