Need Help with a program in C

Re: Need Help with a program in C

Post by msbachman on Thu Oct 28, 2010 6:33 pm
([msg=48273]see Re: Need Help with a program in C[/msg])

@Thetan:

I used that example to illustrate recursion. If that's an example of recursion, it served its point.

Yes, you are right; it is useless, save for explaining recursion!
"I'm going to get into your sister. I'm going to get my hands on your daughter."
~Gatito
User avatar
msbachman
Contributor
Contributor
 
Posts: 685
Joined: Mon Jan 12, 2009 10:22 pm
Location: In the sky lol
Blog: View Blog (0)


Re: Need Help with a program in C

Post by thetan on Thu Oct 28, 2010 8:58 pm
([msg=48277]see Re: Need Help with a program in C[/msg])

msbachman wrote:I used that example to illustrate recursion. If that's an example of recursion, it served its point.

As i said, and i'll repeat, illustrating recursion is inconsequential in such a simple context. Actually, it's counter progress in just about every way possible.

  • Recursion eats up stack space per iteration and depth of iteration is limited by stack space
  • Recursion increases the range of memory accessed, possibly increasing TLB cache misses (decreased program performance)
  • Recursion makes it harder for CPU's to perform accurate branch prediction
  • The repetitive function calls are resource heavy. Modern CPUs can crunch numbers faster then they can push and pull to/from ram, guess what function calls do? yup, they push and pull to/from ram, creating a point of contention

And i'll repeat again K.I.S.S. - Keep It Simple Stupid.

now, if you'd like to post an actually decent post on real world effective uses for recursion i'd support it. However, writing a 5 line code snippet (made further disgusting by lacking format), will get you nothing but rotten tomatoes here.
"If art interprets our dreams, the computer executes them in the guise of programs!" - SICP

Image

“If at first, the idea is not absurd, then there is no hope for it” - Albert Einstein
User avatar
thetan
Contributor
Contributor
 
Posts: 657
Joined: Thu Dec 17, 2009 6:58 pm
Location: Various Bay Area Cities, California
Blog: View Blog (0)


Re: Need Help with a program in C

Post by fashizzlepop on Thu Oct 28, 2010 11:21 pm
([msg=48283]see Re: Need Help with a program in C[/msg])

Watered down Perl:
Code: Select all
$char = <inputChar>; # store input into variable
$num = <inputNum>;

for $i (1..$num){   # for $i with 1 to $num
   print "$char" x $i;   # print $char $i times.
   print "\n";
}


Of course this wont work exactly in C but you can see the thinking.
The glass is neither half-full nor half-empty; it's merely twice as big as it needs to be.
User avatar
fashizzlepop
Developer
Developer
 
Posts: 2304
Joined: Sat May 24, 2008 1:20 pm
Blog: View Blog (0)


Re: Need Help with a program in C

Post by msbachman on Fri Oct 29, 2010 7:11 pm
([msg=48313]see Re: Need Help with a program in C[/msg])

Edit: removed.
"I'm going to get into your sister. I'm going to get my hands on your daughter."
~Gatito
User avatar
msbachman
Contributor
Contributor
 
Posts: 685
Joined: Mon Jan 12, 2009 10:22 pm
Location: In the sky lol
Blog: View Blog (0)


Re: Need Help with a program in C

Post by kujinR on Fri Nov 05, 2010 12:41 am
([msg=48573]see Re: Need Help with a program in C[/msg])

I'm not really as good as these guys are and I have no idea what they're getting at but just wanted to put this out there.

Code: Select all
#include <stdio.h>

int main() {
int n,i,j;
char lett;

printf("Insert a letter -> ");
scanf("%c",&lett);
printf("Insert a number -> ");
scanf("%d",&n);
printf("A Triangle will be designed with the '%c' letter composing it, and of %d lines \n",lett,n);

for(i=1; i<=n; i++) {
   for (j=1; j<=i; j++) {
      printf("%c",lett);
   }
   printf("\n");
}
system("PAUSE");
return 0;
}

