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;
 
 
}
Pozostaw odpowiedź

(c) 2007 by Michał Terbert