Passing an array to a function?

Hi all, new to this site. This is the assignment I need some help with:

"Write a C++ program that declares an array alpha of 50 components of type double. Call a function that will initialize the array so that the first 25 components are equal to the square of the index variable, and the last 25 components are equal to three times the index variable. Call a second function that will output the array so that 10 elements per line are printed."

This is 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
  #include <iostream>

using namespace std;

void calculations();
void output(); 

int main()
{
	const int arraysize = 50;
	double numbers[arraysize];

	return 0;
}

void calculations(double nums[], int size)
{
	for (int index = 0; index <= 25; index++)
		int square = nums[index] * nums[index];
	
	for (nums[index] = 25; index > 25; index++)
		int cube = (nums[index] * 3);

}

void output()
{

}


Thank you in advance.
To be more specific, the first instance of the word "index" in the second for loop says it's undefined and I'm not sure how to fix it. Also, as for printing out 10 numbers per line, does that have anything to do with %? I'm not asking for the answer or anything, just some pointers.
Hi, there are some mistakes in your code.
First of all the second "index" is undefined because you declare the first "index" inside the for-loop. This means that outside of the loop the variable does not exist and you have to re-declare it. Then you should read more carefully the text of the exercise: it says that the values shoud be the square of the index variable, which means the variable you called "index" and you use to traverse the array.

This is a possible solution:

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
#include <iostream>

using namespace std;

void calculations(double[], int);
void output();

int main()
{
	const int arraysize = 50;
	double numbers[arraysize];
	calculations(numbers, arraysize);

	return 0;
}

void calculations(double nums[], int size)
{
	for (int index = 0; index < 25; index++)
		nums[index] = index*index;

	for (int index = 25; index < size; index++)
		nums[index] = 3*index;

}

void output()
{

}


Now try to complete the output function and let us know if you need help.
Last edited on
Thanks for the help, I really appreciate it! As for the output, I came up with this:

1
2
3
4
5
6
7
8
9
10
11
void output()
{
	int index;
	calculations(index);
	cout << index << "    ";
	while ((index % 10) = 0)
	{
		cout << endl;
	}
	
}


But now it has a problem with "index" in the calculations function call, plus one of the parentheses in the while loop.
Last edited on
No wait a minute...
This is a function that should only print something to the standard output. You should not call calculations from here... In your code you never print the values you put in the array!

The structure of your code should be as follows:

1
2
3
4
5
6
7
8
int main() {
    ...
    calculations (...);   // fill the array
    output (...);          // print the array
    ...
}

// here the code of your two functions 
Wait, I'm confused. If I'm not supposed to call calculations in output, then how will it know what to output? And I added the two function calls to main:
1
2
3
4
5
6
7
8
9
int main()
{
	const int arraysize = 50;
	double numbers[arraysize];
	calculations(numbers, arraysize);
	output();
	
	return 0;
}
Last edited on
You should call calculations and let it fill the array. Then you should call output, passing the array and the size as parameters, and let it print the content. Basically you are "delegating" the two operations to the two functions: in the main should just declare the variables and make the appropriate calls.
All right, This is one way I was able to figured it out Here it is. If you need me to explain, feel free to ask.

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
#include <iostream>
#include <cmath>

using namespace std;

double calculations(double [], const int);
void output(double[], const int); 

int main()
{
	const int arraySize = 50;
	double numbers[arraySize];
	
	calculations(numbers, arraySize);
	output(numbers, arraySize);

	return 0;
}

double calculations(double nums[], const int size)
{
    int index;
    int index2;
    int num[] = {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};
    
	for (index = 0; index < size/2; index++)
		  nums[index] = pow(num[index], 2);
	
	for (index2 = index; index2 < size; index2++)
		  nums[index2] = pow(num[index2], 3);

    return *nums;
}

void output(double nums[], const int size)
{
   for(int i=0; i<size; i++)
   {
       cout << i+1 << ":" << nums[i] << endl;
   }
}
Last edited on
I don't think this is correct.

1. The values should be the square of the index variables (or three times the index variable) and you have not done so
2. You are not printing the values in lines formed by 10 elements

This is how I would do it:

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
#include <iostream>

using namespace std;

void calculations(double[], int);
void output(double[], int);

int main() {
	// declaration of variables
	const int arraysize = 50;
	double numbers[arraysize];

	// function calls
	calculations(numbers, arraysize);
	output(numbers, arraysize);

	return 0;
}

void calculations(double nums[], int size) {
	for (int index = 0; index < 25; index++)
		nums[index] = index * index;

	for (int index = 25; index < size; index++)
		nums[index] = 3 * index;
}

void output(double nums[], int size) {
	int count = 0;

	for (int i = 0; i < size; i++) {
		cout << nums[i] << "   ";
		++count;
		if (count % 10 == 0)
			cout << endl;
	}
}
Last edited on
Not really, this is my result.

1:1
2:4
3:9
4:16
5:25
6:36
7:49
8:64
9:81
10:100
11:121
12:144
13:169
14:196
15:225
16:256
17:289
18:324
19:361
20:400
21:441
22:484
23:529
24:576
25:625
26:17576
27:19683
28:21952
29:24389
30:27000
31:29791
32:32768
33:35937
34:39304
35:42875
36:46656
37:50653
38:54872
39:59319
40:64000
41:68921
42:74088
43:79507
44:85184
45:91125
46:97336
47:103823
48:110592
49:117649
50:125000

As you can see, from 1 to 25, the numbers are squared, and from 26 to 50 the numbers are cubed. The way that you did it and the way that I did it both works. What I saying is, in programming, there are multiple ways to achieve the same thing.

edit: Ok yes, I forgot to print out the values of value by 10 lines but that should be easy to modify.

edit2: All right I see what you mean now, I just have to modify it a little bit. I'll be back.
Last edited on
Yes you're right about this point. I miscalculated the index.
But I don't see why you filled the array manually before calling the functions...

P.S. The exercise said three times, not cube. Anyway this is not important
Yes I read the assignment wrong I'm sorry. I assigned them manually because I was getting garbage value for some reason. Any way, if you ever need to do what I did, now you know lol. :)
Last edited on
So I was right in that % had something to do with the output. Again, thank you guys so much!
Yes, you had garbage values because if you declare an array[50] then you don't know which values are stored in the array. Anyway, since the assignment said to use the index variable, this is not important, becuase you immediately overwrite these garbage values.
The way you did, you were reading the values (garbage) to calculate the square, and this was causing problems.
> So I was right in that % had something to do with the output. Again, thank you guys so much!

Yes, nice call! ;)
Topic archived. No new replies allowed.