Ask your own question, for FREE!
Computer Science 9 Online
OpenStudy (anonymous):

C++ language: How can I sort a number of grades for example : 6,6,8,9,5,7,8 from the lowest to the gratest without using a vector,and using a function sort?

OpenStudy (konradzuse):

do you know how to make functions? You would basically need to compare 1 entry to another, using a for loop and comparing it from the highest to the lowest. Then you keep running the loop(most likely a while loop) until you are all sorted.

OpenStudy (anonymous):

Yes I do know how to make a function.Thnx for your help.

OpenStudy (konradzuse):

np medal :O.

OpenStudy (espex):

Technically called a bubble sort. :)

OpenStudy (anonymous):

Can anywone help me with the code ?

OpenStudy (espex):

Paste what you have, I'm sure that @KonradZuse would be more than happy to help, and I'll do what I can. :)

OpenStudy (anonymous):

Ok,the problem is : I have to write a program where the user can input some grades,as long as he wants,and then with those grades i have to find the average and to sort from lowest to highest using two functions,sort and average..without using an array ...

OpenStudy (espex):

Well do it for two static grades first, so you have something to manipulate, then you have an opportunity to get your functions working without introducing any complexity. Once your functions are working change one of the grades so that you get it from the user, when that is functional just wrap it in a while loop.

OpenStudy (anonymous):

I cant do it :(

OpenStudy (espex):

Can you make it print two different results to the screen?

OpenStudy (anonymous):

#include<iostream> using namespace std; int i=3,j=5; int main() { int sort(int x ,int y) {int max=x; int min=y; if (x<y) { max=y; min=x;} else { max=x; min=y;} } cout<<"Sorting elements are:"<<sort(i,j)<<endl; system("pause"); EXIT_SUCCESS; } where is the problem here???

OpenStudy (espex):

You declare your ints i/j as global variables, but then you start your main and go right into sorting x/y but they are never assigned a value.

OpenStudy (anonymous):

so what should i do to improve ?

OpenStudy (espex):

A simple solution would be to change i/j to x/y and then your code will work.

OpenStudy (espex):

If you want to use global variables you can just call them directly from any function and your function can change the values. But what you want to do is have your sort feature print a statement or start using pointers.

OpenStudy (anonymous):

error C2601: 'sort' : local function definitions are illegal

OpenStudy (espex):

That is because your sort function is redefining the "now" global variables of x,y.

OpenStudy (espex):

How about this: #include<iostream> using namespace std; void sort(int x ,int y) { int max=x; int min=y; if (x<y) { max=y; min=x; } else { max=x; min=y; } cout << min << max; } int main() { int x = 3, y = 5; cout<<"Sorting elements are:"<<sort(x,y)<<endl; system("pause"); EXIT_SUCCESS; }

OpenStudy (anonymous):

yeah it works with changing void into it and returning a value..but now how can i modificate this code for a lot of inputs from the user?because this is the most difficult part for me

OpenStudy (espex):

Use a loop.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!