If you use int main() you should put return 0; at the end of code
If you use int main() you should put return 0; at the end of code
And if you use void main(), you should use int main().If you use int main() you should put return 0; at the end of code
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Maybe you should count the correct code, then. I was talking to the post right before me. He asks if his code was correct, I said it had one error. Actually it has two, as I didn't see he wrote #inlude instead of #include.Originally Posted by cdalten
There would really be no logic to me talking about anything but the post right above me without quoting text. It also wouldn't be to smart to say something was close to correct when other good programmers has already said it isn't.
Last edited by SlyMaelstrom; 04-16-2006 at 04:21 PM.
Sent from my iPadŽ
Sorry to go off topic, but something needs to be said. I'd use a quote but I can't remember how off-hand. I guess that makes me an idiot, too. But no matter, the people I'm speaking to will know who they are. If it doesn't apply to you just ignore the following.
Hey now come on fellas, there's no need to get nasty. Believe it or not everybody's gotta start somewhere.
And honestly, how would you like it if you brought your car into the shop and said my car's running like garbage, but I want to fix it myself, and the mechanic replied "Fix the engine" ?
Not very helpful, if you aren't a mechanic, is it?
Now I agree that the best way to learn is often by research, trial, and error, but there's no need to attack someone for asking for help, even if they don't immediately grasp the concept.
But it's not like this BS only happens in this forum, no matter where you go you'll find the same thing. Someone asks for help, and some know-it-all (fill in the blank) wants to go and call them stupid and/or newb and/or any other number of insulting terms. Then the person who asked gives up on using the forums for what they were meant for. You see it all the time. Enough, already.
I don't care how good you are at anything. If you aren't willing to help someone who asks for help, just keep your mouth shut and move on.
Now some code to reflect on...
Error check that.Code:#include <stdio.h> int salemismean=1; int quzahismean=1; int abadnamecaller=1; int people=1; int actuallytryingtohelp=1; int peoplearecool; char pricks[7]="pricks"; int main() { if (salemismean==1 && (quzahismean==1 && abadnamecaller==1)) { printf("Ignore know-it-all %s.\n\n", pricks); } if (people==actuallytryingtohelp) { peoplearecool=1; printf("There's plenty of helpful people out there."); } getchar(); getchar(); return(0); }
Salem and Quzah, you should be ashamed of yourselves.
Next thing you know, people will be throwing feces at each other...
-James-
And by the way... My code may be chunky and ugly and entirely terrible... But it does work. Unless of course the smallest number is 0, or the biggest number is bigger than 999,999 (Which is really big... And you could set it higher.) It does compile and it does run and it does display the smallest number entered. And I've only been programming in C for four days.
Trying to figure out how to use arrays.Originally Posted by Salem
and i wrote something to find the max of two numbers and four numbers.Code:#include <stdio.h> #include <ctype.h> int main(int argc, char **argv) { char string[256]; printf( "Please enter a long string: " ); fgets ( string, 256, stdin ); int number1; number1 = &string[10] - &string[1]; if (number1 == 9) { number1 = 1; } printf( "the difference is, '%d'", number1 ); //getchar(); return 0; }
Code:#include <stdio.h> #include "tests05.h" int maxOf2(int, int); int maxOf4(int, int, int, int); int main (int argc, char** argv) { if ((maxOf2 (TEST1_A, TEST1_B) == RESULT1) && (maxOf2 (TEST2_A, TEST2_B) == RESULT2) && (maxOf2 (TEST3_A, TEST3_B) == RESULT3)) { printf("maxOf2 passed test cases\n"); } else { printf("maxOf2 failed test cases\n"); } if ((maxOf4 (TEST4_A, TEST4_B, TEST4_C, TEST4_D) == RESULT4) && (maxOf4 (TEST5_A, TEST5_B, TEST5_C, TEST5_D) == RESULT5) && (maxOf4 (TEST6_A, TEST6_B, TEST6_C, TEST6_D) == RESULT6)) { printf("maxOf4 passed test cases\n"); } else { printf("maxOf4 failed test cases\n"); } return 0; } int max0f2 (number1, number2) { if (number1 > number2) { return number1; } else { return number2; } } int maxOf4 (number1, number2, number3, number4) { if ((number1 > number2) && (number1 > number3) && (number1 > number4)) { return number1; } else if ((number2 > number1) && (number2 > number3) && (number2 > number4)) { return number2; } else if ((number3 > number1) && (number3 > number2) && (number3 > number4)) { return number3; } else { return number4; } }
that's what i had written before i came across these forums....as u can see the way i did it, it would not have worked with finding the smallest number out of N elements, hence i ASKED.
Last edited by theorbb; 04-16-2006 at 05:54 PM.
> but i have no idea how to go about solving
But you already had
> and i wrote something to find the max of two numbers and four numbers.
So really, what was the problem with having
int currentNumber;
int currentSmallestNumber;
and a bit of logic around calling max0f2(currentNumber,currentSmallestNumber);
Not that writing another function called minOf2() would have been a better idea at this point.
> fgets ( string, 256, stdin );
> int number1;
> number1 = &string[10] - &string[1];
C doesn't (yet) support mixing declarations and statements.
Did you use a C++ compiler to compile this code?
> int max0f2 (number1, number2)
Where are the types here?
int max0f2 (int number1, int number2)
@ Kazmeister
> My code may be chunky and ugly and entirely terrible... But it does work
Bull........ does it work.
You must be confusing bug-free with "works for me".Code:int n;//how many numbers <--- This is initialised to 0 int main() { int z[n]; <-- Wow, that's right, a zero length array ... z[n]=x; <-- stomp stomp stomp, merrily down the yellow brick road...
Sure, on your machine with just a few input numbers you might get lucky.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Horrible analogy. It's not even close to what happened here. It's more like:Originally Posted by Kazmeister
"Hi, I have to start a car."
"Turn the key."
"What?! You won't do it for me? How am I supposed to know what a key is?!"
You seem to be confusing me with someone who gives a ........ what you think. Be sure not to make that same mistake again.Originally Posted by Kazmeister
Quzah.
Last edited by quzah; 04-17-2006 at 01:18 AM. Reason: Analogies and the horrors thereof.
Hope is the first step on the road to disappointment.
so how would i go about finding the difference in size between 2 elements in a array?...and then find the minimum difference between those elements?Originally Posted by Salem>
fgets ( string, 256, stdin );
> int number1;
> number1 = &string[10
should i create a loop which finds the difference and then puts it into another array, lets say Small[N], and then do the whole pseudo code thing to find the smallest number?
but, the problem here is i dont know the proper way, since you just told me that
number1 = &string[10] - &string[1];
is 'supposedly' not possible (compiler did not give me any errors).
im using a program called cygwin to compile.
Last edited by theorbb; 04-17-2006 at 01:34 AM.
A 'mixed declaration' is you scattering the definition of your variables throughout the code block, rather than at the top of the block. This isn't supported in C prior to C99, which very very few C compilers, if any, fully implement. If it does work, you're either compiling as C++, which is what Salem just asked you, or more than likely, not a feature of the language, but a feature of your compiler.
You compare elements just like you any other variable. You know how to stick a value in a variable, right? You know how to stick a value in an array, right?An example of both. Now then, you know how to get a value of a variable right? And you know how to get the value at a specific index, right?Code:int x, y[2]; x = 1; y[ 0 ] = 2;An example of both. Now then, you know how to compare two variables, right?Code:int x = 5, y[2] = { 1, 0 }, z; z = x; z = y[ 1 ];An example of it. Surely you can figure out the final step missing from this illustration...Code:if( x < z )
How to compare two elements of an array? Think. It's not hard.
Quzah.
Hope is the first step on the road to disappointment.
maybe not hard for you, since u have been programming for five years...ive been programming for how long?...three weeks. whats easy for you can be difficult to grasp for others.Originally Posted by quzah
but thanks anyways, so i store the value of the element into a variable (eg z and x) then do the... if (x < z) then keep the x as currentSmallestNumber.
int array1[4];
int array2[4];
>array1[1]<array2[1]
compares the second element of array1 with the second element of array2
What Quzah is trying to teach you is a very valuable lesson for a programmer. There are two kinds of problems you're going to run into as a programmer. Logical problems and syntactical problems. Now, I'm sure, if you thought about it for 10 seconds, you could figure out how to compare two elements of an array. That's assuming of course you know the syntax well. You clearly don't.Originally Posted by theorbb
Now, this is what Quzah wants to teach you. For as long as you're going to be a programmer, you're going to run into syntax problems. Languages change, syntax changes... you can be the best programmer in the world and still run into a problem where you simply don't know the operator character or the function name or the keyword. It's up to you as the programmer to find that syntax though. There are dozens of references on the internet for comparing elements in an array. Why bother posting a message and waiting for an answer from someone, you might as well look it up yourself. As a programmer, being able to reference syntax and read documentation is a key skill. You have to learn it eventually, so start as early as you can.
Oh and I'm pretty sure Quzah has been programmer for much longer than 5 years.
Sent from my iPadŽ