Salem 5,265 Posting Sage

One Flew Over the Cuckoo's Nest
The Blues Brothers
Pale Rider
Terminator 2: Judgement Day - make sure you watch the longer "Directors cut"
V for Vendetta - I find this rather too prophetic for the way the UK is heading :(

For a more surreal experience, try these
The Wall
Videodrome

jephthah commented: some really good movies there. never seen "V", though. have to check that out sometime. +10
iamthwee commented: Love the terminator movies! +21
Salem 5,265 Posting Sage

> fact = fact(number);
How does the compiler decide what you mean?

NeoKyrgyz commented: Good point :) +1
csurfer commented: Your post was the limit to this thread buddy nothing could be added ;) +2
tux4life commented: Excellent! +8
Salem 5,265 Posting Sage

> will it crash the program?
Whether it does or not doesn't matter.

Code which tries to do such things is broken. The lack of a crash doesn't make it any less broken (only harder to diagnose and fix).

Salem 5,265 Posting Sage

> Can memory addresses be negative
No - that's just the way you print it.
Use the %p format for printing a pointer.

> 2)is negative subscript of array allowed in C?
No.
Arrays themselves are always 0-based, so there is never a negative subscript.
That's not to say that you can't have say p = &array[5] and then do p[-1]. > i found on net that array[-1] will give the last element of array
Well it's wrong. Got a reference / URL?

> Question is how array[-1],array[-2 ] element are zero......is there ny reason???
Dumb luck.
Like any out of bound memory reference, there's a good chance you'll find "something" there, and zero is as good a value as any other. The result is however meaningless as you can never use that information in any practical way.

Pavan_ commented: Thanks friend +1
Salem 5,265 Posting Sage

> salem, if u are asked to implement a stack then what u prefer?
Did you read my post?

I don't "prefer" any, I use the one which makes most sense in the circumstances.

If you get stuck in some kind of "I always do it this way" mentality, you're going to make some pretty bone-headed design decisions at some point.

jephthah commented: troof +10
Salem 5,265 Posting Sage

> i don't get salem's argument....i ran ur program on gcc and it's working fine.
Because GCC is one of the compilers which supports C99.
So code which mixes declarations and statements is allowed.

Try with
gcc -std=c89 prog.c

tux4life commented: Correct! +8
Salem 5,265 Posting Sage

http://www.catb.org/~esr/faqs/smart-questions.html#beprecise
1 line of random English words with a ? at the end does NOT make it a question.

freelancelote commented: I agree +1
Salem 5,265 Posting Sage

Well it is, but you said this.
> Also im not really good with programs, all i can do is learn to use them

Without the ability to write code kind of 'code' to analyse a received message, and cause some action to be performed, you're not going to get far with anything.

Have a look at the email filters for your email application. Some of them can be used to recognise specific messages, and cause specific things to happen as a result.


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

Salem 5,265 Posting Sage

Start small and compile often - try it.

csurfer commented: Best comment in short !!! ;) +2
Salem 5,265 Posting Sage

There are many things wrong with your code.

But first, a question.
How many lines did you write before pressing "compile"?
If it was more than 10, then read this
It's quite simple, if you only write 10 lines, press compile and get a new compiler error message, you've got a damn good idea where to start looking.

Write 500 lines (what it looks like you tried to do), then you get 100's of error messages, and no clue how to fix them, save for posting the whole sorry mess on a forum for someone else to figure out.

We're not "code rescue rangers". Adopt a better approach to writing programs.

Now the problems.
1. Your main() is over 500 lines long, break it up into functions.
2. You've used a chaotic mix of spaces and tabs to indent your code. Your editor might make sense of it, but trust me, it's a mess when posted on a forum.
3. What you think is the closing brace of main at around line 521 matches the brace at about line 300. You're way off.

Using lots of functions would help, so that you don't end up with a deeply nested brace-fest inside main.

William Hemsworth commented: Nice link. +11
VernonDozier commented: Good advice. +16
Salem 5,265 Posting Sage

<yoda>Do or do not, there is no try</yoda>
Only then, Jedi master of the source will you be, mmmmmm.

Aia commented: Great master of Boolean logic Master Yoda is. +17
Salem 5,265 Posting Sage

> ifstream input("C://Users//Me//Desktop//a.txt");
ONE forward /
or TWO backward \\

> while(true)
With a break statement at the end?
That's just trivial inline code, just delete all the control structure which you're not using.

siddhant3s commented: Good Catch +8
Salem 5,265 Posting Sage

> i meant to say, change it to if (color && 1)
What's the difference between boolean and bitwise ?

