I'm am new to programming and I need help with a programming problem. If you can help me please do thank you. I'm trying to teach myself but having a few problems. The problem in my book is as follows: Suppose that the elements of a list are in descending order, and they need to be put in ascending order. Write a C++ function that takes as input an array of items in descending order and the number or elements in the array. The function rearranges the element of the array in ascending order. Your function must not incorporate any sorting algorithms, that is, no item comparisions should take place. I'm using Dev C++ as my compiler.
for a list that is already sorted, reversing it *should* work, but you'd only want to walk through half of the items. maybe like Code: void foo(int *a, int s) { int i = 0, tmp; while(i < s / 2) { tmp = *(a + (s - 1) - i); *(a + (s - 1) - i) = *(a + i); *(a + i) = tmp; ++i; } } where a is the array you pass in and s is the number of elements.
Thank you very much it worked fine I understand what you did now. I have one more question I have been using C++ for dummies and one more book to teach myself. My question is what is a very good book that I can use as a beginner that makes things very basic because I feel like the books I have aren't clear enough.
I agree with you that most books start off with a simple topic and then advance it to the point of confusion. It's very hard to learn on your own; that's what I've tried to do and I always feel like I don't quite understand the fundamentals of the language. I can say that when looking for a book, if it has things like void main(), fflush(stdin), system("pause"), getch() then look for something else as using those is a bad habit to get into - main must always return its status; fflush(stdin) is undefined for input streams like the keyboard; system("pause") and getch() are not portable code. A good source to mill over is the C faq. cprogramming has a decent [url="http://faq.cprogramming.com/cgi-bin/smartfaq.cgi]faq[/url] section too though it can jump from beginner to advanced in a few lines. There are also a handful of posters there who can provide a wealth of knowledge so long as you're prepared. I like to mill over the posts by Salem, Prelude, Hammer, and laserlight. shabbir is pretty good too, but getting him to post these days seems like an uphill challenge. :P In any case, best of luck to you and don't get discouraged if your topics aren't answered... search in various forums looking for similar topics and see if any can help.