Help with C++ Program. I need to convert arrays to pointer notation. I cant figure out how. Help please?
-
include <iostream> #include <string> using namespace std; //Function prototypes void InputArray(string names[],int size); void selectionSort(string [],int); void showArray(string [], int); //Main function int main() { const int SIZE=20; //Initializing array with given data string names[SIZE]; int n; cout<<"Enter number of names:"; cin>>n; InputArray(names, n); //Display the values cout<<"The unsorted values are\n"; showArray(names,n); //Sort the array selectionSort(names,n); //Display the values again cout<<"The sorted values are\n"; showArray(names,n); system("pause"); return 0; }//End of Main void selectionSort(string names[],int size) { int startScan,minIndex; string minValue; for(startScan=0;startScan<(size-1);sta… { minIndex=startScan; minValue=names[startScan]; for(int index=startScan+1;index<size;index++) { /*if current indexed value less than minValue*/ if(names[index].compare(minValue)<0) { minValue=names[index]; minIndex=index; } }//End of inner-loop names[minIndex]=names[startScan]; names[startScan]=minValue; }//End of main loop }//End of function /*************************************… * showArray * * This function displays the content of the array. * **************************************… void showArray(string names[],int size) { //Displaying data for(int count=0;count<size;count++) cout<<names[count]<<endl; }//End of function void InputArray(string names[],int size) { //Displaying data for(int count=0;count<size;count++) cin>>names[count]; }//End of function
-
Answer:
This really isn't your fault, it's more YA's but the layout of your post is terrible, I'm not going to read it. Here is pointers in a nutshell. When you have an array, whether it be an int, char, double, or whatever: int static_array[10] ; It is just a block of memory. Really, it is a pointer that has been statically allocated on the stack. You can manipulate this array just as you would a pointer and you can manipulate a pointer just as you could this statically allocated array. For example, the following line of code is the exact same damn thing, except it is allocated on the heap. int * dynamic_array ; dynamic_array = (int *) malloc(sizeof(int) * 10) ; In both examples where we did static and dynamic allocation we can use it the same way. Examine the following code: int i ; for (i = 0 ; i < 10; i++ ) { static_array[i] = i ; dynamic_array[i] = i ; } for (i = 0 ; i < 10; i++ ) { printf("Static = %i Dynamic = %I\n", static_array[i], dynamic_array[i]) ; } What do you believe the output will be? To answer that question it will be the same thing. Let us say that we just created the dynamic array and then did the following: int * dyn_ptr ; dyn_ptr = dynamic_array; Now we have two pointers pointing to the same thing. Since we can do pointer arithmetic we can manipulate these in an interesting way. dyn_ptr++ ; // now points to dynamic_array[1] ; dyn_ptr++ ; // now points to dynamic_array[2] ; if we want to get the value we do this: printf("Value of dyn_ptr = %i\n", *dyn_ptr) ; Since we incremented it twice it will now print out the value 2. In the same way, we could also do static_array++ to make it point to the first element. This may seem like magic, but since the compiler knows how big an int is it can do the proper increment. putting the * before means get the value stored at this memory location.
paulino at Yahoo! Answers Visit the source
Related Q & A:
- Why cant i view my contacts display image? please help if you know ,thanks?Best solution by Yahoo! Answers
- How do I audit a policy document? What areas should I look at and how should it be written?Best solution by Yahoo! Answers
- What do I need to convert my 2004 Nissan Altima into an SE-R model?? ONLY EXTERIOR WISE?Best solution by Yahoo! Answers
- Do I need to get my car inspected before I get it registered in New York State?Best solution by Yahoo! Answers
- Do i need a transit visa for London, if i have permanent residence in Italy?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.