Arrays and Functions

Hi,

Im having trouble with my C++ assignment and was hoping for some help. I need to create multiple functions and arrays. The first function needs to take in 15 numbers from the user and store it into an array and then spit it back out in order. The next function has to take in four parameters to insert a new number into that array at any indexed part of the array. Then another one to get ride of a number in the array.

I guess what Im having trouble with is creating the functions because Im completely lost. Heres what I have so far.

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
 #include <iostream>
using namespace std;

int main()
{
	const int SIZE = 15;
	int i;
	int sum = 0;
	int size[SIZE];
	char choice;
	char F = 0;
	char Q = 0;
	char P = 0;
	int Insert;

	cout << "       ** Given Features **" << endl;
	cout << "P      Print the array contents" << endl;
	cout << "F      Fill the array (user entry)" << endl;
	cout << " " << endl;
	cout << "       ** Function Tets **" << endl;
	cout << "I      Insert" << endl;
	cout << "D      Delete" << endl;
	cout << "R      Reverse" << endl;
	cout << "H      HowMany" << endl;
	cout << "S      SumOdds" << endl;
	cout << " " << endl;
	cout << "M      Print this Menu" << endl;
	cout << "Q      Quit this Program" << endl;

	cout << "Enter your menu selection: " << endl;
	
		
	cin >> choice;
		
		cout << "Please enter " << SIZE << " integers to load into the array" << endl;

			for (i = 0; i < SIZE; i++)
				cin >> size[i];
		
	cout << "Enter your menu selection: " << endl;
	cin >> choice;
	
		cout << "The array: ";
		for (i = 0; i < SIZE; i++)
			cout << size[i] << ' ';
	
	cout << "Enter your menu selection: " << endl;
	cin >> choice;
	
	Insert(list, 16, -100, 3)
		
		
		
		return 0;
}
The next function has to take in four parameters to insert a new number into that array at any indexed part of the array.

If you have an array of size 15 then that's the maximum size of elements it can hold (0-14) - arrays cant be increased in size during runtime unless they are dynamically allocated.

Have you learned pointers yet?



EDIT: I dont suppose this is your assignment? http://www.cplusplus.com/forum/beginner/137058/#msg728259

Looks the same thing :)
Last edited on
So you're problem is that you don't know how to put this into multiple functions? If so, then there's an easy solution to this.

Your first step would be deciding how you want to split the various segments of code into functions. I would recommend making a function for your initial cout statements first(if you prefer to keep them in main, go for it. I would recommend this because if you need to display the statements again for any reason, you can just call the function. Then, I would recommend a function for the last of the initial cout statements that prompts the user to decide on a menu selection and the cin statement following. Afterwards, you can either create a series of if statements, or a switch statement to check for the value of choice. The result will call the another function that I would recommend creating that consists of your for loops. Before I go any further, let me show you how to make another function, since you'll need to know that.

1
2
3
4
5
6
7
8
9
10
11
void functionA(); /*this is a forward declaration, also known as a prototype.
It is required to write another function below main. Alternatively, you can just
write your function above main, but it's easier to read the first way I mentioned. */

int main(){
funtionA(); /*this is how you call a function. It basically just runs the code within
the function */
return 0;}

void functionA(){
cout<<"This is a sentence printed from another function."<<endl;}


Now, for some of the functions I would recommend making, you would need to pass in parameters to use them outside of main, assuming you made the variables needed in main. The syntax for that is as follows:

1
2
3
4
5
6
7
8
9
10
11
void functionA(int a); /*technically, this can have any name for the
declaration and function parameter. I wouldn't recommend doing so
because it can be confusing */

int main(){
int a = 1;
functionA(a);
return 0;}

void functionA(int a){
cout<<"The number that was passed is "<<a<<endl;}


If you didn't understand something about this, or need me to elaborate, please let me know and I will do so.
Last edited on
Topic archived. No new replies allowed.