How to use arrays from user input in seperate functions


This is really hard to describe but for computer science we have to create an array that asks for ten numbers, which I got working great, then it has to print out the order in reverse if the user is asked. After that it has to print out the even order of the array and then the odd. I have all this working perfectly fine in int main() but then I read on in the assignment and I have to put every section the teacher is asking for in a separate function. I don't really get functions well enough yet and can't tell how to pass one array that I have in its own function that also gets all the user input into several other functions.
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
100
101
102
103
104
105
#include<iostream>
#include<string>


using namespace std;

float fill_numbers()
{
	float number[10];
	for(int count = 1; count <= 10; ++count)
	{
		cout << "Enter the " << count << " number: ";
		cin >> number[count];
	}

	//return number[10];
}

float print_reverse(float number[10])
{
	
			for(int count = 10-1; count >= 0; --count)
			{
				cout << "The reverse order of what you entered is: " << number[count] << endl;
			}
}

float print_odd_indices()
{
	for (int count1 = 1; count1 <= 10; count1 = count1+2)
	{
		cout << "Odd Indice " << count1 << ": " << number[count1] << endl;
	}

}

float print_even_indices()
{
		for (int count2 = 1; count2 <= 10; ++count2)
		{
			if (count2 % 2 == 0)
			{
				cout << "Even Indice " << count2 << ": " << number[count2] << endl;
			}
			
		}	
}

float print_average()
{
		float sum;
		float average;

			for (int count3 = 1; count3 <= 10; ++count3)
		{
			sum = sum + number[count3];

			average = sum / 10;
		}

		cout << "The average is: " << average << endl;
}

int main()
{
	string input;



	
	fill_numbers();
	

	cout << "Do you want to print the numbers in reverse order, odd indices, even indices, average of the ten numbers, or exit this program?: ";
	cin.ignore();
	getline (cin, input);
	
	if (input == "Reverse Order")
	{
		print_reverse(fill_numbers);	
	}

	if (input == "Odd Indices")
	{
		print_odd_indices();
	}

	if (input == "Even Indices")
	{
		print_even_indices();
	}

	if (input == "Average")
	{
		print_average();
	}

	if (input == "Exit")
	{
		return 0;
	}



}
Last edited on
I know my code is a mess at the moment but it's because originally none of these functions existed I had everything in int main() working perfectly fine; but once I read my CSCI teacher wanted us to put them all in their own functions I tried to do it in the last hour or so and can't figure out what to do now. I can't figure out how to carry over my number[] array.
First of all, you can pass an array to a function, like so:

1
2
3
4
5
6
7
8
9
10
11
12
void function(int array[], const unsigned short size) {
	/**/
}

int main() {
	const unsigned short size = 10;
	int array[size];

	function(array, size);

	return 0;
}


The way you have it, your fill_numbers() function creates a temporary array of ten integers. The array ceases to exist after the function is done, so anything you've done to the array was pointless, and you're unable to use the same array elsewhere in your code.

It would make more sense if your fill_numbers() function took an array parameter, as well as a size parameter (number of elements).

Also, keep in mind that array indecies begin at zero, so that makes some of your for loops dangerous, since they're accessing memory that's not part of the array.

To iterate through an array of ten elements, something like this would be safe:

1
2
3
for(int i=0; i<9; ++i) {
	/**/
}


Finally, accepting input where uppercase and non-alphabetic characters are important is scary. "Odd Indecies" is not the same as "Odd indecies", which is also not the same as "odd Indecies" or "odd indecies".
Thank you so much! Helped a lot!
Topic archived. No new replies allowed.