LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-29-2016, 03:38 PM   #16
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,220

Rep: Reputation: 5319Reputation: 5319Reputation: 5319Reputation: 5319Reputation: 5319Reputation: 5319Reputation: 5319Reputation: 5319Reputation: 5319Reputation: 5319Reputation: 5319

Quote:
Originally Posted by ntubski View Post
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?

Last edited by dugan; 03-29-2016 at 04:00 PM.
 
Old 03-29-2016, 10:06 PM   #17
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.1
Posts: 722
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
Quote:
Originally Posted by Minty-One View Post
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
 
Old 03-30-2016, 02:17 AM   #18
Michael Uplawski
Senior Member
 
Registered: Dec 2015
Posts: 1,621
Blog Entries: 40

Rep: Reputation: Disabled
Quote:
Originally Posted by psionl0 View Post
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.
 
Old 03-30-2016, 02:27 AM   #19
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,220

Rep: Reputation: 5319Reputation: 5319Reputation: 5319Reputation: 5319Reputation: 5319Reputation: 5319Reputation: 5319Reputation: 5319Reputation: 5319Reputation: 5319Reputation: 5319
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

Code:
#include <stdio.h>


void br()
{
	printf("Brazil, Russia");
}

void ic()
{
	printf("India, China");
}

int main()
{
	br();
	printf(", ");
	ic();
	printf("\n");
	ic();
	printf("\n");
	br();
	return 0;
}
This is a non-smartass answer. I don't have a problem with posting something this simple.

Last edited by dugan; 03-30-2016 at 02:30 AM.
 
Old 03-30-2016, 02:27 AM   #20
Minty-One
LQ Newbie
 
Registered: Dec 2015
Distribution: Arch Linux
Posts: 12

Original Poster
Rep: Reputation: Disabled
/* hwork5.c */

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

void br ();
void ic ();

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");
}
 
Old 03-30-2016, 02:32 AM   #21
Michael Uplawski
Senior Member
 
Registered: Dec 2015
Posts: 1,621
Blog Entries: 40

Rep: Reputation: Disabled
Quote:
Originally Posted by dugan View Post
* ahem

Code:
int main()
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
 
Old 03-30-2016, 02:39 AM   #22
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,220

Rep: Reputation: 5319Reputation: 5319Reputation: 5319Reputation: 5319Reputation: 5319Reputation: 5319Reputation: 5319Reputation: 5319Reputation: 5319Reputation: 5319Reputation: 5319
Quote:
Originally Posted by Michael Uplawski View Post
As we talk to people who are new to programming, be picky and be as exact as possible.
Leaving out parameters that aren't used is being as exact as possible. I believe most linters agree with me.

Minty-One: I see that you posted your attempt at the same time as me. You seem to have swapped the bodies of the br and ic functions.

Last edited by dugan; 03-30-2016 at 02:44 AM.
 
Old 03-30-2016, 02:43 AM   #23
Michael Uplawski
Senior Member
 
Registered: Dec 2015
Posts: 1,621
Blog Entries: 40

Rep: Reputation: Disabled
Quote:
Originally Posted by dugan View Post
Leaving out parameters that aren't used is being as exact as possible.
Exact for any function declaration.
The main function is special all the same. Good luck, anyway.
 
Old 03-30-2016, 05:50 AM   #24
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,860
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
At some point you should pick one: C or C++
Code:
int main ()     -- that's C++
int main (void) -- that's C
 
Old 03-30-2016, 06:31 AM   #25
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.1
Posts: 722
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
Quote:
Originally Posted by Minty-One View Post
/* hwork5.c */

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

void br ();
void ic ();

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.
 
Old 03-30-2016, 07:30 AM   #26
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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) * ","
 
1 members found this post helpful.
Old 03-31-2016, 07:46 PM   #27
gsears4now
LQ Newbie
 
Registered: Jun 2015
Posts: 3

Rep: Reputation: Disabled
use a text array

Quote:
Originally Posted by Minty-One View Post
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!
 
Old 04-01-2016, 12:41 AM   #28
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,220

Rep: Reputation: 5319Reputation: 5319Reputation: 5319Reputation: 5319Reputation: 5319Reputation: 5319Reputation: 5319Reputation: 5319Reputation: 5319Reputation: 5319Reputation: 5319
Uh, gsears4now, if that was meant as a serious answer then you clearly didn't understand the question.

Last edited by dugan; 04-01-2016 at 01:04 AM.
 
Old 04-01-2016, 08:24 AM   #29
Michael Uplawski
Senior Member
 
Registered: Dec 2015
Posts: 1,621
Blog Entries: 40

Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
%w( Brazil Russia India China )
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
 
  


Reply

Tags
defined, programming language, variables



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
User defined linux commands in a user defined shell Sushma05 Linux - Newbie 3 09-13-2013 07:21 AM
[SOLVED] strange output for printf in C with user defined functions lleb Programming 3 06-16-2013 01:15 AM
[SOLVED] building coreutils... functions already defined frozenQueue Programming 8 09-16-2010 09:24 PM
FYI: Creating User Defined Functions for MySQL mchirico LinuxQuestions.org Member Success Stories 1 06-11-2004 08:51 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:10 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration