Calling Functions Issue

I am not sure what to put in the (double, int) part of my functions, because (*newArray, SIZE) doesn't work... Anyone know how to fix this?

Here is my code:

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
85
86
87
88
89
90
91
92
93
#include "stdafx.h"
#include <iostream>
//others
using namespace std;
double *Array = nullptr;

// Declaration of functions
void sort(double, int);
void getAverage(double, int);
void getMedain(double, int);
int main() 
{

	int size = 0;
	while (size < 1) {
		cout << "Enter size of array: ";
		cin >> size;
	}
	double* array = new double[size];

	for (int i = 0; i < size; i++) {
		cout << "Enter number " << i + 1 << ": ";
		cin >> array[i];
	}
	// calling of functions

	// sort(*newArray, SIZE)
	// getAverage(*newArray, num)
	// getMedain(*newArray, num)

	system("pause");

	return 0;
}

void sort(double *newArray, int SIZE)
{
	int beginScan, minIndex;
	double minVal;

	for (beginScan = 0; beginScan < (SIZE - 1); beginScan++)
	{
		minIndex = beginScan;
		minVal = newArray[beginScan];

		for (int index = beginScan + 1; index < SIZE; index++)
		{
			if (newArray[index] < minVal)
			{
				minVal = newArray[index];
				minIndex = index;
			}
		}

		newArray[minIndex] = newArray[beginScan];
		newArray[beginScan] = minVal;
	}
}

void getAverage(double *newArray, int num)
{
	int i;
	double total = 0, avg = 0;
	for (i = 0; i < num; i++)
	{
		total = total + newArray[i];
	}
	avg = total / num;

	cout << "Average/Mean: " << avg << endl;


}

void getMedain(double *newArray, int num)
{
	int middle = 0;
double median = 0;
middle = num / 2;
if (middle % 2 == 0)
{
	median = (newArray[middle - 1] + newArray[middle]) / 2;
}
else
{
	median = newArray[middle];
}

cout << "The Median is: " << median << endl;

}

// Defining functions 
Last edited on
Please use code tags when posting code. See http://www.cplusplus.com/articles/jEywvCM9/

I copied these from your code:
1
2
void sort(double, int); // declaration
void sort(double *newArray, int SIZE) // definition 

Are they identical? If not, how do they differ?
(Hint in bold.)
I am calling the function correctly though, right? Do I need to redefine the variables above, or should I use different variables altogether?
Which function?

If it was not clear, you do show two different functions that both have name 'sort'.
They differ by the type of arguments that they require:
1
2
void sort(double, int);
void sort(double*, int);


Your main can see only the first version. You can write in main() only what matches that.
What does a function that has name "sort" and takes exactly one double and one int do?

The linker will give an error, because the main() calls a 'sort' that has no implementation.

If the line 8 is supposed to be (declaration for) the same function that we see on lines 36-58, then line 8 must match exactly:
void sort(double*, int);
This function takes pointer to double(s) and an integer.


Function call on line 27 has two names: newArray and SIZE.
Where are these variables defined?
I don't see them anywhere on lines 1-26.


Line 14 defines int size and line 19 double *array. These existing objects have values that seem logical to be used somewhere, are they not?
I've been working on it some more, and fixing some of the errors of which you made me aware. Now, at least it runs, but I have a problem with the bolded part. The result I get for Average/Mean is "-nan(ind)", and the error message is "Exception thrown: read access violation.
**newArray** was 0x111010A."


This is my new code:

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
85
86
87
88
89
90
91
92
93
94
95
96
#include "stdafx.h"
#include <iostream>
//others
using namespace std;
double *Array = nullptr;




// Declaration of functions
void sort(double*, int);
void getAverage(double*, int);
void getMedain(double*, int);
int main() 
{

	int size = 0;
	while (size < 1) {
		cout << "Enter size of array: ";
		cin >> size;
	}
	double* array = new double[size];

	for (int i = 0; i < size; i++) {
		cout << "Enter number " << i + 1 << ": ";
		cin >> array;
	}
	// calling of functions
	double *newArray=0;
	int SIZE=0, num=0;
	sort(newArray, SIZE);
	getAverage(newArray, num);
	getMedain(newArray, num);
	system("pause");

	return 0;
}

