Jump to content
IGNORED

Computer Science Corner


Recommended Posts

  Quote
/******************************************************************************

*

* File Name: class1.c

* © 2010 AED

* Author: ACR

* Revision: 2010/02/25 v2

*

* NAME

* class1

*

* SYNOPSIS

* #include <stdlib.h>

* #include <stdio.h>

*

* DESCRIPTION

* Debuging, menory allocation and free, string manipulation

* Function call passing by reference and by argument

*

* DIAGNOSTICS

* missing includes, variable adressing in show_values, string terminator

* allocation of terminator in strings, array starts, missung argument

* missing free

*****************************************************************************/

 

/* function show_values: illustrates the argument passing by value and by reference */

 

#include <stdlib.h>

#include <stdio.h>

#include <math.h>

#include <string.h>

 

void show_values(int a, int* b, char* str1, char* str2)

{

printf("\nFunction show_values:\nFirst argument of program, \"%s\", has %i characters.\n", str2, *b);

printf("The hidden phrase \"%s\" has %i characters\n", str1, a);

/* change string length to 10000 */

a=10000;

/* change first argument length to square root of 10000.0 */

*b=(int) sqrt(10000.0);

}

 

main(int argc, char ** argv)

{

int i, arg1_len;

 

/* phrase in stringA has 11 characters */

char* stringA = "My secret: ";

/* Phrase in stringB has 21 characters */

char* stringB = "I like AED very much!";

char* string;

/* allocation of string */

string = (char*) malloc ((strlen(stringA)+strlen(stringB))*sizeof(char));

/* function strcpy: copy strings */

strcpy(string, stringA);

/* function strccat: concatenate strings */

strcat(string, stringB);

/* what is the purpose of the next instruction ? Anything missing ? */

string[32] ='0';

/* what is *argv[1]? */

if (*argv[1] == '1') {

/* part 1*/

/* Function strlen: */

/* returns the length (in characters) of a string */

printf("\nFirst argument starts with 1:\n");

for (i=1; i < strlen(string); i++) {

printf("Character %d ---- %c\n", i, string);

}

printf("\n");

} else {

/* part 2 */

i=strlen(string);

arg1_len = strlen(argv[1]);

printf("\nInitial Values:\n");

printf("Length of hidden phrase (i) = %d \n", i);

printf("Length of first argument of the program (arg1_len) : %d \n", arg1_len);

show_values(i, &arg1_len, string, argv[1]);

printf("\nAfter show_values:\n");

printf("Length of hidden phrase (i) (32) = %d \n", i);

printf("Length of first argument of the program (arg1_len) (100): %d \n\n", arg1_len);

}

}

 

 

i've only had 5 hours of class and i'm supposed to correct compile debug and whatnot this, fuck it :facepalm:

  • Replies 109
  • Created
  • Last Reply

Top Posters In This Topic

  On 3/11/2011 at 1:55 AM, inteeliguntdesign said:
  On 3/11/2011 at 12:41 AM, Kcinsu said:

Just learned about interfaces today.

 

I get that the interface is kind of a template for methods you will use... but after you construct all of your methods in your class, why keep referring to the interface?

 

It seems more for developing, than anything practical about implementation.

 

Any insights guys?

 

Imagine it as a recipe for general cake making, or fucking, whatever. There's going to be certain types of cake, like carrot caking, and there's going to be certain types of fucking, like bdsm. So why have this general recipe that specifies the general attributes, but doesn't go into detail? If you're going to either a cake fare, or a sex exposition, you need to be sure what you're bringing is of that general type. In programming, there are various similar situations. Let's say you own a porn site, and you've got a function that takes in a class TrojanHorse. It would be nice to have an interface for its general methods, like init() and fuckThingsUp(), but leave the implementation, like fucking things up for Windows, Mac and Linux, to each implementation. The function that takes the TrojanHorse class would just take the the interface, ITrojanHorse or something, and it would just call the methods init() and fuckThingsUp() all the same, but they would differ depending on the implementation for either Windows, Linux or Mac. Hope that helps.

 

