Please HELP!

Hello!
Can you help me please? I have a problem with my program, worked for school. I have to do a program that display negative and positive numbers from a file and show they in two lines. And at the end the program must add the number of positive and the number of negative numbers. All of this must be done with functions. But I don't know how to do this...

Please help!
Last edited on
Let me just put my mind-reader suit on and I'll get back to you >.>

http://www.cplusplus.com/forum/beginner/1/
http://www.cplusplus.com/articles/jEywvCM9/
Last edited on
Which aspect of the task, specifically, are you having trouble with?
With display negative and positive numbers... I don't know how to read functions for this.
For now, I wrote this:

#include <iostream>
#include <fstream> /* za branje iz datoteke */
#include <conio.h> /* for getch(), non-portable */
#include <iomanip> // za std::setprecision()
using namespace std;

int nalozi_datoteko_v_polje_double(double polje[], const char* ime_datoteke) {
fstream file(ime_datoteke);
int st_prebranih_stevil = 0;

while (!file.eof()) {
file >> polje[st_prebranih_stevil];
st_prebranih_stevil++;
}

return st_prebranih_stevil;
}

void izpis_polja_double(double polje[], int dolzina_polja) {
cout << fixed << setprecision(2); // želimo 2 decimalni mesti
for (int i = 0; i < dolzina_polja; i++) {
cout << polje[i] << endl;
}
}

// a (10t) - poišci in izpiši število negativnih in pozitivnih števil
// b (10t) - najprej izpiši vsa pozitivna števila, nato še vsa negativna števila

int main(void) {
const int VELIKOST_POLJA = 100; // nastavimo na veliko vrednost, da imamo dovolj prostora (v tem primeru je rezerva za 100 - 20 = 80 števil)
double polje_realnih[VELIKOST_POLJA];

int st_stevil = nalozi_datoteko_v_polje_double(polje_realnih, "datoteka_z_realnimi_stevili.txt");

cout << "Stevilo stevil: " << st_stevil;

// Print the array.
cout << endl << "Polje:" << endl;
izpis_polja_double(polje_realnih, st_stevil);

getch(); // cakamo na poljuben znak, nato koncamo program

return 0;
}

All you have to do is just go through the array and print out each element, if its negative it will print out as a negative number, idk what the problem is
I know what I have to do but I don't know how to write this in function... If someone write it will be very helpful.
Our teacher in middle school is... well "a little on another world"...
Can you show me the input (file) and expected output?
My file look like this:

Numbers:

1 -2.1 3.33 4 5 6 7.77 8 -9.99 0.17
5.1 -2.2 3.3 -4.4 5.55 -6.66 7.77 8.20 91 -10.75

Output in program:

Positive numbers: 1 3.33 4 5 6 7.77 8 0.17 5.1 3.3 5.55 7.77 8.20 91
Negative Numbers: -2.1 -9.99 -2.2 -4.4 -6.66 -10.75
Number of positive numbers: 14
Number of negative numbers: 6
I don't know how many functions you need to write so I've created separate functions for each task. You can combine them if you want. Next time pls use code tags and indent code properly...

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
82
83
84
#include <iostream>
#include <fstream> /* za branje iz datoteke */
#include <conio.h> /* for getch(), non-portable */
#include <iomanip> // za std::setprecision()
using namespace std;

int nalozi_datoteko_v_polje_double(double polje[], const char* ime_datoteke) {
	fstream file(ime_datoteke);
	int st_prebranih_stevil = 0;

	while (!file.eof()) {
		file >> polje[st_prebranih_stevil];
		st_prebranih_stevil++;
	}

	return st_prebranih_stevil;
}

int countPositive(double polje[], int dolzina_polja) {
	int positive = 0;
	for (int i = 0; i < dolzina_polja; ++i)
		if (polje[i] > 0)
			++positive;
	return positive;
}

int countNegative(double polje[], int dolzina_polja) {
	int negative = 0;
	for (int i = 0; i < dolzina_polja; ++i)
		if (polje[i] < 0)
			++negative;
	return negative;
}

void izpis_pozit(double polje[], int dolzina_polja) {
	cout << "Positive numbers: ";
	for (int i = 0; i < dolzina_polja; ++i)
		if (polje[i] > 0)
			cout << polje[i] << " ";
	cout << "\n";
}

void izpis_negat(double polje[], int dolzina_polja) {
	cout << "Negative numbers: ";
	for (int i = 0; i < dolzina_polja; ++i)
		if (polje[i] < 0)
			cout << polje[i] << " ";
	cout << "\n";
}

void izpis_polja_double(double polje[], int dolzina_polja) {
	cout << fixed << setprecision(2); // želimo 2 decimalni mesti
	for (int i = 0; i < dolzina_polja; i++) {
		cout << polje[i] << endl;
	}
}

// a (10t) - poišci in izpiši število negativnih in pozitivnih števil
// b (10t) - najprej izpiši vsa pozitivna števila, nato še vsa negativna števila

int main(void) {
	const int VELIKOST_POLJA = 100; // nastavimo na veliko vrednost, da imamo dovolj 
//prostora (v tem primeru je rezerva za 100 - 20 = 80 števil)
	double polje_realnih[VELIKOST_POLJA];

	int st_stevil = nalozi_datoteko_v_polje_double(polje_realnih,
"datoteka_z_realnimi_stevili.txt");

	cout << "Stevilo stevil: " << st_stevil;
	/*
	// Print the array.
	cout << endl << "Polje:" << endl;
	izpis_polja_double(polje_realnih, st_stevil);
	*/
	cout << endl;
	izpis_pozit(polje_realnih, st_stevil);
	izpis_negat(polje_realnih, st_stevil);
	cout << "Number of positive numbers: " << countPositive(polje_realnih, st_stevil) << "\n";
	cout << "Number of negative numbers: " << countNegative(polje_realnih, st_stevil) << "\n";

	getch(); // cakamo na poljuben znak, nato koncamo program

	return 0;
}
Thank you so much :D
Topic archived. No new replies allowed.