help with project

everything works properly except the final for loop which outputs the information stored in copyarray after you have moved the information from array to copyarray.


#include <iostream>
using namespace std;

// new function or CopyArray function, made for part 9 being used to copy the current array to a new one to be shown at the end of the program.
int CopyArray (int Array [30]; int Copyarray [30])
{
for (int i =0; i <30 ; i++)
{
Array [i] = Copyarray [i]; // this is the statement copying the information from array to copyarray
}
return 0;
}

int main (void)
{
int Array [30]; // the array being used by the primary portion of the program
int num =0; // number a variable being used to count and input the data into the array
for (int i =0; i < 30; i++) // counter for the numbers being generated by the user.
{
cout << "Enter a number ." << endl;
cin >> num;
if (num >0 && num < 100) // if the number is over 0 and under 100 it is valid
{
cout << "The number you entered is valid." << endl;
num = Array [i]; // adding the current number into the array to avoid placing the same number twice.
}
else
{
cout << "Please enter a valid number between 1-100" << endl; // asking the user for a secondary input that is valid to continue counting
cin >> num; // the new valid number being inputted by the user in place of the original non-valid number.
}
}
int CopyArray (int Array [30]);
cout << "Here are the values you have input into the program." << endl;
for (int i =0; i<30; i++)
{
cout << Copyarray [i] << endl;
}
}
system ("PAUSE");
If you can edit your post, highlight your code, then click on the <> format button on the right side, it'll format your code to make it easier for others to read. :)

1
2
// new function or CopyArray function, made for part 9 being used to copy the current array to a new one to be shown at the end of the program.
int CopyArray (int Array [30]; int Copyarray [30])


You have a semicolon between parameters here instead of a comma. Also - you define the function to take two arrays as parameters, but then call it with just one parameter, a single array element at an index that doesn't exist - 30. Also - you don't need the return type (int) when you call the function.
int CopyArray (int Array [30]);

1
2
3
4
5
if (num >0 && num < 100) // if the number is over 0 and under 100 it is valid
{
cout << "The number you entered is valid." << endl;
num = Array [i]; // adding the current number into the array to avoid placing the same number twice.
}


Do you mean for this to be reversed? You want to assign the num entered to the array? Array[i] = num;

1
2
3
4
5
else
{
cout << "Please enter a valid number between 1-100" << endl; // asking the user for a secondary input that is valid to continue counting
cin >> num; // the new valid number being inputted by the user in place of the original non-valid number.
}


If they enter an invalid number, you prompt them to re-enter, but I don't see that you add the re-entered number into the array?


1
2
3
4
for (int i =0; i<30; i++)
{
cout << Copyarray [i] << endl;
}


The Copyarray Array is defined in the separate function but not in main.
Last edited on
thank you very much for your help so far, I got stuck because visual studios was no longer reading any errors. I appreciate the help.
1
2
3
4
5
6
7
8
9
10
		}
	}
	CopyArray (Array [30], Copyarray [30]);
	cout << "Here are the values you have input into the program." << endl;
	for (int i =0; i < 30; i++)
	{
		cout << Copyarray [i] << endl;
	}
}
system ("PAUSE");


still having some trouble on this part, it says its wrong whether the arrays are referenced as integers or not
If you're trying to send the whole array to a function, just include the name of the array. The array name serves as a pointer to the first element of the array. Putting a number in brackets when sending an array to a function sends just that individual element.

callArrayFunction (wholeArrayName, singleArrayElement[5])

It looks like you're trying to send element 30 to the function. Since the array has 30 elements, the element index numbers run from 0 to 29, so element 30 doesn't exist.

Have you now defined the Copyarray array in main?

Edit: edited to fix wholeArray call
Last edited on
this is what I have now, thank you very much for your help so far, your tips have been very useful indeed.

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

// new function or CopyArray function, made for part 9 being used to copy the current array to a new one to be shown at the end of the program.
int CopyArray (int Array [], int Copyarray [])
{
	for (int i =0; i <30 ; i++)
	{
		Copyarray [i] = Array [i]; // this is the statement copying the information from array to copyarray
	}
	return 0;
}

int main (void)
{
	int Array [30]; // the array being used by the primary portion of the program
	int Copyarray [30]; // the array being used to copy the data input by the user.
	int num =0; // number a variable being used to count and input the data into the array
	for (int i =0; i < 30; i++) // counter for the numbers being generated by the user.
	{
		cout << "Enter a number ." << endl;
		cin >> num; 
		if (num >0 && num < 100) // if the number is over 0 and under 100 it is valid
		{
			cout << "The number you entered is valid." << endl;
			Array [i] = num; // adding the current number into the array to avoid placing the same number twice.
		}
		else
		{
			cout << "Please enter a valid number between 1-100" << endl; // asking the user for a secondary input that is valid to continue counting
			cin >> num; // the new valid number being inputted by the user in place of the original non-valid number.
			Array [i] = num;
		}
	}
	CopyArray (Array [], Copyarray []);
	cout << "Here are the values you have input into the program." << endl;
	for (int i =0; i < 30; i++)
	{
		cout << Copyarray [i] << endl;
	}
}
system ("PAUSE");
Last edited on
so I tweaked it up a little fixed a few errors, but its outputting at the end a -max integer #
line 9 - if you're copying from array to copyarray, you need to reverse the order there. The item on the left hand side of the assignment statement is assigned the value of the item on the right hand side.

line 16 - you don't need a function prototype since you have the definition of the function before main. A function prototype would go before the main function if you were defining the actual function later (after main). The function prototype needs the type on any parameters. (e.g. int CopyArray (int [], int[]) - that tells the compiler the function returns an int and takes two arrays of type int.)

line 36 - you don't need the :
so I edited the code above to include what you told me, but now I am receiving a error on line 35 saying that the arrays as parameters are incompatible with type int
Line 35: Remove the []s. You only need to pass the name of the array.

 
  CopyArray (Array, Copyarray);




Thank you very much for all of your help once again, it compiles and does exactly what its supposed to. Much appreciated.
Topic archived. No new replies allowed.