User Defined Functions with words instead of intergers
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
You don't need to declare any variables to solve this problem.
This.
Also, Google tells me that the question is from Stephen Prata's "C Primer Plus" book. If you're self-studying from that book, then how could you possibly not know how to define functions that call printf?
Would someone steer me in the right direction with solving this problem ? I see variables declared everywhere but it is always with numbers and not words such as in this problem below
5. Write a program that produces the following output:
Brazil, Russia, India, China
India, China,
Brazil, Russia
Have the program use two user-defined functions in addition to main() : one named
br() that prints “Brazil, Russia” once, and one named ic() that prints “India, China”
once. Let main() take care of any additional printing tasks.
I'll give you the code in BASIC (I still like to write code for the Commodore 64 emulator sometimes ) and you can convert it to C.
Code:
10 A$="Brazil, Russia, India, China"
20 PRINT A$
30 GOSUB 100:GOSUB 200:STOP
99 REM BR() function
100 B$=LEFT$(A$,14)
110 PRINT B$:RETURN
199 REM IC() function
200 B$=MID$(A$,17):GOTO 110
I'll give you the code in BASIC (I still like to write code for the Commodore 64 emulator sometimes ) and you can convert it to C.
Being the first to have fallen into the trap by misinterpreting intentions and attitude, I have also missed the starter's shot for the length contest.
Whatever, here is my contribution.
Code:
#!/usr/bin/env ruby
# idea: Dump country names. You need then a CountryDumper
class CountryDumper
end
# No idea of how to do it... Unimportant. Get one of those
# CountryDumpers already. We think about the rest, later
cd = CountryDumper.new
# This thing should do something... erm... wait. So...
cd.instance_eval do
# print Brasil and Russia.., maybe
def br
p "Brasil, Russia"
end
end
# Cool, quickly ... another one another one...
cd.define_singleton_method :ic, lambda {p "India, China"}
cd.br
cd.ic
Last edited by Michael Uplawski; 03-30-2016 at 02:38 AM.
Reason: Camel case, knotted fingers.
I know you don't want to actually post the answer, but I'm sorry, I just can't resist. Now, let's review the requirements:
Quote:
Write a program that produces the following output:
Brazil, Russia, India, China
India, China,
Brazil, Russia
Have the program use two user-defined functions in addition to main() : one named
br() that prints “Brazil, Russia” once, and one named ic() that prints “India, China”
once. Let main() take care of any additional printing tasks.
AHEM!!
As we talk to people who are new to programming, be picky and be as exact as possible. Include in your reflection all possibilities and think of your code-example as the starting point of all that follows during the next few years.
Well. I am picky.
Make it
Code:
int main(int argc, char** argv)
Last edited by Michael Uplawski; 03-30-2016 at 02:33 AM.
Reason: space, point
int main (void)
{
printf("Brazil,Russia,India,China\n");
br();
ic();
return 0;
}
void br ()
{
printf("India,China\n");
}
void ic ()
{
printf("Brazil,Russia\n");
}
This makes me wonder why you thought the problem was so difficult that you had to seek out help. Is it that you didn't expect the problem to be so simple or is there an aspect of C programming that you are having difficulty with?
BTW wrap your code in CODE tags (the # button on your editing tool bar) for greater clarity.
Here's an alternate Ruby idea which might make the C a little more complicated:
Code:
#!/usr/bin/env ruby
def br(names)
names[0..1]
end
def ic(names)
names[2..3]
end
cities = %w( Brazil Russia India China )
puts cities * ","
puts br(cities) * ","
puts ic(cities) * ","
Would someone steer me in the right direction with solving this problem ? I see variables declared everywhere but it is always with numbers and not words such as in this problem below
5. Write a program that produces the following output:
Brazil, Russia, India, China
India, China,
Brazil, Russia
Have the program use two user-defined functions in addition to main() : one named
br() that prints “Brazil, Russia” once, and one named ic() that prints “India, China”
once. Let main() take care of any additional printing tasks.
----------------------------------
First, consider mapping everything to uppercase to regularize your keywords.
Define an array with your country names. Once you input your array of input keys, a simple first letter mapping will eliminate most of your choices. If there's more than one element surviving in your solution space, proceed with a two-letter search. This is called a bucket search. It can be one of the fastest ways to scan through text keys. Reference by your numeric array index. You don't even have to sort the reference array.
If you try to get fancy and sort the source array, then run a sophisticated numeric search on the keywords, you may find you'e running into a higher computational cost. If, however, you convert the character strings into an array of doubleword strings (two dimensional array), then sort and key off of the doublewords, you have an expensive startup cost with a lower actual search cost. Your selection should be controlled by the size of your original sample space. The more keywords to search through and select from the more efficient a numeric conversion should be due to the use of built-in sorting subroutines. (err, ASM/C assumptions there. Sorry. Working inside the constraints of an interpreted shell language probably won't give you those type of tools, unless you pipe to explicitly written routines.)
Several approaches exist. Bucket sort/search is probably the most useful due to the availability of such tools as grep.
Don't be afraid to play with Unix toys like sed and grep! They're designed for goofy data analysis!
Hardly anyone notices how this is neat. Seriously, I ignored the way that parenthesis work here (or do not). Let me confuse the same “anyone” a bit by my statement that I would have always chosen something like
Code:
cities = %w=Brasil Russia India China=
But the parenthesis, brackets, braces are somewhat cooler.
Cheerio,
Michael
[whistling]
Last edited by Michael Uplawski; 04-01-2016 at 08:31 AM.
Reason: or do not, commarhea
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.