Homework check

#include <stdio.h>
#include <stdlib.h>
#define N 8
int main()
{

int pole[N];
int q,i;

do {

printf("Zadajte max %d prvkov pola\n", N);
for (i=0; i<N; i++) {
scanf("%d", &pole[i]);
}
printf("Zadane prvky pola: \n");
for (i=0; i<N; i++) {
printf("%d\n", pole[i]);
}
printf("Zadane prvky opacne:\n");
for (i=N-1; i>=0; i--) {
printf("%d\n", pole[i]);
}

int max=pole[0];
for (i=0; i<N; i++) {
if (pole[i]>max) {
max=pole[i];
}
}
printf("\nMaximum v poli je %d\n", max);

int min=pole[0];
for (i=0; i<N; i++) {
if (pole[i]<min) {
min=pole[i];
}
}
printf("\nMinimum v poli je %d\n", min);

float priem;
int suc=0;

for (i=0; i<N; i++) {
suc+=pole[i];
}
priem=(float)suc/N;
printf("Priemer prvkov pola je %f\n", priem);

printf("Zotriedene pole:\n");

int pom,j;
for (j=0; j<N; j++) {
for (i=0; i<N-1; i++) {
if (pole[i]>pole[i+1]) {
pom=pole[i];
pole[i]=pole[i+1];
pole[i+1]=pom;
}
}
}
for (j=0; j<N; j++) {
printf("%d\n", pole[j]);
}

printf("POKRACOVAT? \n 1=ANO \n 0=NIE \n");
scanf("%d", &q);
} while (q);

return 0;
}
wow, um... I think this is supposed to let the user enter 8 ints, find the min and max, and print them out sorted. Which is what it does. Hard to say though since I don't speak the language. (is that Russian?)

If that's what you were trying to do, you succeeded. What is your question?

also suc+=pole[i] made me giggle. I'm such a child. :)

You should use code tags when posting code: http://www.cplusplus.com/forum/articles/42672/


you could find the min and max in one pass through the array:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
		int max=pole[0];
		int min=pole[0];
		int suc=0;
		for( i=0; i<N; i++ ) {
			if( pole[i]>max ) {
				max=pole[i];
			}
			if( pole[i]<min ) {
				min=pole[i];
			}
			suc+=pole[i]; // obligatory juvenile sniggering
		}
		printf( "\nMaximum v poli je %d\n", max );
		printf( "\nMinimum v poli je %d\n", min );
Last edited on
Felt the need to do it. partially just cause i wanted to look at the code for myself.

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
#include <stdio.h>
#include <stdlib.h>
#define N 8
int main()
{

int pole[N];
int q,i;

do {

printf("Zadajte max %d prvkov pola\n", N);
for (i=0; i<N; i++) {
scanf("%d", &pole[i]);
}
printf("Zadane prvky pola: \n");
for (i=0; i<N; i++) {
printf("%d\n", pole[i]);
}
printf("Zadane prvky opacne:\n");
for (i=N-1; i>=0; i--) {
printf("%d\n", pole[i]);
}

int max=pole[0];
for (i=0; i<N; i++) {
if (pole[i]>max) {
max=pole[i];
}
}
printf("\nMaximum v poli je %d\n", max);

int min=pole[0];
for (i=0; i<N; i++) {
if (pole[i]<min) {
min=pole[i];
}
}
printf("\nMinimum v poli je %d\n", min);

float priem;
int suc=0;

for (i=0; i<N; i++) {
suc+=pole[i];
}
priem=(float)suc/N;
printf("Priemer prvkov pola je %f\n", priem);

printf("Zotriedene pole:\n");

int pom,j;
for (j=0; j<N; j++) {
for (i=0; i<N-1; i++) {
if (pole[i]>pole[i+1]) {
pom=pole[i];
pole[i]=pole[i+1];
pole[i+1]=pom;
}
}
}
for (j=0; j<N; j++) {
printf("%d\n", pole[j]);
}

printf("POKRACOVAT? \n 1=ANO \n 0=NIE \n");
scanf("%d", &q);
} while (q);

return 0;
}
Last edited on
Topic archived. No new replies allowed.