473,385 Members | 1,548 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Sorting the Input

On Mon, 29 Sep 2008 15:16:56 +0100, Ben Bacarisse wrote:
No. A function takes what it must due to its design.
get_single_word() needs a char ** because its job is to set a char *
that "does not belong to it". It is passed a pointer to the char *
that is must set: get_single_word(char **);

Giving get_input() a char *** parameter does not mean that this must be
passed to get_single_word. You get to say what you pass!

An abstract overview of my own program by Ben had helped me focus on the
design issue first. I came to know that whole problem went into the wrong
direction as compared to what was intended. I think I am working on this
program from last 1 month and it is still not done, Here is the intent:
Program will ask the user for input and then will sort the input
alphabetically and will print it on stdout

Since I did not want to put any limitation on input size in this
program, I have used dynamic memory allocation. My intent in
creating and then solving this problem was purely of learning
about dynamic memory allocation and C (as defined by ANSI
standard) and nothing else. Regarding storing input words, since
there is no agreed definition of what a word is, I have taken a
very simple approach to it:

"Any contiguous collection of characters, except single or
multiple whitespace, is a word"
As usual in C, each word is stored as an array of characters. To
sort different words we will create another, second array. The
elements of this array will be pointing to each input word and
since each input word is an array, we will be pointing to a
pointer hence this second array will have pointers to pointers as
its elements. Then we will sort the pointers to point to
different words (arrays). This Efficient technique is described
in K&R2, as we are sorting the pointers to arrays, rather than
arrays themselves.


So this array of pointers will be created in main() but since we have no
idea of how many words user will enter, we will be realloc()ing in calling
function passing this array of pointers as argument. Or we can make this
array a global array and then pass it as desired without messing with
function arguments. Which one will be better ?



--
www.lispmachine.wordpress.com
my email is @ the above blog.
Gooogle Groups is Blocked. Reason: Excessive Spamming

Sep 30 '08 #1
1 2402
On 30 Sep, 07:39, arnuld <sunr...@invalid.addresswrote:
On Mon, 29 Sep 2008 15:16:56 +0100, Ben Bacarisse wrote:
No. *A function takes what it must due to its design.
get_single_word() needs a char ** because its job is to set a char *
that "does not belong to it". *It is passed a pointer to the char *
that is must set: get_single_word(char **);
Giving get_input() a char *** parameter does not mean that this must be
passed to get_single_word. *You get to say what you pass!

An abstract overview of my own program by Ben had helped me focus on the
design issue first. I came to know that whole problem went into the wrong
direction as compared to what was intended. I think I am working on this
program from last 1 month and it is still not done, Here is the intent:

* * * * *Program will ask the user for input and then will sortthe input
* * * * *alphabetically and will print it on stdout

* * * * *Since I did not want to put any limitation on input size in this
* * * * *program, I have used dynamic memory allocation. My intent in
* * * * *creating and then solving this problem was purely of learning
* * * * *about dynamic memory allocation and C (as defined *by ANSI
* * * * *standard) and nothing else. Regarding storing input *words, since
* * * * *there is no agreed definition of what a word is, *I have taken a
* * * * *very simple approach to it:

* * * * *"Any contiguous collection of characters, except single or
* * * * *multiple *whitespace, is a word"

* * * * *As usual in C, each word is stored as an array of characters. To
* * * * *sort different words we will create another, second array. The
* * * * *elements of this array will be pointing to each input word and
* * * * *since each input word is an array, we will be pointingto a
* * * * *pointer hence this second array will have pointers to pointers as
* * * * *its elements. Then we will sort the pointers to point to
* * * * *different words (arrays). This Efficient technique is described
* * * * *in K&R2, as we are sorting the pointers to arrays, rather than
* * * * *arrays themselves.

So this array of pointers will be created in main() but since we have no
idea of how many words user will enter, we will be realloc()ing in calling
function passing this array of pointers as argument. Or we can make this
array a global array and then pass it as desired without messing with
function arguments. Which one *will be better ?
as a rule of thumb I avoif global data. Pass parameters to suitable
funtions. if the ptr-to-ptr-to-ptr confuses you consider
a struct

struct Word_table
{
int word_count;
char **word_list;
};
--
Nick Keighley

"Never go in against a Sicilian when death is on the line"
Sep 30 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: aredo3604gif | last post by:
On Sun, 10 Apr 2005 19:46:32 GMT, aredo3604gif@yahoo.com wrote: >The user can dynamically enter and change the rule connection between >objects. The rule is a "<" and so given two objects: >a <...
7
by: Foodbank | last post by:
Hi everyone. I'm having trouble with this radix sorting program. I've gotten some of it coded except for the actual sorting :( The book I'm teaching myself with (Data Structures Using C and...
8
by: ianaré | last post by:
Hey all, if i use a os.walk() to append files to a list like so... files = root = self.path.GetValue() # wx.TextCtrl input filter = self.fileType.GetValue().lower() # wx.TextCtrl input...
0
by: sweta kandula | last post by:
hello this is sweta and under grad from india i have some doubt regarding my programming i shall explain you the project in detaillll my project is concerned with DFS and Topological sorting...
2
by: RajasScripts | last post by:
Hi I need some guidance in sorting HTML input tag sorting.I am able to sort columns with out input tag and with dates.But the only problem is I am not able to sort a column which is text box with...
11
by: Trent | last post by:
Running this I see that on first run, both bubble and selection sort have 9 sort counts while insertion sort has ZERO. With a sorted list, should this be ZERO for all? Also bsort and Ssort have...
7
by: abracadabra | last post by:
I am reading an old book - Programming Pearls 2nd edition recently. It says, "Even though the general C++ program uses 50 times the memory and CPU time of the specialized C program, it requires...
1
by: Ahmed Yasser | last post by:
Hi all, i have a problem with the datagridview sorting, the problem is a bit complicated so i hope i can describe in the following steps: 1. i have a datagridview with two columns...
3
KevinADC
by: KevinADC | last post by:
If you are entirely unfamiliar with using Perl to sort data, read the "Sorting Data with Perl - Part One and Two" articles before reading this article. Beginning Perl coders may find this article...
5
by: lemlimlee | last post by:
hello, this is the task i need to do: For this task, you are to develop a Java program that allows a user to search or sort an array of numbers using an algorithm that the user chooses. The...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.