You should seriously write a programming book.

------ dailyambient.com ------

New Ambient Music Every Day.


New ambient album "Sun and Clouds" now out.
Use the discount code watmmer for 50% off the $4 album.
Check it out.

Guest hahathhat

this dude isn't very raunchy, but he did teach me a lot of C many many years ago:

 

http://en.wikipedia.org/wiki/C_for_Dummies

 

hilarious books about programming... amazing feat, really. the best C joke i'd seen before then was the recursion bit in the index of K&R...

 

what are some other good programming books??? that would be a good thing to discuss

Guest inteeliguntdesign

The book that first really helped is the book that the C FAQ (one of them, the main I think) really fucking slates for basically lying to you in pretty much every other sentence. They're not wrong. But it was about the only book that I could really digest at the time. I couldn't understand K&R. It's really terse and the examples are deceptive mindfucks. I had a friend who swore by it. But it was way too much for me.

 

Years later I really studied K&R, did all the exercises. It took about 3-4 months solid effort. It wasn't uncommon for one exercise to take me 3 hours to figure out. But it was worth it. Just the dedication and the experience gained from doing the exercises was worth it. I have a soft spot for C. First language I ever learnt. And I like it simplicity. But I'll probably never use it unless I start doing low level stuff in Linux, or start programming OpenGL or use some library that needs it.

 

Got no book recommendations apart from K&R when you're ready for it, really. Thinking C++ was highly recommended by a friend. I really need a good OO book. I have too much of a background in procedural programming. I'm just really starting to understand how to use OO now, as opposed to understand the concepts taught at uni. I think programming is best learnt by practice. I mean, read a book at your level thoroughly, definitely. But after a while, once you've understood most of the concepts and done some examples, the best way is just to try and make a finished project. Simply to fuck up royally and keep learning from that.

Guest Adjective
  On 3/11/2011 at 9:02 PM, inteeliguntdesign said:

Years later I really studied K&R, did all the exercises. It took about 3-4 months solid effort. It wasn't uncommon for one exercise to take me 3 hours to figure out. But it was worth it. Just the dedication and the experience gained from doing the exercises was worth it. I have a soft spot for C. First language I ever learnt. And I like it simplicity. But I'll probably never use it unless I start doing low level stuff in Linux, or start programming OpenGL or use some library that needs it.

 

Got no book recommendations apart from K&R when you're ready for it, really.

i had just decided to go through K&R a few days ago. it's quite possible i'm not ready but i do enjoy the way it's written and i'm slowly working through the exercises. i really like how it dives right into so many code examples and exercises. glad to read it's worth toughing through

  On 3/11/2011 at 3:32 AM, El_Chemso said:
  On 3/11/2011 at 2:48 AM, Babar said:

ABC:

for (int i=0 ; i<array.length ; i++){… continue ABC; …}

 

 

 

For loops allow you to repeat something a set number of times. The variable i acts as the counter, and ceases to exist once the loop is finished. Every time you make a for loop, the variable (i in this case) is created again.

 

Was that the question?

Edited by Kcinsu
Guest inteeliguntdesign
  On 3/11/2011 at 9:15 PM, Adjective said:
  On 3/11/2011 at 9:02 PM, inteeliguntdesign said:

Years later I really studied K&R, did all the exercises. It took about 3-4 months solid effort. It wasn't uncommon for one exercise to take me 3 hours to figure out. But it was worth it. Just the dedication and the experience gained from doing the exercises was worth it. I have a soft spot for C. First language I ever learnt. And I like it simplicity. But I'll probably never use it unless I start doing low level stuff in Linux, or start programming OpenGL or use some library that needs it.

 

Got no book recommendations apart from K&R when you're ready for it, really.

