How to change array elements?

How to sort C++ array in descending order (code given)?

  • How can I change this function to display elements in descending order? It's also not displaying all the elements. Currently it gives the array in ascending order. I've tried ...show more

  • Answer:

    Array's are passed by what...reference. You are passing values out of a function to your main file. So if you declare values of an array in a function can you pass them to the main...NO! Arrays are passed by reference. Declare values in the main. Call the array in the function. Do the sort there. Create another function to return the array. Call the function in the main. Here is what I'm talking about for everyone who has this exact same problem: I hate giving out code but I see this alot: Here I am going to manipulate a single integer and the value inside an array. All the while I will never actually enter the value's of the array into the function, or the integer into it's function (function 1 below). I will simply point to them. This saves time, memory, and a headache way down the road. And..it's the only way your going to get it to work. The numbers in the array are 5 10 15 20. I am going to tell the array to change value of intNumber1 to 9 (for no reason) and decide that the first element of the array is..4 (for no reason.) Here's what you remember: Even though you never change the element of the array, when the array is called, the pointers are called, and 5 become 4 thanks to the second function. Reference my friend. reference. #include <iostream> #include <string> using namespace std; void changeNumber(int &_intNumber1) { ......_intNumber1=9; } // changeNumber(##) void changeMyArray(int _intMyArray[]) { ......._intMyArray[0] = 4; } // Change Array(##) int main() { .........int intNumber1=5; .........int intMyArray[]= {5, 10, 15, 20}; .........changeNumber (intNumber1); .........cout << "The value of intNumber1 is: " << intNumber1 << endl; .........for (int i=0; i < 4; i++) .........cout << intMyArray[i] << endl; .........changeMyArray (intMyArray); // Right Here the Value of [0] changes to 4. Without this line [0] = 5 .........cout << "The first element of the array is: " << intMyArray[0] << "." << endl; .........return 0; }

XO5FFJZFJTO4RGBIPUCVKKH2C4 at Yahoo! Answers Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

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.