tux4life commented: Haha, that's a good one :P !! +7
Salem 5,265 Posting Sage
MosaicFuneral commented: Thanks for the link. +4
Salem 5,265 Posting Sage
Dave Sinkula commented: Heh. The picture/caption in the link is priceless. +19
Salem 5,265 Posting Sage

Ah, the frequently posted "homework disguised as a quiz" ploy huh?

http://cboard.cprogramming.com/c-programming/116393-do-my-homework.html

csurfer commented: Quick Catch Buddy !!! ;) +1
Salem 5,265 Posting Sage
tux4life commented: Good one! +7
Salem 5,265 Posting Sage

> My friend, thanks for the advice, it was a ", yes a "
So post your ACTUAL code, direct from your editor.

Not some abridged version based on how you remember it.

Because we're not going to sit here guessing at what you MIGHT have screwed up just for you to reply "oh, that's fixed in the actual code".

tux4life commented: Right :D +7
Salem 5,265 Posting Sage

Read the manual - it's not used, so passing NULL should work just fine.

tux4life commented: Yes, he should RTFM!! And BTW: Nice link !! +7
Salem 5,265 Posting Sage

http://www.sgi.com/tech/stl/Vector.html
Read the footnotes.

> I cant think of any means to get around this, what should I do?
- Allocate enough space to begin with.
- Choose a different design which doesn't rely on preserving references for such an extended period of time.

> the code I'm working with is huge and it's got vectors left, right and center
Have you considered other data structures?

siddhant3s commented: Good Advices! +7
Salem 5,265 Posting Sage

So are you going to post some code, or do you want to play 20,000 questions while we all guess at what possible mistake you could have made?

Salem 5,265 Posting Sage

Wusses!

You were all to ready to exploit the cheap overseas labour so long as the economic pendulum was swinging in your direction. Now that it's turned, you want to get off the ride?
This should be a challenge and an opportunity, not run away and hide.

Yeah, run and hide under mummy's protectionist skirt for a while until it all blows over. The only country to land a man on the moon, reduced to a shivering wreck. Who knew it would only take 40 years.
Make it happen again and stop wibbling.

Oh, and exactly the same rhetoric is playing out this side of the pond as well. It plays well with "joe the plumber" types, but really doesn't go anywhere to solving the problem.

So if you want your phone bills to double because the call centres are now staffed by overpaid Americans, then go for it. Soon enough you'll end up with another "GM" fiasco, with an over-staffed and under-achieving workforce which is hard to manage properly because "we got a union".

It's the same problem here - too much white-trash illiterati who think they're too good to do some of the menial jobs that need to be done, then get all huffy when "foreigners" come in and take "their" jobs. Newsflash sunshine, if wasn't your job to take, because you weren't doing it to begin with.

Remember, if you're in a job which was "created" by government policy, …

zandiago commented: good points! +6
Salem 5,265 Posting Sage

The current practice of casting malloc stems from people trying to write C code using a C++ compiler.

C++ does NOT allow void* to T* conversions without a cast (which is exactly what a malloc result assignment is).
Otherwise known as bashing the slightly round peg into the slightly square hole.

The solution is to commit to using either C or C++ (figure out how to compile it as C, or use 'new' in place of malloc).

To do otherwise is to have a mish-mash of random syntax called c/c++ which just about does what you want, so long as you don't do anything more complicated than the usual student assignment.

Occasionally unavoidable if you're maintaining something with history. But students writing new code should really be making better use of the tools.

Aia commented: Perpetuation at its best. +16
Salem 5,265 Posting Sage

Do you have a decent definition of "doesn't seem to be working"?

Nothing is printed?
Something is printed?
Your disk has been reformatted?

jephthah commented: thanks for the morning chuckle +9
Salem 5,265 Posting Sage

> Obviously its just part of my code. I'm not sure why, but "if(counter > 8) draw = true;" activates every loop
Is this your actual code, or part of it?
Or just how you "remember" it.

Some points.
1. draw is true on entry, and since there is no draw=false assignment, you're stuck with it being true.

2. A common mis-placed semicolon bug perhaps if(counter > 8)[B];[/B] draw = true; > Secondly... how do you return several values with a function?
> return val1, val2;
> Doesn't seem to work...
No it doesn't.
Pass pointers (or references) to additional locations where you can store additional results.

> And the last question, how do you force an exit out of several loops?
Quickly, use goto

Or use a state variable

bool Once = true;
for( ; cond && !Once ; ) {
	for( ; cond && !Once ;) {
		if(SomeVar1 == true) {
			Once = true;
			cout << "cakes!"
				break;
		}
	}
}
tux4life commented: I didn't think of pointers and references, but it's actually a very good, simple and effectieve solution :) +5
Salem 5,265 Posting Sage
Salem 5,265 Posting Sage

