i don't get it.
is this a question or an order?
i don't get it.
is this a question or an order?
hello guys can you give me...
now that the thread has been moved, the lesson is : DON'T hijack someone else's thread with some completely unrelated question of your own.
but please DO rephrase your question in a coherent manner so the rest of us might understand what you're talking about.
.
Funny thing, Walt, i've noticed your own recent posts where you do exactly the same thing.
such as this one here, where your only response to some guy's crap code was to tell him that it's full of problems and he needs to refrain from posting.
Please ignore yila's code. It is full of problems.
yila, please refrain from posting (supposedly) working code for people. They learn nothing so you are hurting more than helping. Especially when you use bad coding practices.
No explanation of why it was bad. Not even an explanation of *what* was wrong.
Now I wouldn't ever call you out on it because i totally agree with you. I'm tired of explaining the same things over and over to every noob and his brother. Crap code is crap code, and being nice and patient about it doesn't change the fact that most of them aren't listening anyhow except long enough to get the quickest and easiest answer.
And as for credibility ... that's a non issue. Veteran posters like yourself with thousands of posts and hundreds of "solved threads" don't NEED to establish credibility. At least not for newbie posters asking basic "why doesn't my program work?????" questions.
.
the recursive stack is because they're trying to teach you a concept, not just so you can 'get it done'
you definitely should do it the way that's required even if another way seems easier.
i was going to look at this myself, but i'm at work and don't have time right now to sit down and debug it.
The ** was a typo
oh, i see. well, the double asterisk operator (**) is in some languages the "Power" operator. so that cubeVal = i ** 3
would be a valid expression for some languages, though not in C.
I forget where that's used, maybe in BASIC. that's what i thought you might be trying to do.
We're not allowed to use the math library as we haven't done it...
yeah, i figured as much. that's why i prefaced it with the future-conditional statement "in the real world you would _____"
it's common practice that beginning comp sci classes force you to do things the "long" way so you understand the mechanics before they let you take off and use standard libraries.
anywho, if you follow my pseudo code, you should have this done in no time.
.
i see part of your problem... you can't use a double asterisk ** like you think you can. and even if you could, they way you're trying to do it would be wrong.
it's simply this:
'i' cubed = i*i*i
'i' to the fourth is i*i*i*i
but in the real world you'd use the <math.h> library and do this:
iCubed = pow(i,3);
iToTheFourth = pow(i,4);
[pseudocode]
initialize sumOfCubes = 0.0
for a = 1 to 3
{
get float input from user
store in inputValue
initialize cubedValue = 1.0
for b = 1 to 3
{
cubedValue = cubedValue * inputValue
}
print "the cube of " inputValue " is " cubedValue "."
sumOfCubes = sumOfCubes + cubedValue
}
print "the sum of the cubes is " sumOfCubes "."
[/pseudocode]
.
well it works
that's what Toyota said about their millions of recalled cars.
but still, i'd like you to tell me what's wrong with it.
here's why your code is bad. (in your defense, i will say i have seen more bad code crammed into such a short space.)
(1) void main()
(2) printing console instructions prior to a user input without flushing the output buffer either with a \newline or a fflush(stdout) call
(3) using scanf() to get user input from the console.
all of these are basic examples of bad coding that causes no end of real-world program failures. This has been explained here and elsewhere ad nausuem, and i am not going to spoonfeed you answers to questions that have been addressed hundreds of times. If you really want to know why, look it up
seriously, guys, this forum is not what it is used to be.
lol, really, in all your 9 months and 17 posts? you crusty old veteran, you.
on y!answers, you actually happen to find some guys with knowledge, but here i am utterly disappointed
well, as my grandpa always said, "Don't let the door hit you in the ass on your way out."
.
drewangel : i am using turbo c++ compiler and including the header file pthread.h , but i am getting error and it is not getting included
AncientDragon : Your compiler does not support threads. You will have to use a modern compiler '
drewangel : thanks for your reply. but i want to know the synatx for pthreads. are we supposed to use pointer to functions in that?
my goodness. Reading comprehension, much?
Turbo C does not support multithreading. you will not get a POSIX thread library to work. What part of this are you not able to understand?
.
not trying to stir up any controversy, but has anyone heard from Tom Gunn?
wait, wait...
your code prints a full pyramid.
3= *
***
*****
which is way more complicated than printing a half pyramid.
3= *
**
***
man, how did you figure out a full pyramid but not be able to get the much simpler half pyramid?
read what walt said. that's the answer, it doesnt get any simpler.
okay, good luck.
here's a hint. you can use the function strstr, in a manner similar to this:
result_ptr = strstr(registrationString,":")
printf("the registration number is %s\n",(result_ptr + 1));
EDIT: I just noticed your other thread. People here (including me) are very cautious about people coming here wanting work done for them. I dont think you're really trying that, and even if you were i'm not doing your homework with this answer. my code snippet is not a suitable solution, but only illustrates the concept.
i could have pointed you to an entry for "strstr()" function at any C library reference site, but i think we want daniweb to be a little bit more than just a redirection service.
at any rate, the example i provided isnt much different than one found in any reference.
.
Pls can u guide me hw cud i b a good programmer?
... sent from uday's Blackberry Wireless.
I made the map ... in runtime and use a variable for it that will be constantly updating.
I don't need scorring or anything else just a moveing snake and another moving snake since the aim of the assignment is the multiplayer part.
I also have to mention if I didn't mention that the game is running on a surver so i am connecting myself using Putty and a friend of mine is using putty once we are on the machine we need to run the same process and play with the keyboard of each other not multiplayer on one keyboard.
And I think that requres some additional knoledge of unix architecture and system calls that I don't know from where to aquite. Thanks
okay, this is actually even simpler than i originally thought. you can skip step #6, above, since as you say there's really no additional work for interacting or scoring or anything.
you use PuTTY to access your unix account. it's just a remote terminal. you could be onsite and use a terminal at a workstation. the point is that you'll be running a console application in a terminal, and whether its via PuTTY or directly, its all the same.
One thing i wasnt clear about is that you keep (store) the map array and related state information as a FILE (like a binary .bin or .dat file) in a common location. not in the memory of the program. all the program will do is …
@nezachem : nobody cares which martial arts you claim to study. "grasshopper" is a frequent term applied to a new learner in any subject. thats it. if you dont understand english language colloquialisms, move along and don't waste time requiring someone to explain trivial shit.
as for this piece of "advice"
Start simple. Create a server, which holds a [blah blah blah ... snip]
do you really think that someone who cant even begin coding on a simple game program partly because they don't understand the common architecture of their unix system, is going to build a server? he cant use sockets anyhow.
@iliali : look, 90% of this becomes relatively simple, if you back up and quit trying to look at this a whole problem. look at it as a series of little problems.
you need to do the following one step at a time.
(1) build an array to hold a map
(2) build structure(s) to define elements of what that map contains.
(3) define an interface to "move" a "piece" around on that map,
(4) keep track of where the piece is in your structure
(5) ensure that your "piece" cant move illegally based on locations of other objects.
move through this incrementally. build a little bit, test it. Build a little bit more, test it. integrate these two things, test it. and so on.
(6) then you add in a second player, and modify …
crappy (krap' e)
- adjective, slang.
(1) difficult to implement without requiring an excessive amount of overhead to prevent it from falling apart during normal field use.
(2) anything that exists alongside a freely available alternative from the standard library that covers most use scenarios in a manner resistant to typical input errors and with a minimal of overhead.
(3) any function that is routinely handed to noob programmers who don't grasp its complexities, who invariably use it to write poor programs and then stream through programming forums in a non-stop "why doesn't my program work" parade.
(4) the state of being crappy.
(5) scanf()
.
brilliant first post, osc.
tutorials in C and I seem to grasp some of the ideas of the language but as I learned it is a very messy language.
C is a very well-defined and highly structured language, probably one of the most structured languages you'll find. there's nothing messy about it. Perhaps your tutorial writers are sloppy programmers.
Now my problem is that I am trying to develop [a lot of stuff] so if someone can give an insight with this I will be happy.
bite-sized pieces, grasshopper. do not try and build Rome in one day.
first write your program so that you can just have a single player move your snake around the map.
then once you get that working, have a second player added in a single thread where you take turns, so you can get an idea of how scoring will work and determining a winner.
then use multithreading and put the common components (map, etc) in a shared unix directory.
I ahve no code yet since I am designing but once I am clear I will start coding. Thanks again for any help.
and i have no more help yet since you are not coding, but once you are coding i will start helping. for now, here's a starting point.
// include files here
#include <stdio.h>
#include <stdlib.h>
// define statements and macros here
// global variables here
// function prototypes here
int main (void)
{
// local variables here
// begin main code …
okay then, my first post was correct. that is indeed what he's doing: flipping the bits.
notice the (unsigned int) - 1
.... the value of this is an "unsigned -1", aka 0xFFFFFFFF, but it's confusing because he has a space between the negative sign and the one. ie, "- 1"
instead of "-1"
.
this is not technically incorrect because the compiler ignores the whitespace in this case, but it makes for a confusing read because it makes you think of a subtraction operation -- which it is not. i believe that whitespace is an unintended typo that happened to not get noticed because it didn't affect the program execution.
its why i thought maybe it was a flawed attempt of 2's complement negation, and probably why it was confusing to you also.
.
the more i look at it though, the more i'm wondering if it's not an incorrectly rendered attempt to make a negative number using 2's complement arithmetic.
because if it were written ( n ^= (unsigned) -1 ) - 1
(note parentheses) this would be the right hand side (RHS) of an equation that takes the value 'n', inverts it by XORing with 0xFFFFFFFF, then subtracts one ... which gives you the 2's complement binary representation of the original value 'n' made negative. (of course, you'd still have to assign that RHS result to a variable on the LHS for it to be a valid assignment)
so for your example, you have n= 10 which is 0x0000000A
XOR that with 0xFFFFFFFF
gives the result 0xFFFFFFF5
which can either be seen as decimal 4294967285 (unsigned) or -11 (signed). Note that in traditional microprocessor arithmetic, you use registers that dont specify signed or unsigned, it's up to the programmer to interpret it and use it correctly
finally, remember that 2's complement requires you first invert the bits, then subtract one.
so 0xFFFFFFF5 minus 0x00000001 = 0xFFFFFFF4
which is the 2's complement value for -10
.
n is XOR'ed with an unsigned -1, the resulting value is stored in 'n'
-1 is treated as an unsigned integer, meaning just that it's a variable with all bits are ones. a 32-bit int, will be 0xFFFFFFFF
basically this is like a microcontroller register operation that "flips" all the bits in a register.
i.e., the value 0xA5A5A5A5 when XORed with 0xFFFFFFFF becomes 0x5A5A5A5A
though jephthah comes tromping into your thread being condescending and belligerent, spouting derisions and opinions with no backing explanations, he is correct. He feels you are just supposed to trust him
because i've explained the "why's" at length a number of times previously. i've grown tired of doing it.
usually i just ignore it now, mostly, until i see a triple-header like this one.
it's nice that you still have patience. srsly, you'll probably live longer than me.
i would like to make an observation.
obscure variable names, cryptic logic, and peculiar bracket indentations do not lend your code to being easily read by the casual passer-by who might be inclined to scan your code for obvious or hidden errors.
this might also explain why for what's otherwise a trivial parsing exercise, it's been three days with zero comments.
although, with a little work, you might just have an entry for the next Obfuscated Code Contest
if you can just remember one thing: any hacker can type out some code and make it look complicated. a good programmer writes *readable* code and makes it look easy.
ugh.
what is this some cult groupie thing?
sorry, i should have anticipated your response.
this is *not* related to CR/CR+LF conversion problems between OSes. I know you think it looks like it is, but trust me on this.
the last line of certain CSV files truly does not have a newline. the newline was not put there because the DLL did not put it there when creating the file.
and we can not go and change the raw .CSV file after the fact, whether manipulating characters or adding newlines.... this is a strict federally-regulated environment. you don't want to go there.
My problem remains, that the progress database programmer says he cant read a line that doesnt end in a newline. i think that's not entirely accurate.
i'm wondering if anyone has any idea why he might say such a thing.
i had just edited to say that it's unix system. so i should say that the newline search is just looking for the CR, although the files that are being sent are created in windows systems so they have CR/LF
i do not know what script it is using. i can try to find out, they're in a completely different department.
i know this is about MySQL, but there is no 4GL forum, so i'm hoping one of you experts might know:
we have an application that uses Progress (on a unix system) that imports data from .csv files and writes the various fields to the database.
one particular type of .csv file does not have a newline (CR) at the end of its last line, and this last line is therefore being ignored by the importer application.
the consultant who programs the this application (via a script, i do not know which), has told us that every line *must* have a carriage return in order to be read, that if it doesnt have a carriage return it cant be read.
I find this hard to believe. I have fairly strong knowledge of C and variant languages, and can't imagine such a imposed restriction. the problem is that to change the application which writes the .csv file prior to database import will be painful due to manufacturing and regulatory reasons.
can you give me some insight on this?
thanks!
.
It was written/run once and appeared to work...that's quickly in my books.
what constitutes "appearing to work" for you? not having the computer crash? if so, then yeah, it "appeared to work".
other than that, it was full of compiler warnings and gives nonsensical output.
Boo.
When you have a function and need to scan for 2 different numbers, should the scanf be in the function or in the main() and why?
question is flawed. you shouldnt use scanf for user input. use fgets() in conjunction with sscanf() or atol() or atof() or something similar.
see also: http://www.daniweb.com/tutorials/tutorial45806.html
and yes, you can get user input whereever you want to. depends on how you plan to use it what the most sensible location for it. if this is confusing to you, then just stick it in main() until you learn more about how subroutines work.
.
scanf() is hard to use at the best of times.
yeah, that's usually my first answer. scanf sucks for user input.
i would recommend using fgets() in combination with functions such as sscanf(), strstr(), atol(), atof(), strtol(), etc.... i could go on, but Dave Sinkula already wrote an excellent tutorial on getting user input here: http://www.daniweb.com/tutorials/tutorial45806.html
.
instead of using "scanf(...."
use the two lines that i showed you.
the first one (fgets) gets the input... it takes input from the standard input (stdin, typically the keyboard), looks at the first "MAX_LEN_INPUT-1" characters, and stores them in the character string "strInput"
the second line converts the string to an integer.
you could also do floatVal = atof(strInput);
to get a floating point input.
(you will need to define MAX_LEN_INPUT as some value...
okay look, try this and see what i mean:
#include <stdio.h>
#include <stdlib.h>
#define MAX_LEN_INPUT 30
int main (void)
{
char strInput[MAX_LEN_INPUT];
int value_i;
float value_f;
printf("enter an integer value:\n");
fgets(strInput, MAX_LEN_INPUT-1, stdin);
value_i = atoi(strInput);
printf("you entered: %d\n",value_i);
printf("enter a floating point value:\n");
fgets(strInput, MAX_LEN_INPUT-1, stdin);
value_f = atof(strInput);
printf("you entered: %f\n",value_f);
return 0;
}
im just typing this out freehand, this is not bulletproof code by any means, but is a good place for a beginner to start with concepts.
Dear Bantex
I'm sorry, i was overly harsh and mean to a beginner. it's late where i am, and i should go to sleep.
but seriously, your teacher needs a board upside their head. and anyone involved in your educational system. they are failing you. look up what im talking about, if you don't believe me. it's commonly known in any technically proficient arena.
i mean it. whoever is teaching you these habits is an idiot. get away from these bad habits now, or you will be doomed to a career in tech support call centers.
oh. my. god.
get rid of that "gets" statement right now. replace it with "fgets"
and change your "void main" to "int main(void)" while youre at it, since that is sloppy practice.
but seriously, "gets" is some effed up shiite. never ever use it.
ever.
and, really, "conio.h"? "clrscr()"?? getch()???
where the hell are you getting your instruction from? your teacher needs to be hit upside the head with a board.
oh, let me guess: India. right?
.
scanf is a notoriously difficult function to use, especially for beginners.
try using fgets in conjunction with atoi instead.
char strInput[MAX_LEN_INPUT];
int value;
fgets(strInput, MAX_LEN_INPUT-1, stdin);
value = atoi(strInput);
once you figure out this basic method, use strtol instead of atoi, and you can make a pretty bulletproof input routine
.
re: allocating your array
matrix = (int *) malloc(nrows * sizeof(int));
if(matrix == (int *) NULL)
what is your "nrows"?? its uninitialized. you need to specify what the size of the array you want to malloc.
note, the size of a single int is either 4 or 8 bytes depending on your compiler. but don't worry about that, what you need to worry about is malloc-ing enough ints to account for the number of rows AND the length of each row.
so not only will you need "nrows" to be the correct value, but you need the size of each row... think "ncols" or "rowlength"
a couple points while im here...
don't cast your malloc as an (int *) that's unnecessary and potentially buggy. it dates from the days before ANSI C and is not good practice.
and dont cast NULL as an (int *) either. NULL is already defined just fine, in the standard library.
as for "nrows" this is the number of rows that you should have already read from your text file before you allocate your matrix. which you havent
so make a while block that continues to get one line at a time as long as the result is not null. use "fgets"
see: http://www.cplusplus.com/reference/clibrary/cstdio/fgets/
consider this example:
char singleLine[MAXLINELEN];
nrows = 0;
while (fgets(singleLine, MAXLINELEN-1, infile) != NULL)
{
nrows++;
printf(line #%d: %s\n",nrows,singleLine);
}
how can you apply it to your code? …
okay, that's a start. what's your question?
so what's your question? and what have you done so far?
go ahead and show us what you've got. here's a handy template:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
// paste code here
return 0;
}
oh, wait? you want hints on how to start? okay, here are the basic functions, in roughly the order you'll want to use them. give or take...
malloc
fopen
while
fgets
if / else
realloc
fclose
free
there's probably a few more, but don't say i didn't give you anything.
.
well... your question is kind of hard to follow but, i think your confusion might be in your while() statement.
for instance: if k=1
, and then you say while (k-- > 0)
the statement k-- > 0
is true, because k post-decremented is evaluated while k=1
but then the post decrement occurs and the rest of the block is evaluated while k=0
i shall post in here now, so i can remember to visit it later, when i am "leisurely bored" :)
OMG
It was a stupide question
Thank you for answering! ;)
i recall some recent snafu at NASA causing a significant failure, and being traced to a divide-by-zero error. (or, maybe i dreamed it. i'm not sure now...) but anyhow it's not a stupid question. it's a good lesson.
For instance, I just had to come into work all day sunday to revalidate a production system revision that failed because I had accidentally inserted a single "stupid semicolon" immediately after a FOR conditional statement.
point is, everyone needs to check their work, because these "stupid errors" can get by any of us. your learning now, will save you time and money in the future.
.
Well, this was probably a bad idea,
yeah.... it's unfair to the newbie poster to be handed such a lovingly polished solution, and it's unfair to the rest of us here who strive to teach students how to do their own work without handing it to them.
it'd be one thing if he were a professional trying to solve a difficult problem. but the rule here is "We Don't Do Your Homework". it's kind of hidden up at the top of every forum as a sticky.
but, i will say, your code is very nice.
.
give a man a fish.
i'm still saying that overwriting the original string with \newline characters is a bad idea. it makes the program unnecessarily complicated, and opens up more opportunities for buggy code.
I suggest that you reconsider your approach to the problem. to put it one way, i think you are "going around your ass to get to your elbow". nothing personal, its a problem all new programmers (and some old) have.
consider this: simply break your string into tokens that are already delimited by the existing spaces. then, concatenate all IP addresses (with or without their trailing commas) into one string.
const char str = "/usr/bin/ssh * 147.188.195.15, 147.188.193.15, 147.188.193.16 22";
char PathName[MAXPATHSIZE] = '\0';
char UID[MAXUIDSIZE] = '\0';
char IPaddr[MAXIPADDRSIZE] = '\0';
char Port[MAXPORTSIZE] = '\0';
char *ptr;
ptr = strtok(str," ");
while (ptr != NULL)
{
if (strstr( ptr, "/") != 0)
strcpy (PathName, ptr);
else if (strstr( ptr, ".") != 0)
strcat (IPaddr, ptr);
else if (strstr( ptr, "*") != 0)
strcpy (UID, ptr);
/*
... now you figure out how to distinguish between numeric UIDs
... and numeric Port Numbers
... One possibility is a "state machine" or just a flag that tracks when
... the IPaddresses have been found, so a subsequent
... simple numeric must be a Port and not a UID.
...*/
ptr = strtok(NULL, " ");
}
.
swap the product and seller indices
[pseudo]
print columnheaders, newline
for p = 1 to NUMPRODS
print "product #", p
for s = 1 to NUMSELLERS
print matrix[s-1][p-1]
next s
print newline
next p
done
[/pseudo]
.
Hello, I'm trying to split a string into 4 separate strings using a delimiter "\n"
i would suggest that replacing printable characters with '\n' as a delimiter is a Bad Idea. Because what is '\n', anyhow?
is it 0x0D ?
is it 0x0A ?
or is it 0x0D + 0x0A ?
rethink the problem and just parse your tokens on what actually exists without overwriting. if you must overwrite, then at least use another printable character.
.
please.
just.... stop.
once you address what walt said, your first code problem i see is <conio.h>
remove that line, and remove everything that depends on it. then rewrite in standard C.
until you do that, i doubt anyone here will bother trying to compile and run your code, because no one uses the joke of a shitty compiler that continues to use that deprecated library.
hi....... I want a program in c based on any data structures with de code.plz............it shd be catchy n can be based on reality
omfg U R 2 cool! wer R U @ a/s/l? Did U gt de CODEZ? snd dem 2 me PLZ! zomgWTFbbq!!!1 lololol
.