jschmuff
asked on
Need help with pointer output using array and pointer notation
Ok this is a continuation question.. I am using pointer notation with the output being in a function and the input being in the main body. Problem is it compiles works except the output is not the numbers reversed it is a weird output.
Here is my code:
#include <iostream>
using namespace std;
void arrayRev();
const int SIZE = 5;
int main()
{
int numbers[SIZE];
int* p_numbers;
int counter;
p_numbers = numbers;
cout << "Enter five integers: ";
for (counter = 0; counter < SIZE; counter++)
{
cin >> numbers[counter];
cout << "Enter five integers: ";
}
cout << endl;
arrayRev();
return 0;
}
void arrayRev()
{
int numbers[SIZE];
int* p_numbers;
int counter;
p_numbers = numbers;
cout << "The numbers in reverse are: ";
for (counter = 4; counter >= 0; counter--)
cout << *(p_numbers+counter) << " ";
cout << endl;
}
Here is my code:
#include <iostream>
using namespace std;
void arrayRev();
const int SIZE = 5;
int main()
{
int numbers[SIZE];
int* p_numbers;
int counter;
p_numbers = numbers;
cout << "Enter five integers: ";
for (counter = 0; counter < SIZE; counter++)
{
cin >> numbers[counter];
cout << "Enter five integers: ";
}
cout << endl;
arrayRev();
return 0;
}
void arrayRev()
{
int numbers[SIZE];
int* p_numbers;
int counter;
p_numbers = numbers;
cout << "The numbers in reverse are: ";
for (counter = 4; counter >= 0; counter--)
cout << *(p_numbers+counter) << " ";
cout << endl;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Do you understand why? This is important to know for future code writing.
Indeed you are not passing the list to the output function, so basically you are printing garbage, it should be something like:
int main()
{
int numbers[SIZE];
int* p_numbers;
int counter;
p_numbers = numbers;
cout << "Enter five integers: ";
for (counter = 0; counter < SIZE; counter++)
{
cin >> numbers[counter];
cout << "Enter five integers: ";
}
cout << endl;
arrayRev(numbers);
return 0;
}
void arrayRev(int *p_numbers)
{
int counter;
cout << "The numbers in reverse are: ";
for (counter = 4; counter >= 0; counter--)
cout << *(p_numbers+counter) << " ";
cout << endl;
}
int main()
{
int numbers[SIZE];
int* p_numbers;
int counter;
p_numbers = numbers;
cout << "Enter five integers: ";
for (counter = 0; counter < SIZE; counter++)
{
cin >> numbers[counter];
cout << "Enter five integers: ";
}
cout << endl;
arrayRev(numbers);
return 0;
}
void arrayRev(int *p_numbers)
{
int counter;
cout << "The numbers in reverse are: ";
for (counter = 4; counter >= 0; counter--)
cout << *(p_numbers+counter) << " ";
cout << endl;
}
ASKER
That version of the code to try produced an error:
.\main.cpp(26) : error C2660: 'arrayRev' : function does not take 1 arguments
.\main.cpp(26) : error C2660: 'arrayRev' : function does not take 1 arguments
ASKER
ok so I declared int numbers[SIZE]; //before main function
removed it from both arrayRev function and the main.
seems to work properly now. Thanks for the help and I do understand I knew once you said to declare it globally I was beginning to see the real problem with the code. Now that I got that working just curious would there be a way to turn this into a class dont worry this is my assignment I just wanna know if it would be possible just in case your not sure of my homework this is my assignment:
Write a C++ function ReverseArray using "pointer notation" that will write out the elements of an array of int in reverse order.
removed it from both arrayRev function and the main.
seems to work properly now. Thanks for the help and I do understand I knew once you said to declare it globally I was beginning to see the real problem with the code. Now that I got that working just curious would there be a way to turn this into a class dont worry this is my assignment I just wanna know if it would be possible just in case your not sure of my homework this is my assignment:
Write a C++ function ReverseArray using "pointer notation" that will write out the elements of an array of int in reverse order.
yeah, you need to change the function prototype too, to match the real function:
#include <iostream>
using namespace std;
void arrayRev(int *); // <------------this must change too
#include <iostream>
using namespace std;
void arrayRev(int *); // <------------this must change too
if you declare the array as global, then you don't need to pass as an argument, but this version is more reusable.
about the class, you can make a "StringReverser" class and put the function just made as a method.
about the class, you can make a "StringReverser" class and put the function just made as a method.
>> ok so I declared int numbers[SIZE]; //before main function
There's no need for that. Passing it as a parameter is a lot better (see also your other thread where this has also been suggested a few times).
If you have trouble with function parameters, then I suggest to check this tutorial :
http://www.cplusplus.com/doc/tutorial/functions.html
There's no need for that. Passing it as a parameter is a lot better (see also your other thread where this has also been suggested a few times).
If you have trouble with function parameters, then I suggest to check this tutorial :
http://www.cplusplus.com/doc/tutorial/functions.html