"me too" had better read the forum rules as well.
http://www.daniweb.com/forums/announcement125-2.html

If you can't be arsed to put in any more effort than bumping your own message after an hour, then just drop the course and stop pretending that you'll ever make it to the end.

Salem 5,265 Posting Sage

Why don't you just post your whole exam, since you seem to know a lot about what questions ARE going to be on it.
http://www.daniweb.com/forums/post870168-3.html

Better yet, just give us the email address of the examiner, and we can forward the answers directly, and cut out the middle-man.

> Please can someone tell me the difference between a struct and an enumeration?
YOU tell US what you think the answer should be, then it makes it much less likely for us to consider you a sponge.

Salem 5,265 Posting Sage

As if to prove a point, the very first system I tried it on, the results conflict with the assertion.

Some test code, to format and print 1E7 integers to a text file, then read them back in again.

Test C program

#include <stdio.h>
#define MAX_R   10000
#define MAX_C   1000
int main ( ) {
#if DO_WRITE
    FILE *fp = fopen("test_c.txt","w");
    int r, c, prod;
    long long int sum = 0;
    for ( r = 0 ; r < MAX_R ; r++ ) {
        for ( c = 0 ; c < MAX_C; c++ ) {
            prod = r * c;
            fprintf( fp, "%d ", prod );
            sum += 1;
        }
        fprintf( fp, "\n");
    }
    fclose(fp);
    printf("Last prod value =%d\n", prod );
    printf("Number of values=%lld\n", sum );
#else
    FILE *fp = fopen("test_c.txt","r");
    int prod;
    long long int sum = 0;
    while ( fscanf( fp, "%d", &prod ) == 1 ) {
        sum += 1;
    }
    fclose(fp);
    printf("Last prod value =%d\n", prod );
    printf("Number of values=%lld\n", sum );
#endif
    return 0;
}

Test C++ program

#include <iostream>
#include <fstream>
using namespace std;
const int MAX_R = 10000;
const int MAX_C = 1000;
int main ( ) {
    std::ios_base::sync_with_stdio(false);
#if DO_WRITE
    ofstream op("test_cpp.txt");
    int r, c, prod;
    long long int sum = 0;
    for ( r = 0 ; r < MAX_R ; r++ ) {
        for ( c = 0 ; c < MAX_C; c++ ) {
            prod = r * c;
            op << prod << " …
siddhant3s commented: Agreed. Specially the last remark about debugging. +6
Salem 5,265 Posting Sage

You have threads, and a single unguarded global variable.

You have two functions with very similar names,
- sockConnect
- connectSock

Your 'state' global variable has two flags which seem to mean the same thing.
- bool ic;
- bool inuse;

Can you do what you want (once) as a single thread?

Because at the moment, it's a confused mess of half-cooked ideas all milling around as a bunch of race conditions.

Salem 5,265 Posting Sage
~s.o.s~ commented: +rep for enlightmentening him; that essay is a must read +28
Salem 5,265 Posting Sage

So, over a period of a couple of years, I do
- upgrade the RAM
- replace the hard disk
- replace the graphics card
- replace the NIC
- replace the motherboard and processor.

Do I still have the same machine?

Overly elaborate "machine locked" software only really serves to piss off genuine customers who paid for it, only to be met with abuse from the s/w vendor when they've "upgraded" the wrong thing.
The last thing you need is paying customers taking their business somewhere else!

People who pirate your s/w were going to pirate it anyway. Any s/w which is vaguely useful will be cracked (and with the internet, can be found by anyone willing to look).

You might be better served by spending your time on improving the features, and removing the bugs, so that more genuine people would want to buy it.
As opposed to spending time and money on people who were never going to give you any money to begin with.

"Nice security system, shame about the program" is all to common.

Or change your business model so you give the s/w away and you sell service to those that want it. The program in effect becomes an interactive advert for what you're really selling, and which is a lot harder to rip-off.

tux4life commented: Wow, I just buy a new computer then :) +4
Salem 5,265 Posting Sage

> suppose there were another planet similar to earth in composition, temperature, and atmosphere.
The atmosphere of primordial earth was not one you could live in.
There was no free oxygen, and a bunch of other stuff which would not help either.

Nor does life have to restrict itself to the surface.
http://en.wikipedia.org/wiki/Hydrothermal_vent
Of which the current favourite outside of earth seems to be
http://en.wikipedia.org/wiki/Europa_(moon)

Technological civilisations may indeed be rare, possibly even unique within say a single galaxy. One of the big hurdles being the maths needed to understand how radio works isn't that far removed from that needed to work out how nuclear fission works.
"Hey, we can talk to the universe - poof, and there was silence once more".

But my guess is microbial life is endemic.
http://en.wikipedia.org/wiki/Extremophile
Basic life can get a foothold in all sorts of places.
Liquid water + a bag of chemicals + an energy source (not necessarily a star) + time, and you're on your way.

Salem 5,265 Posting Sage

> Cin Keyboard inpout to const char*
You want to "INPUT" to something already declared as const - doesn't that strike you as being in the least bit odd?

You say "no changes allowed", then try and change it?


Lesson number 1

#include <iostream>
#include <string.h>
#include "holding.h"
#include <stdio.h>

Decide whether you're a C programmer or a C++ programmer.
C/C++ is like walking down the median line of two lines of traffic moving in opposite directions. One small slip-up, and you're road-kill.
Life is much easier if you're in the flow of one or the other.

Lesson number 2
Having decided that C++ is the way to go, FORGET all about things like "char *" and use a std::string instead.
When you absolutely must have a "char*" for some historic interface, then use the c_str() method of a proper C++ string to get it.

Lesson 3
Lose all those global variables.

Lesson 4
Lose the void main, make it int

tux4life commented: Right, nice posting :) +4
Salem 5,265 Posting Sage
ahihihi... commented: nice game +0
Salem 5,265 Posting Sage

Read the rules, we don't give you code just because you managed to cobble together some buzz-words in the hope that it looks like effort.

Salem 5,265 Posting Sage

Most people leave it until there's less than 12 hours to go before running round screaming URGENT (which it isn't by the way)
http://www.catb.org/~esr/faqs/smart-questions.html#urgent

