GIDForums  

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

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 23-Dec-2007, 09:22
NeRdHeRd NeRdHeRd is offline
New Member
 
Join Date: Dec 2007
Posts: 10
NeRdHeRd is on a distinguished road

Strings, Pointers, and Printing Alphabetically


Could someone please look at the code below and tell me what I'm doing wrong. I'm supposed to input a string of seven words and then print them on the scree. After that I am supposed to use strcmp() to help sort them and them print them alphabetically on the screen. Since I really have no Idea what the hell I'm doing I'd appreciate any help I could get. Thanks.

CPP / C++ / C Code:
/*Purpose: list words in alphabetical order*/

#include <stdio.h>
#include <string.h>

#define N_STRINGS 7
#define CHARS 15	


void swap(char*, char*);
void bubble(char*, int);

int main(void)
{
char a[N_STRINGS][CHARS];	
int i;

printf("%s\n\n%s%d%s",
"This program will put words in alphabetical order.",
"Enter ", N_STRINGS," words: ");
for (i = 0; i < N_STRINGS; ++i){
scanf("%s",&a[i]);	
}
printf("\nHere is the list of words you entered: \n");
for (i = 0; i < N_STRINGS; ++i)
printf("%s\n",&a[i]);	

bubble(*a, N_STRINGS);	
printf("\nHere are your words in alphabetical order:\n");	
for (i = 0; i < N_STRINGS; ++i)
printf("%s\n",&a[i]);	


return 0;
}


void bubble(char *w, int n)	
{	 int i, j;

for (i = 0; i < n; ++i){
for (j = i +1; j < n; ++j){
if (strcmp(&w[i], &w[j])>0)
swap(&w[i],&w[j]);
}
}
}

void swap(char *p, char *q)
{
char tmp;

tmp = *p;
*p = *q;
*q = tmp;
}
  #2  
Old 23-Dec-2007, 13:31
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 846
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: Strings, Pointers, and Printing Alphabetically


If you wrote that you must have SOME idea of what's going on...
You need to tell us some things so we can get a sense of where you're 'at' like...
What happens?
Do you get an error on compiling? post that (in code tags please)
Do you get un expected output? post that (in code tags please)

Did you write all that and then try to compile/run?
You should 'build' in pieces, compiling and running to test as you go.
That will help to narrow down a problem's causes.
Like do a single scanf() printf(),,, if it works move to next,,, if not find out why and fix.
repeatrepeatrepeatrepeatrepeat...
  #3  
Old 23-Dec-2007, 19:12
NeRdHeRd NeRdHeRd is offline
New Member
 
Join Date: Dec 2007
Posts: 10
NeRdHeRd is on a distinguished road

Re: Strings, Pointers, and Printing Alphabetically


Ok. Sorry about that. I have no errors when compiling or running the program. The program prompts me for the seven words. I then enter the words and I get them printed back in list form and then get the list again where they are supposed to be alphabetized. See below:

Code:
This program will put words in alphabetical order. Enter 7 words: xray alpha tango lima yankee charlie whiskey Here is the list of words you entered: xray alpha tango lima yankee charlie whiskey Here are your words in alphabetical order: xray alpha tango lima yankee charlie whiskey test has exited with status 0.

The strcmp() is supposed to help in sorting them but for the life of me I can't figure it out. Thank for the help.
  #4  
Old 24-Dec-2007, 10:11
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 846
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: Strings, Pointers, and Printing Alphabetically


