|
cokane
Stranger



Registered: 09/16/08
Posts: 907
Last seen: 29 days, 6 hours
|
Re: Programming Suggestions [Re: Seuss]
#16110533 - 04/19/12 09:37 AM (1 year, 29 days ago) |
|
|
I cleaned it up some.
Code:
#include <iostream> using namespace std;
void print_even(int start, int end) { if(start < end) { for(int i=start; i<=end; ++i) // count up { // This is a binary/math trick which will check if // the last bit of the integer is 1, if the last bit is 1 the number // is odd, therefor it inverses it with '!' if( !(i & 1) ) { cout << i << endl; } } } else { for(int i=start; i>=end; --i) { // This is another math trick that will divide the number with 2 if a // fraction remains it will return 1, therefore we are also // inverting this with "!" if( !(i % 2 ) ) cout << i << endl; } }
return; } int main() { int value1, value2; cout << "Please enter a small number." << endl; cin >> value1; cout << "Please enter a larger number." << endl; cin >> value2;
print_even(value1,value2); cout << endl; cout << "WOW This program counts by two's very fast!!!!!" << endl; cout << endl; return 0; }
-------------------- Microsoft Certified Technology Specialist (Windows 7)
CompTIA A+ & Security+
TrollTIA Lurking+
|
cokane
Stranger



Registered: 09/16/08
Posts: 907
Last seen: 29 days, 6 hours
|
Re: Programming Suggestions [Re: cokane]
#16110545 - 04/19/12 09:41 AM (1 year, 29 days ago) |
|
|
I was messing with part 2 and 3 and for some reason its adding even numbers into my total even though I am trying to make it not add even numbers, by making them count as zero.
Code:
#include <iostream> using namespace std;
int main() { cout << "This caclualtor only adds positive odd valued numbers. " << endl; cout << endl; cout << "Please enter an odd valued interger to add or press 0 (zero) to exit. " << endl; cout << endl;
int total = 0;
for(int number = 0; ;number++) // infinito for loop that can only be closed by entering zero { cin >> number;
if(number <= 0) // break dance break; if(number == 7) // skippy continue; if (!(number % 2 == 0)) // THIS PART SEEMS MESSED UP // cout << "The current total is " << endl; cout << endl; total = total + number; cout << total << endl; } return 0; }
I was hoping to work on this more today, but I have to go to my parents lake house and install a water pump. I installed the same water pimp last year, maybe this year they will learn how to properly winterize it so it doesnt freeze and crack... FUDGER FACE
-------------------- Microsoft Certified Technology Specialist (Windows 7)
CompTIA A+ & Security+
TrollTIA Lurking+
Edited by cokane (04/19/12 09:50 AM)
|
Seuss
Error: divide byzero



Registered: 04/27/01
Posts: 23,394
Loc: Caribbean
Last seen: 3 months, 2 days
|
Re: Programming Suggestions [Re: cokane]
#16110668 - 04/19/12 10:34 AM (1 year, 29 days ago) |
|
|
This is why indentation is important. Keep your code clean!
You have this: Code:
if (!(number % 2 == 0)) // THIS PART SEEMS MESSED UP // cout << "The current total is " << endl; cout << endl; total = total + number; cout << total << endl;
... but, you want this (conceptually... this is still wrong without braces): Code:
if (!(number % 2 == 0)) // THIS PART SEEMS MESSED UP // cout << "The current total is " << endl; cout << endl; total = total + number; cout << total << endl;
... of course, you need to add in some braces { } to actually make the indented block a single execution block.
To be even more clear, your current code is equivalent to: Code:
if (!(number % 2 == 0)) // THIS PART SEEMS MESSED UP // { cout << "The current total is " << endl; } cout << endl; total = total + number; cout << total << endl;
|
imachavel
Stranger



Registered: 06/06/07
Posts: 8,700
Loc: Florida - not listed
Last seen: 5 hours, 43 minutes
|
Re: Programming Suggestions [Re: Seuss]
#16112216 - 04/19/12 05:42 PM (1 year, 29 days ago) |
|
|
what is the obsession now with every programmer hearing that if you use a java virtual machine and compile java code for a living you will make a ton of money. Simply because adobe is out dated? Will java code truly be every programmers dream from now on just to help with a run time environment for a network app that doesn't want to rely on adobe flash to run a video format?
--------------------
|
Chespirito
Stranger



Registered: 02/13/09
Posts: 3,258
|
Re: Programming Suggestions [Re: imachavel]
#16112608 - 04/19/12 07:17 PM (1 year, 29 days ago) |
|
|
what?
|
Seuss
Error: divide byzero



Registered: 04/27/01
Posts: 23,394
Loc: Caribbean
Last seen: 3 months, 2 days
|
Re: Programming Suggestions [Re: Chespirito]
#16113132 - 04/19/12 09:28 PM (1 year, 29 days ago) |
|
|
> what
... are all of those fish doing up there?
-------------------- Just another spore in the wind.
|
Annom
※※※※※※



Registered: 12/22/02
Posts: 6,329
Loc: Europe
Last seen: 21 minutes, 36 seconds
|
Re: Programming Suggestions [Re: imachavel]
#16113287 - 04/19/12 09:58 PM (1 year, 29 days ago) |
|
|
I think you fail the Turing test.
|
Evil Clown


Registered: 03/26/12
Posts: 149
Loc: undisclosed
|
Re: Programming Suggestions [Re: Annom]
#16113431 - 04/19/12 10:33 PM (1 year, 29 days ago) |
|
|
I program exclusively in turtle language.
|
imachavel
Stranger



Registered: 06/06/07
Posts: 8,700
Loc: Florida - not listed
Last seen: 5 hours, 43 minutes
|
Re: Programming Suggestions [Re: Annom]
#16121835 - 04/21/12 10:59 PM (1 year, 27 days ago) |
|
|
Quote:
Annom said: I think you fail the Turing test.
omg did I???!?
http://en.wikipedia.org/wiki/Turing_test
you mean I can't tell if your computer is programmed to type in an answer and respond? hrr hurr or if it's a person? hurr hrr. no captcha? hrr hrr. Here is a geek test:
was your first language?
a) english? or
b) c#
harr hurr
Anyway this is all I really know about Java, and therefore don't really know much what it is used for:
The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities. Java applications are typically compiled to bytecode (class file) that can run on any Java Virtual Machine (JVM) regardless of computer architecture. Java is a general-purpose, concurrent, class-based, object-oriented language that is specifically designed to have as few implementation dependencies as possible. It is intended to let application developers "write once, run anywhere" (WORA), meaning that code that runs on one platform does not need to be recompiled to run on another. Java is currently one of the most popular programming languages in use, particularly for client-server web applications, with a reported 10 million users.
funny, I didn't know it didn't need to be recompiled to another platform. Because I've heard writing it in Linux is a pain in the ass because of compatibility, that you have to write a whole platform it can run on. I could have heard wrong, it's very easy. That last thing I said didn't make sense since, it seems it was made EXCLUSIVELY for linux at first, considering that oracle made it, and oracle is well known for virtualization, and of course they helped turn bsd unix kernel into a desktop server platform with red had(which of course wasn't done yet until linus made the linux kernel from the unix one)
btw THIS:
http://www.benbarbour.com/node/43
looks fascinating. Actually I take that back, that last part with assembly language to address the kernel routine compiler looks very very boring. Plus what more would you learn then what a boot sector is, and if you wanted to see the real life application of writing that, just put an OS .iso to bootable media and install it to any storage device such as an hdd etc.
--------------------
Edited by imachavel (04/21/12 11:26 PM)
|
cokane
Stranger