4 days is plenty of time for you to make a stab at solving these trivial problems.

Besides, you've got over 70 posts, which means you REALLY ought to be familiar with the rules by now.
http://www.daniweb.com/forums/announcement4-2.html

thoughtcoder commented: good point +1
Salem 5,265 Posting Sage

Checking for == 6 (the actual number of conversions you expect for success) would be better than simply >0

Reading a line using fgets(), then parsing the result with sscanf() (or something better) would help as well.
Partial fails of fscanf leave the input stream in a horrible mess.

Ancient Dragon commented: good points :) +36
Salem 5,265 Posting Sage
tux4life commented: Nice one! +4
Salem 5,265 Posting Sage

-R, --recursive
change files and directories recursively

http://www.rt.com/man/chmod.1.html

Salem 5,265 Posting Sage

Or Psychopath?


Lots more here. http://www.telegraph.co.uk/travel/picturegalleries/5278200/Sign-language-week-48.html

Made me laugh, in the midst of despair with all the greedy trough feeding SCUM
http://www.telegraph.co.uk/news/newstopics/mps-expenses/5298065/MPs-expenses-Four-ministers-who-milked-the-system.html
http://www.telegraph.co.uk/news/newstopics/mps-expenses/5292959/MPs-expenses-Ten-extraordinary-claims.html
And so on ad nauseum.

These fuckers should be spending all their energies on running the country (instead, just ruining it), not running around finding all the ways to milk as much cash as they can before they get their arses kicked out of office.

Mother of parliaments? Yeah right!
Must have been unwed, given all the bastards which are running around.
Like for 'bankers', the collective noun for this lot is a "wunch".

Aia commented: I wish some serious thought were given to it by more people. +17
Salem 5,265 Posting Sage

> printf (astr)
NEVER do this!
http://en.wikipedia.org/wiki/Format_string_vulnerabilities

The format string of printf/scanf must always be under your control, ideally in read-only memory.

All someone would have to do with your code is feed it some lines with % chars, and watch the fun begin!

jephthah commented: good point. +9
Aia commented: Be warned! +16
Salem 5,265 Posting Sage

52040292466647269602037015248896
That's the size of your last file - are you sure you want to do this?

Y'know, some simple maths to determine whether your plan will actually yield a result in lets say the age of the universe?

iamthwee commented: right on! +19
Nick Evan commented: Haha, great advice +17
Salem 5,265 Posting Sage

> This poll will close on 17th Sep 2010 at 16:22
Expecting something to happen then?

> Massive solar flare to blow off earth's atmosphere
Hasn't happened in 3+Bn years, so it seems unlikely to happen in the next week or two.
I doubt the sun has the means to generate the kind of flair which would cause a problem. There are well documented hits from very massive CMEs, and we're still here.