i had just decided to go through K&R a few days ago. it's quite possible i'm not ready but i do enjoy the way it's written and i'm slowly working through the exercises. i really like how it dives right into so many code examples and exercises. glad to read it's worth toughing through

 

There are few sites with the exercises done - but not too many though, a testament to their difficulty I think. I'd have a look through them once you're done with one. I think I cheated and looked before I'd finished a couple of times, or if I was really unsure what to do (I didn't actually understand the concept of tab-stops before reading that book...). The good thing about K&R is that, after that book, you know all of C pretty thoroughly. Apparently there are only three books you need before you're an expert: http://www.di-mgt.com.au/cprog.html#booksonc The last one is about program design, more than the language from what I briefly remember (only looked at it briefly unfortunately), and the second one just lets you rehearse what you've learnt in K&R, from the looks of it.

Guest hahathhat

hello, i am javascript. print(), you say? have this printer dialogue. would you like to print this shit on your deskjet? no? you're wondering why someone would think that takes priority over, like, printing out a string of text to the console? i don't know these things, man. i'm just javascript

Guest Adjective
  On 3/11/2011 at 9:43 PM, inteeliguntdesign said:
  On 3/11/2011 at 9:15 PM, Adjective said:
  On 3/11/2011 at 9:02 PM, inteeliguntdesign said:

Years later I really studied K&R, did all the exercises. It took about 3-4 months solid effort. It wasn't uncommon for one exercise to take me 3 hours to figure out. But it was worth it. Just the dedication and the experience gained from doing the exercises was worth it. I have a soft spot for C. First language I ever learnt. And I like it simplicity. But I'll probably never use it unless I start doing low level stuff in Linux, or start programming OpenGL or use some library that needs it.

 

Got no book recommendations apart from K&R when you're ready for it, really.

i had just decided to go through K&R a few days ago. it's quite possible i'm not ready but i do enjoy the way it's written and i'm slowly working through the exercises. i really like how it dives right into so many code examples and exercises. glad to read it's worth toughing through

 

There are few sites with the exercises done - but not too many though, a testament to their difficulty I think. I'd have a look through them once you're done with one. I think I cheated and looked before I'd finished a couple of times, or if I was really unsure what to do (I didn't actually understand the concept of tab-stops before reading that book...). The good thing about K&R is that, after that book, you know all of C pretty thoroughly. Apparently there are only three books you need before you're an expert: http://www.di-mgt.com.au/cprog.html#booksonc The last one is about program design, more than the language from what I briefly remember (only looked at it briefly unfortunately), and the second one just lets you rehearse what you've learnt in K&R, from the looks of it.

 

thanks for that link, looks like a lot of good material referenced there.

 

also, when toying around with python in the past i found the problems at http://projecteuler.net to be a great exercise. once you solve the probs though you get to look at how other users solved them in every sort of language, including paper & pencil. i plan on working through those in C at some point.

re: The Microsoft languages: When C# first came out it was just a barefaced Java rip off but since then it has evolved quite nicely, with Generics (superior to the Java implementation of Generics, I'm told) and Linq and other stuff. ASP.NET does indeed suck but ASP.NET MVC is pretty good (a proper model view controller architecture)

 

I actually really like Javascript (and Actionscript I guess, as its based on the same premise). I like the concept of every object basically being an array/hashtable that you can add anything into, and the closures and suchlike. And jQuery is awesome, it really reveals the power of javascript. When you think of all the browsers in the world, Javascript is the worlds most ubiquitous language, so its worth learning.

 

(You have to sort of mentally separate Javascript the language from all the browser DOM inconsistencies that built up around it. They are annoying for sure, but as a pure language, Javascript is pretty neat)

 

About 20 years ago I learnt some ARM Assembly language (for the Acorn Archimedes, the first machine to use an ARM chip). ARM chips are now everywhere - 1 or 2 in every phone or smartphone or tablet, I'm told. I was sitting on the train this evening watching everyone on their phones and figured there was probably about 100 ARM chips in that carriage alone. Weird. ARM assembly language is pretty neat once you get your head around it. the RISC design makes the language really neat and elegant.