Registered: 09/16/08
Posts: 907
Last seen: 29 days, 6 hours
|
Re: Programming Suggestions [Re: imachavel]
#16132177 - 04/24/12 09:21 AM (1 year, 24 days ago) |
|
|
I got a NEW assignment to work on.(among other last minute papers I need to write, the end of the semester is fast approaching. I am going to post a paper about VPN's later today.)
Code:
Write a program that takes an array of float (with special attention to functions with array parameters) and prints its contents to the nearest integer. The rounding should be totally programmed by you (don't use any C++ special libraries for this).
You could write two functions in addition to main:
int round (float x)
which would round any float to the nearest int, and return the int value
and
void print_integers(float a [ ], int size)
which would print the contents of array of size size a already rounded to the nearest int.
Your main function could call print_integers, and print_integers would call round for every element of the array, and print the rounded value.
There are several easy ways to make round work. Use any you want, but you will have to write it yourselves.
-------------------- Microsoft Certified Technology Specialist (Windows 7)
CompTIA A+ & Security+
TrollTIA Lurking+
|
imachavel
Stranger



Registered: 06/06/07
Posts: 8,700
Loc: Florida - not listed
Last seen: 5 hours, 43 minutes
|
Re: Programming Suggestions [Re: cokane]
#16135129 - 04/24/12 10:12 PM (1 year, 24 days ago) |
|
|
oh god those integers are much easier to print in python, about 5 lines and you can do all the math you want. In c if it's one file you'll be lucky. But if it's one file simple enough to compile
gcc or g++ -o file name inputfile.c
you got a web site to show you what to do? like a hello world but about printing math functions like adding or subtracting or multiplying to get the command line to give you a response? If so post it and I'll try it out and tell you what happens. I'd hate to know how to write a file system in and have to write one. You would have to come up with so many brand new names to address other files and directories. Probably wasn't since 1986 anyone really talked about writing an entire file system. A program is much easier and relies and already made libraries instead of you having to write an entire new library
--------------------
|
Seuss
Error: divide byzero



Registered: 04/27/01
Posts: 23,394
Loc: Caribbean
Last seen: 3 months, 2 days
|
Re: Programming Suggestions [Re: imachavel] 1
#16136891 - 04/25/12 07:02 AM (1 year, 24 days ago) |
|
|
> oh god those integers are much easier to print in python, about 5 lines and you can do all the math you want. In c if it's one file you'll be lucky.
You really shouldn't make declarative statements about subjects that you have no fucking clue what you are talking about: Code:
int round(float x) { return static_cast<int>((x>0.0f) ? x+0.5f : x-0.5f); } void print_integers(float a[], int size) { for(int i=0; i<size; ++i) std::cout << round(a[i]) << std::endl; }
It is actually a lot easier than the above and could be done with a single line, but the constraints placed upon the program by the instructor forbid us from doing: Code:
void print_integers(float a[], int size) { for(int i=0; i<size; ++i) std::cout << std::fixed << std::setprecision(0) << a[i] << std::endl; }
-------------------- Just another spore in the wind.
|
cokane
Stranger



Registered: 09/16/08
Posts: 907
Last seen: 29 days, 6 hours
|
Re: Programming Suggestions [Re: Seuss]
#16137073 - 04/25/12 09:21 AM (1 year, 23 days ago) |
|
|
I wrote a bubble sort array earlier this semester, I wonder if there is a way to mold my older code into this new assignment.
Code:
#include <iostream> #include <cstdlib>
using namespace std;
int main() { int numb[3]; int i, j;
for(i=0;i<=2;i++) { cout << "Please enter a integer : "; cin >> numb[i]; }
for(i=0;i<=2;i++) { for(j=i+1;j<=2;j++) { int temp;
if(numb[i] > numb[j]) { temp = numb[i]; numb[i] = numb[j]; numb[j] = temp; } } }
for(i=0;i<=2;i++) { cout << endl << numb[i] << endl; cout << endl; }
system("pause"); }
-------------------- Microsoft Certified Technology Specialist (Windows 7)
CompTIA A+ & Security+
TrollTIA Lurking+
|
Seuss
Error: divide byzero



Registered: 04/27/01
Posts: 23,394
Loc: Caribbean
Last seen: 3 months, 2 days
|
Re: Programming Suggestions [Re: cokane]
#16137088 - 04/25/12 09:29 AM (1 year, 23 days ago) |
|
|
The first few lines of your code that record the input from the keyboard and store the results in an array can be used, but you need to modify it to use floats rather than ints. Drop the last two for loops that do the sort and print the result and replace them with a call to the functions I provided above. Pretty trivial. (I didn't test the code I wrote...)
Because you are using the std namespace, you can remove the std:: scope statements from any of the stream operators.
-------------------- Just another spore in the wind.
|
cokane
Stranger



Registered: 09/16/08
Posts: 907
Last seen: 29 days, 6 hours
|
Re: Programming Suggestions [Re: Seuss]
#16137246 - 04/25/12 10:37 AM (1 year, 23 days ago) |
|
|
So far, so bad.
Code:
#include <iostream> #include <cstdlib>
using namespace std;
int main() { float numb[3]; float i; int size;
for(i=0;i<=2;i++) { cout << "Please enter a integer : "; cin >> numb[i]; }
int round(float x) return static_cast<int>((x>0.0f) ? x+0.5f : x-0.5f); void print_integers(float a[], int size){ for(int i=0; i<size; ++i) cout << round(a[i]) << endl; } system("pause"); }
-------------------- Microsoft Certified Technology Specialist (Windows 7)
CompTIA A+ & Security+
TrollTIA Lurking+
|
Seuss
Error: divide byzero



Registered: 04/27/01
Posts: 23,394
Loc: Caribbean
Last seen: 3 months, 2 days
|
Re: Programming Suggestions [Re: cokane]
#16137291 - 04/25/12 10:54 AM (1 year, 23 days ago) |
|
|
Move (and format/indent) the round and print_integers functions above main() ...
Remove int size from main, it isn't needed...
In place of what you moved above, add: print_integers(numb, 3);
Don't forget to add the curley braces back to the round function.
-------------------- Just another spore in the wind.
|
imachavel
Stranger



Registered: 06/06/07
Posts: 8,700
Loc: Florida - not listed
Last seen: 5 hours, 43 minutes
|
Re: Programming Suggestions [Re: Seuss]
#16137421 - 04/25/12 11:54 AM (1 year, 23 days ago) |
|
|
Quote:
Seuss said: The first few lines of your code that record the input from the keyboard and store the results in an array can be used, but you need to modify it to use floats rather than ints. Drop the last two for loops that do the sort and print the result and replace them with a call to the functions I provided above. Pretty trivial. (I didn't test the code I wrote...)
Because you are using the std namespace, you can remove the std:: scope statements from any of the stream operators.
I did, doesn't seem to work for crap:
gcc -o floatingintegers floatingintegers.c floatingintegers.c:1:5: warning: conflicting types for built-in function ‘round’ [enabled by default] floatingintegers.c: In function ‘round’: floatingintegers.c:1:29: error: ‘static_cast’ undeclared (first use in this function) floatingintegers.c:1:29: note: each undeclared identifier is reported only once for each function it appears in floatingintegers.c:1:41: error: expected expression before ‘int’ floatingintegers.c: In function ‘print_integers’: floatingintegers.c:2:44: error: ‘for’ loop initial declarations are only allowed in C99 mode floatingintegers.c:2:44: note: use option -std=c99 or -std=gnu99 to compile your code floatingintegers.c:2:74: error: expected expression before ‘:’ token danel@danel-Dimension-4700:~/code/printnumberstofloatinc$ g++ -o floatingintegers floatingintegers.c floatingintegers.c: In function ‘void print_integers(float*, int)’: floatingintegers.c:2:70: error: ‘cout’ is not a member of ‘std’ floatingintegers.c:2:98: error: ‘endl’ is not a member of ‘std’
Not great with the classes, but seems you wrote that wrong, addressing libraries in the wrong order. No big deal but all that blah blah you don't know what the fuck you are talking about gets rude when everyone makes mistakes INCLUDING YOURSELF it DOES happen doesn't it. I am BRAND NEW to c, and don't claim to know a lot, but I believe I was right saying this would be simpler in python
>>> from decimal import Decimal >>> Decimal(2.675) Decimal('2.67499999999999982236431605997495353221893310546875')
much much less addressing and using different libraries for function definitions. Well no because python addresses a c library which addresses these function definitions, but for all practical purposes it is much shorter and much simpler.
python floatingdecimalpython.py File "floatingdecimalpython.py", line 1 >>> from decimal import Decimal ^ SyntaxError: invalid syntax
crap I shouldn't have redirected like that. Ok let's try your last bit of code cokane:
gcc -o floatingintegers floatingintegers.c floatingintegers.c:1:20: fatal error: iostream: No such file or directory compilation terminated. danel@danel-Dimension-4700:~/code/printnumberstofloatinc$ g++ -o floatingintegers floatingintegers.c floatingintegers.c: In function ‘int main()’: floatingintegers.c:15:22: error: invalid types ‘float [3][float]’ for array subscript floatingintegers.c:19:1: error: a function-definition is not allowed here before ‘return’ floatingintegers.c:20:41: error: a function-definition is not allowed here before ‘{’ token
nope. Very interesting. I'm going to spend maybe 4 hours today trying to compare the difference between what suess wrote and what you wrote. I am brand new to c and c with classes and proprietary c(c# and object-o-c), just started trying to learn it a month ago. Interested on how different libraries can be addressed using different function definitions, as long as they create the same variables AND functions themselves, seems to be the main purpose. bb in a few hours(or days)
--------------------
|
imachavel
Stranger



Registered: 06/06/07
Posts: 8,700
Loc: Florida - not listed
Last seen: 5 hours, 43 minutes
|
Re: Programming Suggestions [Re: cokane]
#16137438 - 04/25/12 12:05 PM (1 year, 23 days ago) |
|
|
one thing that is entirely different about your code is that suess created one set of function definitions. You created many and several. One mistake and all of that is going to conflict itself. It's like taking a ball that bounces infinitely off a surface until it can't find a surface to bounce off over there, and that ball is an input or output signal. Now take the difference between one set of walls and then 15 sets of walls, it as though the ball will be directed to one error then to another then the whole thing will be filled with errors.
If you plan to create one set of function definitions, better to try and put it all into one set of curly brackets. If you plan to create a program with a gui, and you need to reference function definitions that point to any file or multiple files and libraries that the user can interact with by choosing a different gui 'button' or 'menu' and it has to be formatted with graphical interface for the user, then you need several functions definitions, and multiple files, but still should try and keep it concise.
I know very little about c, but know a little bit about the concept to know when writing it, when you create so many function definitions then it's harder to trouble shoot an error, because the return syntax error doesn't eally tell you specifically what it did wrong, it tells you how it's wrong from the perspective of where it looked for a file or library. So the problem may be on line 25, but if line 25 is looking for <iostream> or "iostream" and what it's looking for it can't find in iostream, it's going to tell you the error is in iostream, because it doesn't know the difference between the fact that you wrote something to use from iostream, and that you wrote whatever you used to address files and libraries within the function definitions is wrong. Crap this stuff can get confusing
--------------------
|
cokane
Stranger



Registered: 09/16/08
Posts: 907
Last seen: 29 days, 6 hours
|
Re: Programming Suggestions [Re: cokane]
#16137518 - 04/25/12 12:37 PM (1 year, 23 days ago) |
|
|
#include <iostream> #include <cstdlib>
using namespace std; int round(float x) void print_integers(float a[], int size) // The compiler is giving me a red error here 
int main() { float numb[3]; float i; print_integers(numb, 3);
for(i=0;i<=2;i++) { cout << "Please enter a integer : "; cin >> numb; }
return static_cast<int>((x>0.0f) ? x+0.5f : x-0.5f);
for(int i=0; i<size; ++i) cout << round(a) << endl; system("pause"); }
-------------------- Microsoft Certified Technology Specialist (Windows 7)
CompTIA A+ & Security+
TrollTIA Lurking+
|
Evil Clown


Registered: 03/26/12
Posts: 149
Loc: undisclosed
|
Re: Programming Suggestions [Re: cokane]
#16137898 - 04/25/12 02:50 PM (1 year, 23 days ago) |
|
|
needs more semi-colons
|
|