@OP(if you're still here) I just edited your original code to get the desired output.
Put simply, the outer for loop moves the output downwards, while the inner for loop moves it sideward.
"Better to keep your mouth shut and be thought a fool than to open it and remove all doubt."
"red = changed"
User avatar
kujinR
Poster
Poster
 
Posts: 270
Joined: Thu Jul 29, 2010 4:39 am
Blog: View Blog (0)


Re: Need Help with a program in C

Post by fashizzlepop on Sun Nov 07, 2010 11:47 pm
([msg=48719]see Re: Need Help with a program in C[/msg])

This sounds like a University problem maybe... you can be charged with plagiarism if you copy that code directly. Even if cite it.
The glass is neither half-full nor half-empty; it's merely twice as big as it needs to be.
User avatar
fashizzlepop
Developer
Developer
 
Posts: 2304
Joined: Sat May 24, 2008 1:20 pm
Blog: View Blog (0)


Re: Need Help with a program in C

Post by fabianhjr on Mon Nov 08, 2010 6:05 pm
([msg=48748]see Re: Need Help with a program in C[/msg])

As a matter of fact I think I saw this challenge in the SKiddie forum "HackForums.net".
Just saying, it is a little suspicious that this thread has more or less the same as the challenge.
Donate bitcoins to me! [1DhRP3hHgmSLQdRTZyT8VPTmzAj7Z2rsGA]
Dunno what bitcoins are? BitcoinMe
fabianhjr
Poster
Poster
 
Posts: 286
Joined: Tue Sep 21, 2010 7:48 pm
Blog: View Blog (0)


Re: Need Help with a program in C

Post by Arameus on Sat Dec 04, 2010 10:05 pm
([msg=50015]see Re: Need Help with a program in C[/msg])

Looks like I'm a little late, but I feel compelled to code things that are problematic, otherwise my subconscience nags me about an unsolved problem, so here is exactly what you are asking for. And I don't feel bad for ruining it for you, since you already either figured it out, or the assignment was due a while ago.

Also, NEVER EVER use scanf. It is extremely exploitable. Don't use gets, either. Use fgets, and sscanf the string into your integer. Also system("Pause") is a sin in C. Here is an article that will explain to you why: http://www.gidnetwork.com/b-61.html

One more thing, if main() doesn't have any parameters, say int main(void). Don't just say int main().

Oh, and finally, don't do printf("\n"). Do puts(""). printf() checks for formatting, variables, etc. If you are just printing out text or a newline, puts() will do just that and nothing else, thus saving (a very small amount of) resources.

Code: Select all
#include <stdio.h>

void getint(int *p)
{
   char intstr[5];
   fgets(intstr, 5, stdin);
   sscanf(intstr, "%d", p);
}

int main(void)
{
   register int i, x;
   
   int timestoprint;
   puts("Input the integer:");
   getint(&timestoprint);
   
   char chartoprint[5];
   puts("Input the character:");
   fgets(chartoprint, 5, stdin);
   puts("");
   
   for (i = 0; i < timestoprint; i++)
   {
      for (x = 0; x <= i; x++)
      {
         printf("%c", chartoprint[0]);
      }
      puts("");
   }
   return 0;
}


-- Sat Dec 04, 2010 7:06 pm --

fabianhjr wrote:As a matter of fact I think I saw this challenge in the SKiddie forum "HackForums.net".
Just saying, it is a little suspicious that this thread has more or less the same as the challenge.


I don't know, this could be a common challenge, like FizzBuzz or something. Besides, who cares if he is from HackForums? If he is trying to learn then he is a friend of ours.
Arameus
New User
New User
 
Posts: 36
Joined: Mon Feb 01, 2010 6:53 pm
Location: Ballston Spa, NY
Blog: View Blog (0)


Re: Need Help with a program in C

Post by fabianhjr on Sat Dec 04, 2010 10:49 pm
([msg=50016]see Re: Need Help with a program in C[/msg])

Well, his OP is legitimate. Just had my suspicious.
Good code eitherway.
Donate bitcoins to me! [1DhRP3hHgmSLQdRTZyT8VPTmzAj7Z2rsGA]
Dunno what bitcoins are? BitcoinMe
fabianhjr
Poster
Poster
 
Posts: 286
Joined: Tue Sep 21, 2010 7:48 pm
Blog: View Blog (0)


Re: Need Help with a program in C

Post by Arameus on Sun Dec 05, 2010 2:56 am
([msg=50021]see Re: Need Help with a program in C[/msg])

fabianhjr wrote:Well, his OP is legitimate. Just had my suspicious.
Good code eitherway.


ooo thanks! Last time I showed C code I wrote to a hacker I was asking for help, and he refused to help me unless I improved the code that worked, because it was so poorly written. Usually I talk about code in generic programming forums, but I know that in places that claim to embrace hacker culture compliments are not given to those who don't deserve them, so I genuinely appreciate it.

Is there a better solution? I tend to somehow automatically find the least efficient algorithm possible for a given problem. Especially when numbers are involved, where I have difficulty doing anything that isn't "Check every possible number that could be right, look at the results in the morning."

It really is too bad buffer overflow exploits exist, from a programmers perspective. Getting integers used to be as simple as saying scanf("%d", &variable); but now we have to be a little more creative. Things like that really show how much C has aged. What do you think of this? If there is one thing that absolutely is NOT the answer to these problems, it is C++.
Arameus
New User
New User
 
Posts: 36
Joined: Mon Feb 01, 2010 6:53 pm
Location: Ballston Spa, NY
Blog: View Blog (0)


PreviousNext

Return to C and C++

Who is online

Users browsing this forum: No registered users and 0 guests