Jump to content
  • Advertisement
Sign in to follow this  
shadowfire36

pointers and array ..differences ?

This topic is 3409 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i was given an assignment by my prof. , but he stated that using pointer notation . the object t=of the program was to write a C++ function ReverseArray using "pointer notation" that will write out the elements of an array of int in reverse order. his comments were This is array notation: cout << "The int values you entered are: " << endl; for (int i = 0; i < arraySize; i++) cout << intList << endl; This is pointer notation cout <<*intList< i have no clue of what he means so im a bit lost , but he returned my program(F) to fix and trying to get someone to explain what i did wrong this is my code
#include <iostream>

using namespace std;


int *numList; // array of POINTERS to numbers
int arraySize = 0 ; // input area for longest possible number.

int main()
{
void ReverseArray(int* &intArray, int size);





cout << "Enter the size of the new int array: ";
cin >> arraySize;

cout << "\n"; //newline

//(array var) = new (type)[(size taken from user}]

numList = new int[arraySize];

for(int i = 0; i < arraySize; i++)
{
cout << "\nEnter the next number in the array: ";
cin >> numList;


}

ReverseArray(numList, arraySize);


return 0;
}//end main

void ReverseArray(int* &intArray, int size)
{
//for each element up to "size"
for (int i=size-1; i>=0; i--)
/*for(int i = 0; i < size; i++)*/
{
cout << "Element " << i << " equals: " << intArray << "." << endl;
cout << numList << endl; // print the numbers
}

system("PAUSE");
}

Share this post


Link to post
Share on other sites
Advertisement
I think he meant this kind of iteration;

for(int* p = intArray ; p != (intArray + size) ; ++p)
{
cout << *p << endl;
}


Now you figure out how to do that in reverse order.

Share this post


Link to post
Share on other sites
actually i wanted to know with in my code . what did i do different to get an "f" ?

the instructor stated i didnt you sue the pointer notation but i thought i did .. so what did i do do , declare a variable correctly , etc ... my program does exactly what was requested, but he stated that i did not use the proper notation , and that what im trying to figure out what was he talking about

Share this post


Link to post
Share on other sites
So basically the elements of an array can be accessed either using pointer notation or "array notation"


int poop[5]; //a 5 element long array of int

//access the 3rd element in the array using array notation
int thirdElement = poop[2];

//access the 3rd element in the array using pointer notation
int thirdElement = *(poop + 2);


//iterate the array using array notation (this is what you did)
for ( int i = 0; i < 5; ++i )
{
int anElement = poop;
}

//iterate the array using pointer notation (this is what you were supposed to do, though you are supposed to iterate backwards this example is forwards)
int i = 0;
for ( int *p = poop; i < 5; ++i, ++p )
{
int anElement = *p;
}



An array (and the accompanying notation) is just a built in way of dealing with memory. An array is just a contiguous block of allocated memory. The array notation [x] is just a way of saying "pointer to head of array plus x". When you increment a pointer you are just moving the pointer along the array the appropriate distance to acess the xth element.

-me

Share this post


Link to post
Share on other sites
So to answer your question, you used array notation, not pointer notation. i.e. you didn't do the assignment correctly at all.

-me

Share this post


Link to post
Share on other sites
Quote:
Original post by Palidine
So to answer your question, you used array notation, not pointer notation. i.e. you didn't do the assignment correctly at all.

-me




omg thank you !!!!!!!!!!!!!!!!!!!!!!!!!!!

that made so much more sense

Share this post


Link to post
Share on other sites
I doubt you were supposed to simply output the array in reverse order inside the ReverseArray function.
A programmer reading through your code should be able to guess what a function does, and most of the time get it right. Functions should thus be named according to what they do.
A function called ReverseArray should actually be copying items around in such a way as to place the items in the reverse order to what they were originally. It should not simply output the array backwards. The array should not actually be output from this function at all, but should instead be output from the main function afterwards.

It is often important in programming, to separate functions that manipulate data from functions that output the data too. Otherwise you wouldn't be able to reverse an array twice and then print it out once (back in the original order). Okay, bad example

You also shouldn't have the intArray parameter as a reference. You can remove the '&' and it will still work. Having it as a reference allows you to modify what numList points to, which is not what you want to do. Given that you've made it possible to do that, many programmers reading the code would assume that this is because it does do that. So again that fails the "guess what it does" test.

This is all assuming that your teacher isn't simply teaching you badly, of course.

Share this post


Link to post
Share on other sites
Quote:
Original post by iMalc
I doubt you were supposed to simply output the array in reverse order inside the ReverseArray function.
A programmer reading through your code should be able to guess what a function does, and most of the time get it right. Functions should thus be named according to what they do.
A function called ReverseArray should actually be copying items around in such a way as to place the items in the reverse order to what they were originally. It should not simply output the array backwards. The array should not actually be output from this function at all, but should instead be output from the main function afterwards.

It is often important in programming, to separate functions that manipulate data from functions that output the data too. Otherwise you wouldn't be able to reverse an array twice and then print it out once (back in the original order). Okay, bad example

You also shouldn't have the intArray parameter as a reference. You can remove the '&' and it will still work. Having it as a reference allows you to modify what numList points to, which is not what you want to do. Given that you've made it possible to do that, many programmers reading the code would assume that this is because it does do that. So again that fails the "guess what it does" test.

This is all assuming that your teacher isn't simply teaching you badly, of course.


...

Quote:
Original post by shadowfire36
...the object t=of the program was to write a C++ function ReverseArray using "pointer notation" that will write out the elements of an array of int in reverse order...


Sounds like his prof is just teaching him basic c++ concepts. You're missing the point of this thread. Correct function naming is a completely unrelated module/problem.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!