![]() |
|
#1
|
|||
|
|||
Sorting program using pointer notation NEED HELP!!Here is my instructions and my completed program is below. I want to finish this before I go to work so please help. Ive been stuck on it the whole day.
Write a C++ program to solve the following problems. Through out this program assignment you need to use pointer notation. You cannot use array notation. -10 points if you use array notation anywhere in your program except for declaration of your array or reading names form the keyboard. Example of array notation: A[0], A[3][10], A[0][3], etc Example of pointer notation: *A, *(*(A+2)+3), *(*A+2), etc Write a program to prompt the user to enter number of names to be sorted. This number of name has a range from 1 to 20 people. Once you have this number then you need to prompt the user to input names as many number of items as he has specified. These names could be in any order. Your program has to sort them (by firstname) in ASCENDING order. Once you are done, you need to clear screen and print the result. Your first output should be the original set of names (unsorted), then followed by second set of your output (sorted names). At the end of the list, your program has to check whether or not the user wants to quit. If he selects "Q" or "q", then your program should clear the screen and terminate your program. Note: You need to pass your data by using pointer to a function called: o sort_name(char *name [], int *item) o You can clear screen by printing new line character or endl about 20 times. Example: Please enter number of names you want to sort: 4 Please enter 5 items: John Doe Joe More Steven Smith Marry Ann UNSORTED John Doe Joe More Steven Smith Marry Ann SORTED Steven Smith Marry Ann John Doe Joe More Do you want to quit? Q HERE IS MY PROGRAM- I have one error and here it is error C2664: 'sortptrs' : cannot convert parameter 1 from 'char [20][15]' to 'char *[]' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast CPP / C++ / C Code:
Last edited by admin : 12-Nov-2005 at 20:17.
Reason: Please insert your C code between [c] & [/c] tags
|
|||
|
#2
|
||||
|
||||
Re: Sorting program using pointer notation NEED HELP!!Welcome to GID. Please read the Guidelines prominently posted at the top of the forum. They'll help you post readable code and understandable questions. Also, check out this information on formatting C/C++ code so your code can be read easier.
The error you posted is quite obvious: Quote:
CPP / C++ / C Code:
Also, you are missing braces in the for loop in sortptrs(): CPP / C++ / C Code:
__________________
During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence? |
|
#3
|
||||
|
||||
Re: Sorting program using pointer notation NEED HELP!!Hi Cougar,
Welcome to the GID Forums. Two more things you have to consider: The #include headers should be: CPP / C++ / C Code:
and if you include <cstring.h>, you probably have got an error saying that there is no cstring.h. Now that you have included correct header files, you have to include this line: CPP / C++ / C Code:
The last one: you cannot assign one char* to other. so, if you do like this: CPP / C++ / C Code:
ISO forbids assignment of arrays. so, you should do something like this: CPP / C++ / C Code:
Since you use C++, you can use string instead of char*. That will help a lot in clearing error messages. Regards, Paramesh. __________________
Don't walk in front of me, I may not follow. Don't walk behind me, I may not lead. Just walk beside me and be my friend. |
|
#4
|
|||
|
|||
Re: Sorting program using pointer notation NEED HELP!!Quote:
This will not work with his sort routine, since the program performs the following: CPP / C++ / C Code:
For your example nameptr would be treated as a constant pointer, and the assignment would be illegal and the compiler will complain. Furthermore, according to his assignment Quote:
No, looking at the program, I think that the sort routine would work OK, but its argument needs to be an array of pointers to char. So, there are a couple of ways to make an array of pointers to char (and to make each of the pointers point to something useful): Here's a way that you see a lot, using dynamic memory allocation: CPP / C++ / C Code:
Now, name is an array of 20 pointers to char. Then, each element of name[] is set equal to the address of an array[15] of char. The rest of the program can stay the same (except that, before exiting the program you should remember to delete [] everything that was obtained from new []). Note to the Original Poster: the assignment said to use pointer notation everywhere, so maybe the loop above should be written: CPP / C++ / C Code:
Now, if you don't want to use dynamic allocation, you could do something like this: CPP / C++ / C Code:
Now, I have an array of pointers to char. I set each of the pointers to the address of a row of the 2-d array. (And each row of the 2-d array is an array[20] of char to hold a name.) Note to the Original Poster: how would you use pointer notation in the loop? I'll do one of them, you do the other. (The whole point of the assignment seems to be to get you familiar with pointer notation and its relationship to arrays ---- A really, realllly, realllllllllly great assignment!!!) CPP / C++ / C Code:
Now, get rid of the array notation on the right hand side to get maximum points. Regards, Dave |
|
#5
|
|||
|
|||
Re: Sorting program using pointer notation NEED HELP!!Quote:
Of course you can assign one char* to another (as long as the one being assigned to is not const). He is not assigning an array. In the Original Poster's code, in this routine, nameptr[] is an array of pointers to char. So, nameptr[0] is a pointer to char; nameptr[1] is a pointer to char, etc. Since temptr is a pointer to char, it's legal to make the assignment the way he did. Furthermore, and I hate to repeat myself, but: temptr is a pointer to char, not an array of char and is not initialized to anything. If you use strcpy(temptr, anything), you won't get a compile time error, but you very well may get a run time error. This is one of the most common run-time error generators on the face of the planet: writing to something that is not pointing to a valid memory address. (Actually, it's the most common run-time generator in the entire solar system --- I checked it.) His code is correct: In order to swap names in the sort routine, he is changing the values of the pointers, not actually moving the strings around. That's a really excellent way to do it (some people might even say that it's the "right" way to do it). The sort routine works as it should. Try it (after you get the arguments correct for calling the sort function). Note that I said, "the sort routine works." Make the modification to the array declarations that I suggested in my previous post, and it sorts the names (at least I think it does --- check it out). I did not say that the original assignment will be satisfied by the program in that slightly modified form. As you test the program with the sample input names given in the assignment, there may be other problems to solve, but at least it's a start. Regards, Dave P.S. (Occasionally I see a statement something like, "I am a pretty good C programmer, but I'm not very good at pointers." It is to laugh. |
|
#6
|
||||
|
||||
Re: Sorting program using pointer notation NEED HELP!!Hi Dave,
Thank You for your wonderful explanation. Since you told not to say sorry, i'll correct my mistakes from now. That statement would fit to me. I modify that statement from: "I am a C programmer, and I'm not very good at pointers" to "I am a pretty good C programmer, and I'm good at pointers" With the help of yours, Walt's and GID Forum's guidance. Paramesh. __________________
Don't walk in front of me, I may not follow. Don't walk behind me, I may not lead. Just walk beside me and be my friend. |
|
#7
|
|||
|
|||
Re: Sorting program using pointer notation NEED HELP!!OK guys thanks for the help but I had to turn this assignment by 8pm Saturday so this was my final program. Did I follow correctly the directions of my assignment because this program was able to sort the names.
CPP / C++ / C Code:
Last edited by LuciWiz : 14-Nov-2005 at 03:01.
Reason: Please insert your C++ code between [c++] & [/c++] tags
|
|
#8
|
||||
|
||||
Re: Sorting program using pointer notation NEED HELP!!Hi cougar,
Dont read the first two posts. Read the third one: davekw7x's post. if you submit this program, you would probably get -10 points: Quote:
Read Dave's post again to get maximum points. Paramesh. __________________
Don't walk in front of me, I may not follow. Don't walk behind me, I may not lead. Just walk beside me and be my friend. |
|
#9
|
|||
|
|||
Re: Sorting program using pointer notation NEED HELP!!I will take Dave's advice on my program in future programs. Since I had to turn in my program yesterday I will have 10 points deducted but that should be it. Also I sent my TA another program that just kept the names unsorted but it did not sort them. Hopefully he wont take 10 points since I submitted 2 programs.Below is the other program.
peace PS- We have one last program which I hear is gonna be insane so I will be back on these boards in about a week or 2. CPP / C++ / C Code:
Last edited by LuciWiz : 14-Nov-2005 at 03:02.
Reason: Please insert your C++ code between [c++] & [/c++] tags
|
|
#10
|
||||
|
||||
Re: Sorting program using pointer notation NEED HELP!!Quote:
Remember that you have to insert your code between [c++] and [/c++] tags so that it looks nice and easier to read. Best of luck for your assignment. Paramesh. __________________
Don't walk in front of me, I may not follow. Don't walk behind me, I may not lead. Just walk beside me and be my friend. |
Recent GIDBlog
Programming ebook direct download available by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Pointer Usage in C++: Beginner to Advanced | varunhome | C++ Forum | 0 | 19-Aug-2005 10:25 |
| Type casts ? | kai85 | C++ Forum | 12 | 23-Jun-2005 13:04 |
| [Tutorial] Pointers in C (Part II) | Stack Overflow | C Programming Language | 0 | 27-Apr-2005 18:36 |
| [Tutorial] Pointers in C (Part I) | Stack Overflow | C Programming Language | 1 | 08-Apr-2005 19:35 |
| fltk-2.0 cvs | Plumb | FLTK Forum | 20 | 13-Nov-2004 08:10 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The