Edited by zazen
  • 3 weeks later...
Guest El_Chemso
  On 3/12/2011 at 1:45 AM, zazen said:

About 20 years ago I learnt some ARM Assembly language (for the Acorn Archimedes, the first machine to use an ARM chip). ARM chips are now everywhere - 1 or 2 in every phone or smartphone or tablet, I'm told. I was sitting on the train this evening watching everyone on their phones and figured there was probably about 100 ARM chips in that carriage alone. Weird. ARM assembly language is pretty neat once you get your head around it. the RISC design makes the language really neat and elegant.

 

 

I did ARM last term. Ok once you get your head around it. Most complex thing I could do though was a ceaser cipher. I read that MS had made windows 7 in arm recently, my body shuddered at the idea! I mean how do you do that with only a few registers?!

Guest Babar

I was wondering if it is possible to define new 'blocks' in C-based languages (a 'block' being in my words a structure(!=struct) of the form keyword(argument(s)) {…} )

 

for instance, i'd like to be able to write

 

unless(windowHasMoved) { …blabla…}

and that'd compile just like if(!windowHasMoved).

 

is it possible ? How bad/confusing is it in term of code design ?

you can use blocks in Objective C which is fairly neat. Dunno if you can achieve something like that just using C macros.

 

@interface [...]
- (id) unless: (id) condition executeBlock: (id (^)())block;
@end

@implementation [..]
- (id)unless:(id)condition executeBlock:(id (^)())block
{
   if(condition)
       return block();
   else
       return nil;
}
@end

/* usage: */

[self unless:@"ROFL" executeBlock:^id()
   {
       return @"LOL";
   }];

 

is that what you want?

there might be better places on the web to get answers for such questions ;]

  On 4/1/2011 at 11:09 AM, Babar said:

I was wondering if it is possible to define new 'blocks' in C-based languages (a 'block' being in my words a structure(!=struct) of the form keyword(argument(s)) {…} )

 

for instance, i'd like to be able to write

 

unless(windowHasMoved) { …blabla…}

and that'd compile just like if(!windowHasMoved).

 

is it possible ? How bad/confusing is it in term of code design ?

 

#define unless(X) if(!X)

 

Should work, but I didn't test it. I wouldn't recommend it though. It might be easier for you, but it's not necessarily easier for somebody else who has to read your code.

Yo, my name is Saad and I don't give a fuck.

Guest Babar

@phling

i wasn't talking about blocks but about 'blocks' (for (…), while(…), etc).

 

@ericsosh

works like a charm !

I actually got used to writing 'unless' when i started using Inform7 (that programming language that looks like english). As it's linguistically blind and phrases(functions) can be crafted on the basis of a verb (To decide whether John rocks the house : blablabla) or an adj (To decide whether John is cool : blabla) it can't tell whether the negative form of a phrase relies on an auxiliary verb (if john doesn't rock the house) or a copula (if john is not cool).

So you can write

if not john rocks the house

 

but that's plain ugly and totally out of touch with the language's spirit, so they implemented the unless keyword.

 

Of course it can be confusing if you don't know that 'unless = if(!', but I think it can be useful in situation such as:

 

if(!windowHasNotMoved) vs unless(windowHasNotMoved) (but maybe that's already confusing code design :crazy:)

 

That's something that could be investigated through some kind of cognitive experiment : what's more expansive to the brain ? symbolical negation or linguistical sign-embedded negation ? mhhhh…

perl is a write-only language

  On 5/7/2013 at 11:06 PM, ambermonk said:

I know IDM can be extreme

  On 6/3/2017 at 11:50 PM, ladalaika said:

this sounds like an airplane landing on a minefield

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   1 Member

×
×