Polecenie
Wylosować elementy do tablicy prostokątnej X na Y. Obliczyć sumę wszystkich elementów dodatnich, ujemnych i zliczyć ile ich jest. Zliczyć ile jest elementów równych zero. Na koniec zapytać czy powtórzyć całość, dopuszczalne znaki to T/t/n/N. Program zabezpieczyć hasłem.

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>
#include <stdlib .h>
#include <stdio .h>
#include <time .h> //windows #include <conio>
#define X 5
#define Y 2
using namespace std;
 
 
int gotoxy(int x, int y) {
	char essq[100]; 
	char xstr[100]; 
	char ystr[100]; 
	sprintf(xstr, "%d", x);
	sprintf(ystr, "%d", y);
	essq[0] = '\0';
	strcat(essq, "\033[");
	strcat(essq, ystr);
	strcat(essq, "d");
	strcat(essq, "\033[");
	strcat(essq, xstr);
	strcat(essq, "G");
	printf("%s", essq);
	return 0;
} //tylko dla linuksa!
 
 
 
main () {
	system("clear"); //system("cls");
	int ix,iy,sd=0,sn=0,iled=0,ilen=0,ilez=0;
	char znak;
	int tab[X][Y];
	srand(time(NULL));
	string haslo="tajne";
	string wprowadzone;
	do {
	  cout < < "Podaj haslo: ";
	  cin >> wprowadzone;
	  if ( wprowadzone != haslo ) 
		  cout < < "Wprowadzone haslo jest bledne\n";
	} while ( wprowadzone != haslo );
 
      do {
	      system("clear"); //windows system("cls");
	sd=0;sn=0;iled=0;ilen=0;ilez=0;
	cout << "\t\t\tTablica" ;  
       for (iy=1; iy<=Y; iy++)
        for (ix=1; ix<=X; ix++) 
	{
		tab[ix][iy]=rand()%10-rand()%5;
		gotoxy(ix*5+15,iy+2);
		cout << tab[ix][iy];
	}
 
	 for (iy=1; iy<=Y; iy++)
          for (ix=1; ix<=X; ix++) 
	    if ( tab[ix][iy] > 0 ) 
	    {
               sd=sd+tab[ix][iy];
	       iled=iled+1;
	    }
	    else if ( tab[ix][iy] < 0 ) 
	    {
		sn=sn+tab[ix][iy];
		ilen=ilen+1;
	    }
	    else
		ilez=ilez+1;
 
cout<<endl<<"Liczb dodatnich jest: "<<iled<<" i ich suma wynosi: "<<sd<<endl;
cout<<"Liczb ujemnych jest: "<<ilen<<" i ich suma wynosi: "<<sn<<endl;
cout<<"Zer natomiast jest: "<<ilez<<endl;
 
      cout << "Czy chcesz kontynuowac? [T/N] "; 
      	do {
        	gotoxy(32,Y+6); 
		znak=toupper(getchar());
      	}while (znak != 'T' && znak != 'N');
      } while (znak != 'N');
}

Pozostaw odpowiedź

(c) 2007 by Michał Terbert