Sorry, this has gotten long.... so I started with:
gcc -Wall -W -pedantic nh10.c -o nh10.exe
nh10.c: In function `main':
nh10.c:34: warning: char format, different type arg (arg 2)
nh10.c:38: warning: char format, different type arg (arg 2)
nh10.c:43: warning: char format, different type arg (arg 2)

Ok it compiles with some warnings and a run looks like this:
nh11.exe
Code:
This program will put words in alphabetical order. Enter 7 words: hello world and how are you today Here is the list of words you entered: hello world and how are you today Here are your words in alphabetical order: world and how are you today
----------
First print looks ok.
So what happens to the hello on second print?
First fix those warnings...
CPP / C++ / C Code:
    scanf("%s", a[i]);       /* scanf("%s", &a[i]);    line 34 */
    printf("%s\n", a[i]);    /* printf("%s\n",&a[i]);  line 38 */
    printf("%s\n", a[i]);    /* printf("%s\n", &a[i]); line 43 */
See? I removed the '&' which preceeded the a[i]....
While it 'worked' in this case, it is not the proper way to do this.
The '&' is used to furnish the address of an object.
I guess you got the idea from a scanf() example??
That example was probably for a single data like:
CPP / C++ / C Code:
 char a;  scanf("%c", &a); 
The deal here is that you have an ARRAY of chars.

This is important:
The identifier name you give an array is a pointer to the space set aside in
memory for the array data. In the this case:
CPP / C++ / C Code:
char a[N_STRINGS][CHARS];  / * is the same as: * /  char a[7][15] 
A char is 1 byte and you have 7*15 of them... so the identifier 'a' points
to the starting address of 105 contiguous bytes in memory.
AKA: the array's 'base' address.
Try sticking this in place of the first printf() loop:
CPP / C++ / C Code:
  printf("\na= %p \n", (void*)a );
  for(i = 0; i < N_STRINGS; i++)
    printf("a[%d]= %p   %s \n", i, (void*)a[i], a[i] );
The %p is a specifier to print an address (in hex).
Obviously something's wrong with the bubble() because the first string gets
messed up in the second output so I comment that out for and the output is now:
Code:
This program will put words in alphabetical order. Enter 7 words: hello world and how are you today Here is the list of words you entered: a= 0073FD80 a[0]= 0073FD80 hello a[1]= 0073FD8F world a[2]= 0073FD9E and a[3]= 0073FDAD how a[4]= 0073FDBC are a[5]= 0073FDCB you a[6]= 0073FDDA today Here are your words in alphabetical order: hello world and how are you today

Note a and a[0] are the same address AND that the address of each of the
elements are exactly 'CHARS' bytes (or 15) apart.

Get the picture? I hope so because you're going to need to understand these
concepts to figure out what's wrong with bubble().
I have some stuff to get to and I won't be able to address the rest of this
right now except to say that I need to review this too as I am having trouble
passing a pointer to bubble()... (void* ???) It can be tricky to get the
correct information to a function for it to work.
I will leave you with this link to an EXCELLENT tutor on pointers which I will
be re-reading for help. It has an example of what you are trying to do :
http://pw1.netcom.com/~tjensen/ptr/pointers.htm
In addition to that there are several great threads on this forum re: this.
Go to the top right of the forum listings page.
Search 'pass, dimensional, array' or 'pass array strings' etc...
Merry Whatever!,, Howard();
Last edited by Howard_L : 24-Dec-2007 at 11:20.
  #5  
Old 24-Dec-2007, 13:35
NeRdHeRd NeRdHeRd is offline
New Member
 
Join Date: Dec 2007
Posts: 10
NeRdHeRd is on a distinguished road

Re: Strings, Pointers, and Printing Alphabetically


Quote:
Originally Posted by Howard_L
Sorry, this has gotten long.... so I started with:
gcc -Wall -W -pedantic nh10.c -o nh10.exe
nh10.c: In function `main':
nh10.c:34: warning: char format, different type arg (arg 2)
nh10.c:38: warning: char format, different type arg (arg 2)
nh10.c:43: warning: char format, different type arg (arg 2)

Ok. I have no idea what any of the above means. Looks like jibberish to me.

Anyway, thanks for the link it has alot of good information. Unfortunately, I don't really understand it nor do I really care too. I'm just trying to fight through this course. If I can get a C I'll be more than happy. Thanks for all the pointers. I'll be banging my head on my desk until I get this thing to work.
  #6  
Old 24-Dec-2007, 20:08
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 846
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: Strings, Pointers, and Printing Alphabetically


...I guess so,, sorry
  #7  
Old 25-Dec-2007, 12:37
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 846
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: Strings, Pointers, and Printing Alphabetically


Perhaps this will help without giving too much away...
CPP / C++ / C Code:
/* IF YOU HAVE: .. */
#define N_STRINGS  7
#define CHARS     15
char a[N_STRINGS][CHARS];

scanf("%s",&a[i]);       /* NO  */
scanf("%s",a[i]);        /* YES */

printf("%s\n", &a[i]);   /* NO  */
printf("%s\n", a[i]);    /* YES */