void sort(double *newArray, int SIZE)
{
	int beginScan, minIndex;
	double minVal;

	for (beginScan = 0; beginScan < (SIZE - 1); beginScan++)
	{
		minIndex = beginScan;
		minVal = newArray[beginScan];

		for (int index = beginScan + 1; index < SIZE; index++)
		{
			if (newArray[index] < minVal)
			{
				minVal = newArray[index];
				minIndex = index;
			}
		}

		newArray[minIndex] = newArray[beginScan];
		newArray[beginScan] = minVal;
	}
}

void getAverage(double *newArray, int num)
{
	int i;
	double total = 0, avg = 0;
	for (i = 0; i < num; i++)
	{
		total = total + newArray[i];
	}
	avg = total / num;

	cout << "Average/Mean: " << avg << endl;


}

void getMedain(double *newArray, int num)
{
	int middle = 0;
double median = 0;
middle = num / 2;
[u][i]if (middle % 2 == 0)
{
	median = (newArray[middle - 1] + newArray[middle]) / 2;
}
[/u]else
{
	median = newArray[middle];
}

cout << "The Median is: " << median << endl;

}

// Defining functions 
Last edited on
@Jakjam,
I suggest that you go through your code, line-by-line, from the top and ask yourself what you are doing on that line ("rubber-duck debugging").

From the top, and purely by inspection.

- Line 5: What is the purpose of
double *Array = nullptr;
This variable isn't used - remove it.

- Lines 29 and 30
1
2
double *newArray=0;
	int SIZE=0, num=0;

You already have data in array "array" (poor name) - use it. newArray is NOT NEEDED in main() - remove it from there.
You already have a variable size (note the lower case) - this should be the size indicator for all the following function calls in lines 31, 32, 33 - you DON'T NEED a new variable SIZE (the upper-case version: note that size and SIZE are completely different variables) NOR a separate variable num - remove them (from main()). Then rewrite your function calls in terms of "array" and "size".

What you call the array variable and size variable in the function definitions themselves is completely independent. Names chosen there are irrelevant (but should be consistent within a function) - however, I can think of more meaningful names than newArray. The fact that you have called the size variable SIZE in one routine and num in the other two suggests that you may have been cutting and pasting code from elsewhere, and is also a recipe for confusion.

- Deal with one function at a time - not all three.
Last edited on
Now, at least it runs, but I have a problem with the bolded part.

No, you don't. Your problem are the lines 29 and 30, as lastchance pointed out.

Your effective function calls are:
1
2
3
sort( nullptr, 0 );
getAverage( nullptr, 0 );
getMedain( nullptr, 0 );


In your getMedain() you calculate
middle = num / 2;
You did call the function with a 0 and thus num==0. We can already compute the middle:
middle = 0 / 2;
middle = 0;

0 % 2 == 0, and therefore median is calculated:
median = ( newArray[-1] + newArray[0] ) / 2;
Your function call did give newArray (in getMedain) the value of nullptr.
The newArray does not point to element of any array. The newArray is invalid pointer.

Dereferencing invalid pointer is undefined behaviour.

Consider yourself lucky that the program merely crashes. It could silently slaughter all the baby seals of the world.


If you would call the function with a valid pointer, then the newArray would (hopefully) point to the first element of an array, but index -1 is still out of range. An error.



[EDIT]
Another look at the names in the call:
You and compiler see:
1
2
3
4
5
6
void sort( double*, int );

