GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 12-Nov-2005, 17:37
cougar1112 cougar1112 is offline
New Member
 
Join Date: Nov 2005
Posts: 5
cougar1112 is on a distinguished road

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:
#include <iomanip.h>
#include <iostream.h>
#include <cstring.h>
void sortptrs(char * nameptr[], int n);
main()
{
 int what, limit, j;
 char name[20][15];
 char why;
 what = 2;
 while(what == 2)
 {
 cout << "\nPlease enter the number of names you will sort\n";
 cin >> limit;
 
 for (j=0; j<limit; j++)
 {
  cout << "\nName: ";
   cin >> name[j];
   
 }
sortptrs(name, limit);
  for (j=0; j<limit; j++)
 {
  cout << "\nName: ";
   
   cout << name[j];
   
 }
cout << "\n\nTo END the program Press - Q -\n";
cout << "To CONTINUE, PRESS - Any Letter -\n";
cin >> why;
if(why == 'q' || why == 'Q')
break;
 }

}

void sortptrs(char * nameptr[], int n)
{
 int i, eff_size, maxpos=0;
 char *tmpptr;
 for(eff_size=n; eff_size > 1; eff_size--)
 {
for(i=0; i<eff_size; i++)
if(strcmp(nameptr[i], nameptr[maxpos])>0)
maxpos=i;
tmpptr=nameptr[maxpos];
nameptr[maxpos]=nameptr[eff_size-1];
nameptr[eff_size-1]=tmpptr;
 }
}
Last edited by admin : 12-Nov-2005 at 20:17. Reason: Please insert your C code between [c] & [/c] tags
  #2  
Old 13-Nov-2005, 01:31
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

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:
error C2664: 'sortptrs' : cannot convert parameter 1 from 'char [20][15]' to 'char *[]'
Types pointed to are unrelated;
You have a 2 dimensional array you're passing as a pointer array. Change your parameter to a double array as the value is defined:
CPP / C++ / C Code:
void sortptrs(char nameptr[20][15], int n)


Also, you are missing braces in the for loop in sortptrs():
CPP / C++ / C Code:
        for(i=0; i<eff_size; i++)
        if(strcmp(nameptr[i], nameptr[maxpos])>0)
        maxpos=i;
        tmpptr=nameptr[maxpos];
        nameptr[maxpos]=nameptr[eff_size-1];
        nameptr[eff_size-1]=tmpptr;
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
  #3  
Old 13-Nov-2005, 03:52
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 929
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

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:
#include <iomanip>
#include <iostream>
#include <cstring>
because if you include .h, then the it would be depreciated header in C++.

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:
using namespace std;
Because cout, cin etc are in namespace std;

The last one:
you cannot assign one char* to other.
so, if you do like this:
CPP / C++ / C Code:
       tmpptr=nameptr[maxpos];
        nameptr[maxpos]=nameptr[eff_size-1];
        nameptr[eff_size-1]=tmpptr;
an error message will say that:
ISO forbids assignment of arrays.

so, you should do something like this:
CPP / C++ / C Code:
strcpy( tmpptr, nameptr[ maxpos ] );
where strcpy is the function that copies one character array to another.

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  
Old 13-Nov-2005, 07:48
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,217
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Sorting program using pointer notation NEED HELP!!


Quote:
Originally Posted by WaltP
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.


Change your parameter to a double array as the value is defined:
CPP / C++ / C Code:
void sortptrs(char nameptr[20][15], int n)


This will not work with his sort routine, since the program performs the following:
CPP / C++ / C Code:
nameptr[maxpos]=nameptr[eff_size-1];

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:
Originally Posted by couger1112
Note: You need to pass your data by using pointer to a function called:
o sort_name(char *name [], int *item)

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:
 char *name[20];
 int i;
 for (i = 0; i < 20; i++) {
   name[i] = new char[15];
 }

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:
 char *name[20];
 int i;
 for (i = 0; i < 20; i++) {
   *(name+i) = new char[15];
 }

Now, if you don't want to use dynamic allocation, you could do something like this:

