using functions

My code shouldn't have anything other than function calls in the main, I'm having trouble on how I can do this?

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
  #include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>

using namespace std;
struct Point
{
    double x=1;
    double y=1;
    
};
void print (double a, double b);
double random(unsigned long int & seed);
double small_value(double smallx,  double smally);
double smallest_value( double smallx,  double smally);

int main()
{
    const int SIZE = 1000;
    unsigned long int seed;
    srand(time(NULL));
    seed = rand();
    
    Point array[SIZE];
    double smallx= array[0].x;
    double smally= array[0].y;
    for(int i=0; i<SIZE; i++)
    {
        array[i].x = random(seed);
        array[i].y = random(seed);
        if(array[i].x < smallx && array[i].y < smally)
        {
            smallx= array[i].x;
            smally= array[i].y;
        }
    }

    print(smallx,smally);
    return 0;
}

double random(unsigned long int & seed)
{
    const int MODULUS = 15749;
    const int MULTIPLIER = 69069;
    const int INCREMENT = 1;
    seed = ((MULTIPLIER*seed)+INCREMENT)%MODULUS;
    return double (seed)/MODULUS;
    
}

void print (double a, double b)
{
    
    cout  << "Smallest x: " <<  a<< endl;
    cout  <<"Smallest y: " << b << endl;
    
}
To give you an idea (one out of numerous ways to do it). I commented out for illustration. However, I did not verify the actual calculations.

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
#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>

using namespace std;
struct Point
{
	double x = 1;
	double y = 1;

};
void print(double a, double b);
double random(unsigned long int & seed);
void small_value(Point [], const int, unsigned long int); // ADDED...
double small_value(double smallx, double smally);
double smallest_value(double smallx, double smally);

int main()
{
	const int SIZE = 1000;
	unsigned long int seed;
	srand(time(NULL));
	seed = rand();
	Point array[SIZE];
	small_value(array, SIZE, seed);
	//double smallx = array[0].x;
	//double smally = array[0].y;
	//for (int i = 0; i < SIZE; i++)
	//{
	//	array[i].x = random(seed);
	//	array[i].y = random(seed);
	//	if (array[i].x < smallx && array[i].y < smally)
	//	{
	//		smallx = array[i].x;
	//		smally = array[i].y;
	//	}
	//}

	//print(smallx, smally);
	return 0;
}

double random(unsigned long int & seed)
{
	const int MODULUS = 15749;
	const int MULTIPLIER = 69069;
	const int INCREMENT = 1;
	seed = ((MULTIPLIER*seed) + INCREMENT) % MODULUS;
	return double(seed) / MODULUS;

}

void small_value(Point array[], const int SIZE, unsigned long int seed)
{
	double smallx = array[0].x;
	double smally = array[0].y;
	for (int i = 0; i < SIZE; i++)
	{
		array[i].x = random(seed);
		array[i].y = random(seed);
		if (array[i].x < smallx && array[i].y < smally)
		{
			smallx = array[i].x;
			smally = array[i].y;
		}
	}

	print(smallx, smally);

}

void print(double a, double b)
{

	cout << "Smallest x: " << a << endl;
	cout << "Smallest y: " << b << endl;

}
Surely this code has appeared before?

@cash, your determination of smallx and smally in your lines 32 to 36 really isn't going to work. Update them separately.
Last edited on
Topic archived. No new replies allowed.