> Far away giant gamma ray burst
Far away ones aren't the problem, they happen every few days.
It's the close up ones which are the problem!

> Swine-flu or bird-flu
No disease yet has had a 100% fatality rate.
It might be a very bad day, but so long as some farmers survive, it won't be curtains for everyone.

Remember, viruses invented jet aircraft to propagate themselves more effectively ;)

> Other (explain)
Most large (to be a big problem) NEO's have been catalogued and are of no immediate cause for concern.
A big comet from the Oort cloud with minimal (or zero) warning seems the most likely contender for true extinction.
Supervolcanoes like Yellowstone would devastate a continent and trash the atmosphere to the point only a few would survive.

Salem 5,265 Posting Sage

I thought jelo-C was a new programming language in a variety of fruity flavours ;)

Ancient Dragon commented: LOL :) +36
scru commented: JeloC ++ +6
John A commented: This. +21
Salem 5,265 Posting Sage

> I'm currently using the new borland so this is a correct syntax
> after all it's working on my computer,
You have no idea how much grief you're going to get if you keep up with the "it works on this compiler" approach.

If for no other reason, when you change to another compiler, all the crap you've learnt for the old compiler will no longer work on the new one.

If you're going to the trouble of learning C++, then try to learn standard C++ (which will work everywhere), and not "dialect-C++" which only works in one place.


> as for the variables i am on writing description for everyone of them.
But if you wrote more meaningful names to begin with, you wouldn't need to write a description. int ns; // number of students vs. int numberOfStudents; Which by the way would be obvious in all the places it was USED as well, not just where it was declared.
When your 'descriptive' comment scrolls off the top of the page, 'ns' once again becomes meaningless.

> hope it's clear now
Actually, I couldn't give a toss. It was posted without code tags so I just ignored it.

tux4life commented: Agreed :) +3
Salem 5,265 Posting Sage

> am using turbo c 3.00
Why?
It's 20+ years out of date, and my guess is you're running XP or something as your main OS.

Congratulations, you've turned your Ferrari into a horse and cart.

Get code::blocks or visual studio express (details in a search engine near you).

Oh, and re-read ALL the forum rules until you figure out how to use code tags. It's only mentioned half a dozen times, how did you manage to miss all of them.

jephthah commented: aha. Borland 3.0: preferred compiler of Amish Colonies everywhere. +8
Salem 5,265 Posting Sage

It's your call, but the technique suffers from taking an increasing amount of time to complete as most of the numbers have already been guessed.
You could be waiting a while for the last iteration to finish, when there's only one number left to guess.

Ezzaral commented: Good point. +21
Salem 5,265 Posting Sage

> It's a 2-D array, so it's more like an int**, not an int*.
Only in as much as you can use [x][y] notation to step through them.
A pointer to a 2D array is NOT the same as a pointer to a pointer.
http://c-faq.com/aryptr/pass2dary.html

An example of actual pointers to 2D arrays, being passed and returned.

#include <stdio.h>

/* the syntax for returning a pointer to a 2D array is hairy! */
int (*hard( int (*a)[20]) )[20] {
    static int result[10][20];
    int r, c;
    for ( r = 0 ; r < 10 ; r++ ) {
        for ( c = 0 ; c < 20 ; c++ ) {
            result[r][c] = a[r][c];
        }
    }
    return result;
}

/* a suitable typedef makes it so much easier on the eye */
/* The typedef adds nothing NEW here, hard() and easy() are fully */
/* interchangeable when it comes to using them */
typedef int (*twod)[20];
twod easy( twod a ) {
    static int result[10][20];
    int r, c;
    for ( r = 0 ; r < 10 ; r++ ) {
        for ( c = 0 ; c < 20 ; c++ ) {
            result[r][c] = a[r][c];
        }
    }
    return result;
}

/* Yet another equivalent way of declaring the input array parameter */
void fill ( int a[10][20] ) {
    int r, c;
    for ( r = 0 ; r < 10 ; r++ ) {
        for ( c = 0 ; …
VernonDozier commented: Good link. +15
Dave Sinkula commented: Hairy indeed. +18
NicAx64 commented: Thanks man but something more to read on this will improve the underestandibility of everyone who dig this in future +1
Salem 5,265 Posting Sage

http://www.dban.org/
This does two things:
1. It removes every last trace of the old installation.
2. It gives the disk a really good work-out.

I had the same trouble as you some time back (but with 2K IIRC). Eventually, I figured out that there were a few bad sectors towards the middle of the disk.

tux4life commented: You're always making extremely good posts! +3
airbourne commented: Loved the links. Trying them out now on my PC at home. :) +2