/* You can not pass an entire array to a function,
   so we pass a pointer to address of the array in memory.
   The function will not know the size so we have to pass that too.
so THE FUNCTION:                                                   */
void bubble(char*, int);                    /* NO  */
void bubble(void * a, int rows, int cols);  /* YES */

/* and it MIGHT BE CALLED LIKE: */
bubble(*a, N_STRINGS);        /* NO  */
bubble(a, N_STRINGS, CHARS);  /* YES */

/* THE FUNCTION: */
void swap(char*, char*);  /* YES            */
                          /* ... IMPORTANT: */
char temp[CHARS];    /* NEED AN ACTUAL ARRAY FOR STORAGE, NOT JUST A POINTER   */
strcpy()        /* USE TO SWAP DATA AT LOCATIONS, NOT SWAP POINTER VALUES */
This output might give some clues about how the sort/swap is looped etc..
It's close to what was originally posted.:
Code:
D:\C\gidforum\nerdherd> nh12.exe This program will put words in alphabetical order. Enter 7 words with up to 15 characters each: hello world and how are you today Here is the list of words you entered: a= 0073FD80 arr[0]= 0073FD80 hello arr[1]= 0073FD8F world arr[2]= 0073FD9E and arr[3]= 0073FDAD how arr[4]= 0073FDBC are arr[5]= 0073FDCB you arr[6]= 0073FDDA today not swapping 0073FD80 ( hello) with 0073FD8F ( world) swapping 0073FD8F ( world) with 0073FD9E ( and) swapping 0073FD9E ( world) with 0073FDAD ( how) swapping 0073FDAD ( world) with 0073FDBC ( are) not swapping 0073FDBC ( world) with 0073FDCB ( you) swapping 0073FDCB ( you) with 0073FDDA ( today) swapping 0073FD80 ( hello) with 0073FD8F ( and) not swapping 0073FD8F ( hello) with 0073FD9E ( how) swapping 0073FD9E ( how) with 0073FDAD ( are) not swapping 0073FDAD ( how) with 0073FDBC ( world) swapping 0073FDBC ( world) with 0073FDCB ( today) not swapping 0073FDCB ( world) with 0073FDDA ( you) not swapping 0073FD80 ( and) with 0073FD8F ( hello) swapping 0073FD8F ( hello) with 0073FD9E ( are) not swapping 0073FD9E ( hello) with 0073FDAD ( how) not swapping 0073FDAD ( how) with 0073FDBC ( today) not swapping 0073FDBC ( today) with 0073FDCB ( world) not swapping 0073FDCB ( world) with 0073FDDA ( you) not swapping 0073FD80 ( and) with 0073FD8F ( are) not swapping 0073FD8F ( are) with 0073FD9E ( hello) not swapping 0073FD9E ( hello) with 0073FDAD ( how) not swapping 0073FDAD ( how) with 0073FDBC ( today) not swapping 0073FDBC ( today) with 0073FDCB ( world) not swapping 0073FDCB ( world) with 0073FDDA ( you) not swapping 0073FD80 ( and) with 0073FD8F ( are) not swapping 0073FD8F ( are) with 0073FD9E ( hello) not swapping 0073FD9E ( hello) with 0073FDAD ( how) not swapping 0073FDAD ( how) with 0073FDBC ( today) not swapping 0073FDBC ( today) with 0073FDCB ( world) not swapping 0073FDCB ( world) with 0073FDDA ( you) not swapping 0073FD80 ( and) with 0073FD8F ( are) not swapping 0073FD8F ( are) with 0073FD9E ( hello) not swapping 0073FD9E ( hello) with 0073FDAD ( how) not swapping 0073FDAD ( how) with 0073FDBC ( today) not swapping 0073FDBC ( today) with 0073FDCB ( world) not swapping 0073FDCB ( world) with 0073FDDA ( you) Here are your words in alphabetical order: arr= 0073FD80 arr[0]= 0073FD80 and arr[1]= 0073FD8F are arr[2]= 0073FD9E hello arr[3]= 0073FDAD how arr[4]= 0073FDBC today arr[5]= 0073FDCB world arr[6]= 0073FDDA you
Peace on Earth , Goodwill Toward (All) Men (and Women)
 
 

Recent GIDBlogVista ?Widgets? on Windows XP by LocalTech

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 09:25

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

All times are GMT -6. The time now is 17:54.


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