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 16-Nov-2005, 01:17
cougar1112 cougar1112 is offline
New Member
 
Join Date: Nov 2005
Posts: 5
cougar1112 is on a distinguished road

About my Sorting with Pointer Notation Program.(Help)!


Ok guys im back again because I got an extension till 11:00 pm Wednesday to turn in my program. Ok I finished the program, but I will get 10 points off because I used Array Notations. Im having trouble changing the array notation to pointers so I need some help. Here is my instructions and my completed program is below.

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



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;
}
  #2  
Old 16-Nov-2005, 02: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: About my Sorting with Pointer Notation Program.(Help)!


Hi Cougar,

Why start a new thread when you already have a thread about this assignment?

Ok. Here is a start:
forget your current code.
Take a look at the Dave's post and continue with your old program/
Just two changes and you'll get maximum points:
http://www.gidforums.com/t-7680.html

Here is a quote from that post:
Quote:
Originally Posted by Dave
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

Best 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.
  #3  
Old 16-Nov-2005, 02:47
cougar1112 cougar1112 is offline
New Member
 
Join Date: Nov 2005
Posts: 5
cougar1112 is on a distinguished road

Re: About my Sorting with Pointer Notation Program.(Help)!


Ok I will redo my old code instead of the new one that I did. Hopefully it will sort the names like the new code.

peace
  #4  
Old 16-Nov-2005, 07:46
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
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: About my Sorting with Pointer Notation Program.(Help)!


Quote:
Originally Posted by cougar1112
Ok guys im back again because I got an extension till 11:00 pm Wednesday to turn in my program. Ok I finished the program,

The whole idea about pointer notation and array notation is based on the following very fundamental definition in C and C++:

If ypt is a pointer to anything and i is an integer data type, then the following two expressions are symantically equivalent. This is fundamental to your understanding of the C language (C++ too) as it is used!!!!!!!!!! (Oops---I just broke the exclamation point key.)

Code:
ypt[i] *(ypt+i)

So you can have things like the following:

Code:
int *ypt; int i = 3; int x[10]; ypt = &x[3]; ypt[i] = 42;

Furthermore, if yar[] is an array of anything, then if a program uses the name, yar, by itself (with no brackets) it is treated as a constant pointer to the type of data. (And the value of the pointer is fixed as the address of the first element of the array.) So, again, the following two things are equivalent:

Code:
yar[i] *(yar+i)
Note that in this case, since yar is a const pointer, there are some restrictions on how it is used:

Code:
int yar[10]; int *intpt; intpt = yar; /* that's ok */ yar = x; /* won't work */

but you can have
Code:
y[i] = x; *(y+i) = x;

A few examples:
CPP / C++ / C Code:
#include <stdio.h>

int main()
{
  int x[100];
  int i;
  int *intpt;

  for (i = 0; i < 100; i++) { /* just set members of x to different values */
    x[i] = i;
  }
  intpt     = &x[0];      /* set intpt to the address of the first element */
  intpt     = x;          /* exactly the same thing */
  intpt     = intpt + 6;  /* now intpt is equal to the address of x[6]; */
  intpt[30] = 10;         /* What element of x does this set equal to 10 */
  x[20]     = *(intpt+5); /* What is the new value of x[20]? */
  *(x+19)   = 17;         /* What does this do? */
  *(intpt+3)= 5;          /* What does this do? */

  return 0;
}



In addition to the point about pointers, if I were grading the assignment I would deduct points for every single requirement that was stated. (And I must say, that this is a very clear and complete program specification. I wish all of the things I have ever been given to work on had such a good problem statement. Heck, I wish any of them had.)


1. What about this requirement?

Quote:
Note: You need to pass your data by using pointer to a function called:

o sort_name(char *name [], int *item)

2. Have you tested your program(s) with the following input names, given as examples in the program assignment?
Quote:
John Doe
Joe More
Steven Smith

3. Remember this requirement:
Quote:
Your program has to sort them (by firstname) in ASCENDING order.

4. Don't forget this:
Quote:
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).
.
.
.
o You can clear screen by printing new line character or endl about 20 times.

A final note: in the previous thread I did not intend to imply that I think your sort routine itself is correct. I just wanted to get you to a place where the program would compile so that you can test it. My advice is always this: It's your program. You test it. Don't ever take anything (ideas or code) from anywhere (book, magazine, internet, professional colleague, classmate, or anywhere else --- not even your own past programs that seemed to work) and just assume that it's OK. Test each step before going on to the next.

Regards,

Dave
Last edited by davekw7x : 16-Nov-2005 at 08:50.
 
 

Recent GIDBlogAccepted for Ph.D. program 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
2D arrays:dynamic allocation and freeing bravetanveer C Programming Language 48 27-Nov-2007 16:55
Sorting program using pointer notation NEED HELP!! cougar1112 C++ Forum 16 14-Nov-2005 12:15
Pointer Usage in C++: Beginner to Advanced varunhome C++ Forum 0 19-Aug-2005 10:25
[Tutorial] Pointers in C (Part II) Stack Overflow C Programming Language 0 27-Apr-2005 18:36

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 02:18.


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