Function Program

Hello All,

I'm writing a program that, by using functions, begins by initializing an array (Array1) of 20 values with all zeroes (using the program InitializeArray). Next it initializes another array (Array2) of 20 values starting with 0 in ascending order (a[0]=0, a[1]=1,..etc)(using the function InitializeArrayAsc). The program then copies Array2 into Array1 (using the function "Copy") and prints Array1 with it's new values (using the function "Print")

I'm having some trouble with my output.

My program should be outputting one array with 20 values of 0, next another array with 20 values in ascending order beginning at 0 (0-19), and lastly a re-print of the first array with the values from the second.

Here is my code 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <iomanip>

using namespace std;

const int MAX = 20;

float InitializeArray(int array1[MAX], int);
float InitializeArrayAsc(int array2[MAX], int);

void Print(int elements[MAX], int extent);

void Copy(int source[MAX], int target[MAX], int, int);

int main()
{
  int array1[MAX];
  int array2[MAX];

  InitializeArray(array1, MAX);
  InitializeArrayAsc(array2, MAX);

  cout << "Array 1:\n";
  Print(array1, MAX);

  cout << "Array 2:\n";
  Print(array2, MAX);

  cout << "\nCopying Array 2 into Array 1...\n";
  Copy(array2, array1, 0, MAX);

  cout << "\nArray 1:\n";
  Print(array1, MAX);

  return 0;
}

float InitializeArray(int array1[MAX], int)
{
  float array;
  return array;
}

float InitializeArrayAsc(int array2[MAX], int)
{
  float arrayasc;
  return arrayasc;
}

void Copy(int source[MAX], int target[MAX], int, int)
{
}

void Print(int elements[MAX], int extent)
{
  const int PER_LINE = 10;
  int i;

  for(i = 0; i < extent; i++)
  {
    if (i % PER_LINE == 0 && i != 0)
    {
      cout << endl;
    }
    cout << setw(8) << left << elements[i];
  }

  cout << endl;

  return;
}


My output is of the right format, but is not displaying the correct values of any of my arrays. Either way, here it is.

1
2
3
4
5
6
7
8
9
10
11
12
Array 1:
-1073744064127     4198198 0       -146289587249      0       0       -154551596849
4196419 0       4198096 0       4198033 0       0       0       0       0
Array 2:
4197622 0       -154435211149      5247912 0       -1073744112127     5247912 0
4197619 0       -1073743952127     65535   1       -1073744096127     4197665 0

Copying Array 2 into Array 1...

Array 1:
-1073744064127     4198198 0       -146289587249      0       0       -154551596849
4196419 0       4198096 0       4198033 0       0       0       0       0


Any help at all? It would be much appreciated.

Thanks
Last edited on
That code shouldn't compile.

I'll go over initialisation. You have code:
1
2
3
4
5
int main()
{
  int array1[MAX];

  InitializeArray(array1, MAX);

Function InitializeArray must set each element of the array passed in to zero. So InitializeArray needs a loop to access each element, and it doesn't need to return a value, so it returns void.
1
2
3
4
5
void InitializeArray(int array1[MAX], int)
{
    for (int = 0; i < MAX; ++i)
        array1[i] = 0;
}


You don't need InitializeArrayAsc. You can use InitializeArray to initialise both array1 and array2 as:
1
2
3
4
5
6
7
int main()
{
  int array1[MAX];
  int array2[MAX];

  InitializeArray(array1, MAX);
  InitializeArray(array2, MAX);
Okay, thank you that fixed my issue with the first array, but I am still having a few more issues.

Here is my new output
1
2
3
4
5
6
7
8
9
10
11
12
Array 1:
0       0       0       0       0       0       0       0       0       0
0       0       0       0       0       0       0       0       0       0
Array 2:
4197656 0       -154435211149      5247944 0       -1073744112127     5247944 0 
4197653 0       -1073743952127     65535   1       -1073744096127     4197699 0 

Copying Array 2 into Array 1...

Array 1:
0       0       0       0       0       0       0       0       0       0
0       0       0       0       0       0       0       0       0       0


I guess now what I'm asking is what exactly is causing my program to send these wild numbers to the output. Also, how is it that now I seem to be copying array1 into array2? The last output is all zeroes.

Here's my program again after changes.
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
#include <iostream>
#include <iomanip>

using namespace std;

const int MAX = 20;

void InitializeArray(int array1[MAX], int);
float InitializeArrayAsc(int array2[MAX], int);

void Print(int elements[MAX], int extent);

void Copy(int source[MAX], int target[MAX], int, int);

int main()
{
  int array1[MAX];
  int array2[MAX];

  InitializeArray(array1, MAX);
  InitializeArrayAsc(array2, MAX);

  cout << "Array 1:\n";
  Print(array1, MAX);

  cout << "Array 2:\n";
  Print(array2, MAX);

  cout << "\nCopying Array 2 into Array 1...\n";
  Copy(array2, array1, 0, MAX);

  cout << "\nArray 1:\n";
  Print(array1, MAX);

  return 0;
}

void InitializeArray(int array1[MAX], int)
{
  for (int i=0; i < MAX; i++)
  {
    array1[i] = 0;
  }
}

float InitializeArrayAsc(int array2[MAX], int)
{
  float arrayasc;
  return arrayasc;
}

void Copy(int source[MAX], int target[MAX], int, int)
{
}

void Print(int elements[MAX], int extent)
{
  const int PER_LINE = 10;
  int i;

  for(i = 0; i < extent; i++)
  {
    if (i % PER_LINE == 0 && i != 0)
    {
      cout << endl;
    }
    cout << setw(8) << left << elements[i];
  }

  cout << endl;

  return;
}
change the InitializeArrayAsc to

1
2
3
4
5
6
7
void InitializeArrayAsc(int array2[MAX], int)
{
	for (int i=0; i < MAX; i++)
	{
		array2[i] = i;
	}
}
Topic archived. No new replies allowed.