int main() 
{
  // code
  sort( ??? ); // What to write here? 

The only thing that we do (need to) know about the sort() is that it needs two values: a pointer to double(s) and an integer. There is nothing about names. Only the types.

You have an another function call in your program:
system( "pause" );
How come you can call that function with a literal value rather than with a variable that is named identically to the name of the argument that the implementation of function system() does use?
Last edited on
I am working on a group project, and I am supposed to put two parts together... The first part I received was good. The second one I received, the one I am working on currently, gave me an absurd amount of errors. So yes, I have no idea what I am doing, because the code I received made no sense to me.

This was the original code:

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
double *Array = nullptr;

newArray = new double[num]


//sort(double *newArray, int SIZE)
{

int beginScan, minIndex;
double minVal;

for (beginScan = 0; beginScan < (SIZE - 1); beginScan++)
{
minIndex = beginScan;
minVal = newArray[beginScan];

for (int index = beginScan + 1; index < SIZE; index++)
{
if (newArray[index] < minVal)
{
minVal = newArray[index];
minIndex = index;
}
}

newArray[minIndex] = newArray[beginScan];
newArray[beginScan] = minVal;
}
}

//getAverage(double *newArray, int num)
int i;
double total = 0, avg = 0;
for (i = 0; i < num; i++)
{
total = total + array[i];
}
avg = total / num;

cout << "Average/Mean: " << avg << endl;


//getMedain(double *newArray, int num)
int middle = 0;
double median = 0;
middle = num / 2;
if (numViewers % 2 == 0)
{
median = (array[middle - 1] + array[middle]) / 2;
}
else
{
median = array[middle];
}

cout << "The Median is: " << median << endl;


I have made more corrections, but I think I need to completely rewrite the getMedain() function...

Any idea where to start?
but I think I need to completely rewrite the getMedain() function...

Any idea where to start?



Going back to the code that I was commenting on, the first thing you need to get right is the function calls from int main() - don't touch the functions just yet. Because you introduced a completely unnecessary new pointer *newArray in main and additional zero length parameters SIZE and num you are getting the effective call with nullptr that @Keskiverto has explained to you.

Note that I'm referring to newArray in main() - the dummy argument that is used in getMedain() was, as far I could see without testing it, OK. Then you need to check that the arrays getting into each function are what you think they are - just print them out within those functions.

I suggest:
(1) In int main() only, remove unnecessary variables and check that you are passing the array and size to all three functions, not newArray or SIZE or num.

(2) Then, for each function in turn, write some lines within that function to print out the array it has been passed, just to make sure that this is the array that you think it is. Note that the names of variables in these functions need have nothing to do with the name of variables in main() and they can probably be left as they are.

(3) Check each function in turn (maybe commenting out calls to the rest).

Try not to post "back" versions of the code, or all these explanations are going to get confused. Also, please look at your post after it has been submitted and, if necessary, edit it to remove any formatting typos.
Last edited on
Finished my project... I feel very dumb, and made two very simple mistakes that messed everything up. Thank you for all your help!
For any future reference, I will post my final code:

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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include "stdafx.h"
#include <iostream>
//others
using namespace std;

// Declaration of functions
void sort(double*, int);
void getAverage(double*, int);
void getMedain(double*, int);

int size = 0;

int main() 
{

	int size = 0;
	while (size < 1)
	{
		cout << "Enter size of array: ";
		cin >> size;
	}
	double* array = new double[size];

	for (int i = 0; i < size; i++) {
		cout << "Enter number " << i + 1 << ": ";
		cin >> array[i];
	}
	// calling of functions
	sort(array, size);
	getAverage(array, size);
	getMedain(array, size);
	system("pause");

	return 0;
}

void sort(double *array, int size)
{
	while (size < 1)
	{
		cout << "Enter size of array: ";
		cin >> size;
	}
	int beginScan, minIndex;
	double minVal;

	for (beginScan = 0; beginScan < (size - 1); beginScan++)
	{
		minIndex = beginScan;
		minVal = array[beginScan];

		for (int index = beginScan + 1; index < size; index++)
		{
			if (array[index] < minVal)
			{
				minVal = array[index];
				minIndex = index;
			}
		}

		array[minIndex] = array[beginScan];
		array[beginScan] = minVal;
	}
}

void getAverage(double *array, int size)
{
	int i;
	double total = 0, avg = 0;
	for (i = 0; i < size; i++)
	{
		total = total + array[i];
	}
	avg = total / size;

	cout << "Average/Mean: " << avg << endl;


}

void getMedain(double *array, int size)
{
	int middle = 0;
double median = 0;
middle = size / 2;
if (size % 2 == 0)
{
	median = (array[middle - 1] + array[middle]) / 2;
}
else
{
	median = array[middle];
}

cout << "The Median is: " << median << endl;

}

// Defining functions 
Good.

One question though: If you do allocate memory dynamically, who will deallocate it?
Registered users can post here. Sign in or register to post.