CPP / C++ / C Code:
 char *name[20];
 char names[20][15];
 int i;

 for (i = 0; i < 20; i++) {
   name[i] = &names[i][0];
 }


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:
 for (i = 0; i < 20; i++) {
   *(name+i) = &names[i][0];

Now, get rid of the array notation on the right hand side to get maximum points.

Regards,

Dave
  #5  
Old 13-Nov-2005, 07:59
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,217
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Sorting program using pointer notation NEED HELP!!


Quote:
Originally Posted by Paramesh
The last one:
you cannot assign one char* to other.
so, if you do like this:
CPP / C++ / C Code:
       tmpptr=nameptr[maxpos];
        nameptr[maxpos]=nameptr[eff_size-1];
        nameptr[eff_size-1]=tmpptr;
an error message will say that:
ISO forbids assignment of arrays.

so, you should do something like this:
CPP / C++ / C Code:
strcpy( tmpptr, nameptr[ maxpos ] );
where strcpy is the function that copies one character array to another.

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  
Old 13-Nov-2005, 10:00
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 929
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough
Talking

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  
Old 13-Nov-2005, 11:21
cougar1112 cougar1112 is offline
New Member
 
Join Date: Nov 2005
Posts: 5
cougar1112 is on a distinguished road

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:
#include <iomanip.h>
#include <iostream.h>
#include <string.h>
void sortptrs(char strtab[20][15], int lim);
main()
{
 int what, limit, j;
 char strtab[20][15];
 char why;
 what = 2;
 while(what == 2)
 {
 cout << "\nPlease enter the NUMBER of names you will sort\n";
 cin >> limit;
 
 for (j=0; j<limit; j++)
 {
  cout << "\nName: ";
   cin>> strtab[j];
   
 }
 sortptrs(strtab, limit);
 cout << "\n\nBelow is the Sorted List of Names\n\n"; 
 
 for (j=0; j<limit; j++)
 {
  cout << "\nName: ";
   
   cout << strtab [j];
   
 }
cout << "\n\nTo END the program Press - Q -\n";
cout << "To CONTINUE, PRESS - Any Letter -\n";
cin >> why;
if(why == 'q' || why == 'Q')
break;
 }

}

void sortptrs(char strtab [20][15], int lim)
{
 int i, eff_size, maxpos = 0;
 char tmp[15];
 for(eff_size = lim; eff_size > 1; eff_size--)
 {
  for(i=0; i<eff_size; i++)
   if(strtab[i] > strtab[maxpos])
    maxpos = i;
   strcpy(tmp, strtab[maxpos]);
   strcpy(strtab[maxpos], strtab[eff_size - 1]);
   strcpy(strtab[eff_size - 1], tmp);
 }
return;
}
Last edited by LuciWiz : 14-Nov-2005 at 03:01. Reason: Please insert your C++ code between [c++] & [/c++] tags
  #8  
Old 13-Nov-2005, 11:28
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 929
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

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:
Originally Posted by cougar
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.

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  
Old 13-Nov-2005, 12:00
cougar1112 cougar1112 is offline
New Member
 
Join Date: Nov 2005
Posts: 5
cougar1112 is on a distinguished road

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:
#include<iomanip.h>
#include<iostream.h>
#include<cstring>
void sort_name(char * name[], int n); // prototype
int main()
{ //variables declared
 
 int what, limit, j;
 char name[20][15];
 char why;
 what = 2;
 while(what==2)
 
{
 cout<< "Please enter the number of names you will sort\n";
  cin >> limit;
 for (j=0; j<limit;j++)
{
 cout << "\nName ";
  cin >> name[j];
}
 
 void sort_name(char * name[], int n);//sort
 for (j=0; j<limit; j++)
{
 cout << "\nName ";
 cout << name[j];
}
 
 cout << "\n\nTo END the program Press - Q -\n\n";//Quit
 cout << "To CONTINUE, PRESS - Any Letter Or Quit -";//Continue
  cin >> why;
  
  if(why == 'q' || why == 'Q')
  
break;
}
return 0;  // indicates successful termination
}// end main
 void sort_name(char * name[], int n)//sorts names with pointers
{
 int i, eff_size, maxpos=0;
 char *tmpptr;
 
 for(eff_size=n; eff_size > 1; eff_size--)
{
 for(i=0; i<eff_size; i++)
 if(strcmp(name[i], name[maxpos])>0)
 maxpos=i;
 tmpptr=name[maxpos];
 name[maxpos]=name[eff_size-1];
 name[eff_size-1]=tmpptr;
}
}// end function
Last edited by LuciWiz : 14-Nov-2005 at 03:02. Reason: Please insert your C++ code between [c++] & [/c++] tags
  #10  
Old 13-Nov-2005, 12:06
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 929
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: Sorting program using pointer notation NEED HELP!!


Quote:
Originally Posted by cougar
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.
You are always welcome.
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 GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 05:04.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.