Home | Community | Message Board


DaVinci Vaporizer
Please support our sponsors.

General Interest >> Science and Technology

Welcome to the Shroomery Message Board! You are experiencing a small sample of what the site has to offer. Please login or register to post messages and view our exclusive members-only content. You'll gain access to additional forums, file attachments, board customizations, encrypted private messages, and much more!

Jump to first unread post. Pages: < Back | 1 | 2 | 3 | 4 | 5 | Next >  [ show all ]
OfflineSeussA
Error: divide byzero


Folding@home Statistics
Registered: 04/27/01
Posts: 23,394
Loc: Caribbean
Last seen: 3 months, 2 days
Re: Programming Suggestions [Re: cokane]
    #16137912 - 04/25/12 02:55 PM (1 year, 23 days ago)

I want to help you learn, not do your work for you... follow along rather than skipping to the end.

It is always easier to write a small part of code, test it, then write another small part, test it, and so on... start with main() so that you can run your program:

Code:

#include <iostream>
#include <cstdlib>

using namespace std;

// we will add more here

int main()
{
float numb[3];

for(int i=0; i<=2; ++i)
{
cout << "Please enter a float: ";
cin >> numb[i];
}

// we will add more here

system("pause");
}



Compile the above and fix anything that needs fixing... once you get that working, add in the print_integers function:

Code:

#include <iostream>
#include <cstdlib>

using namespace std;

// we will add more here

void print_integers(float a[], int size)
{
for(int i=0; i<size; ++i)
std::cout << a[i] << std::endl; // eventually call round(a[i])
}

int main()
{
float numb[3];

for(int i=0; i<=2; ++i)
{
cout << "Please enter a float: ";
cin >> numb[i];
}

print_integers(numb, 3);

system("pause");
}



Compile the above and fix anything that needs fixing... once you get that working, add in the round() function:

Code:

#include <iostream>
#include <cstdlib>

using namespace std;

int round(float x)
{
// add in code to round the number, will produce an error without a return statement
}

void print_integers(float a[], int size)
{
for(int i=0; i<size; ++i)
std::cout << round(a[i]) << std::endl;
}

int main()
{
float numb[3];

for(int i=0; i<=2; ++i)
{
cout << "Please enter a float: ";
cin >> numb[i];
}

print_integers(numb, 3);

system("pause");
}



I left a little bit for you to do...  take it a step at a time to help you understand what is happening.


--------------------
Just another spore in the wind.


Post Extras: Print Post  Remind Me! Notify Moderator
Offlineimachavel
Stranger
Male User Gallery


Registered: 06/06/07
Posts: 8,700
Loc: Florida - not listed
Last seen: 1 day, 18 hours
Re: Programming Suggestions [Re: cokane]
    #16137926 - 04/25/12 03:00 PM (1 year, 23 days ago)

indeed:

g++ -o floatingintegers floatingintegers.c
floatingintegers.c:6:1: error: expected initializer before ‘void’

ok I rewrote it as such:

#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 :thumbdown:


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");
}



and got another error:

g++ -o floatingintegers floatingintegers.c
floatingintegers.c: In function ‘int round(float)’:
floatingintegers.c:9:1: error: expected initializer before ‘int’
floatingintegers.c: At global scope:
floatingintegers.c:11:1: error: expected unqualified-id before ‘{’ token

should I use the {} as initializers for function definitions? Or opt for another character to use as an initializer like for example () or =

{} is for an initializer list right?

=  is for assignment expression

and () is for an expression list right? Did I say that right? I'm not too great with initializers just know the basic {} for function definitions

in c or c with classes
{
}
needs to be on a seperate line each


--------------------


Post Extras: Print Post  Remind Me! Notify Moderator
Offlineimachavel
Stranger
Male User Gallery


Registered: 06/06/07
Posts: 8,700
Loc: Florida - not listed
Last seen: 1 day, 18 hours
Re: Programming Suggestions [Re: Seuss]
    #16137964 - 04/25/12 03:10 PM (1 year, 23 days ago)

err.. what float should be added?

g++ -o floatingintegers floatingintegers.c
danel@danel-Dimension-4700:~/code/printnumberstofloatinc$ ./floatingintegers
Please enter a float:


Lol have no idea what floating integers are, just trying to follow along to learn the correct initializers and function definitions, as well as correctly addressed libraries, and other correct ways to format, basic beginner stuff like gui writing I was working on a project to

include sdl library

load surface textures

load screen

set video mode

load bit map

load image

delay loading

reload

quit


so the image flashes twice. Not too fancy, not sure how I'd get it to flash infinitely with a set delay, reading up about it today. Good job solving the floating integers, except I'm not sure of the math equation


--------------------


Post Extras: Print Post  Remind Me! Notify Moderator
OfflineSeussA
Error: divide byzero


Folding@home Statistics
Registered: 04/27/01
Posts: 23,394
Loc: Caribbean
Last seen: 3 months, 2 days
Re: Programming Suggestions [Re: imachavel]
    #16137980 - 04/25/12 03:15 PM (1 year, 23 days ago)

Quote:

{} is for an initializer list right?

=  is for assignment expression

and () is for an expression list right?




Almost...

{ } define a block of code.  The compiler treats a block of code like a single statement.

= is for assignment

and () have multiple meanings depending upon context.  However, the most common use is for argument lists.


--------------------
Just another spore in the wind.


Post Extras: Print Post  Remind Me! Notify Moderator
Offlineimachavel
Stranger
Male User Gallery


Registered: 06/06/07
Posts: 8,700
Loc: Florida - not listed
Last seen: 1 day, 18 hours
Re: Programming Suggestions [Re: Seuss]
    #16138032 - 04/25/12 03:34 PM (1 year, 23 days ago)

thanks

btw are you used to writing in c#? I removed that system pause and it seems to work better:

./floatingintegers
Please enter a float: 1
Please enter a float: 2
Please enter a float: 3
1065353216
1073741824
1077936128

here is the whole thing:

#include <iostream>
#include <cstdlib>

using namespace std;

int round(float x)
{
  // add in code to round the number, will produce an error without a return statement
}

void print_integers(float a[], int size)
{
  for(int i=0; i<size; ++i)
    std::cout << round(a) << std::endl;
}

int main()
{
  float numb[3];

  for(int i=0; i<=2; ++i)
  {
    cout << "Please enter a float: ";
    cin >> numb;
  }

  print_integers(numb, 3);

}



//Just removed the system pause as it was giving me an error


--------------------


Edited by imachavel (04/25/12 04:33 PM)


Post Extras: Print Post  Remind Me! Notify Moderator
Offlinecokane
Stranger
Male User Gallery


Registered: 09/16/08
Posts: 907
Last seen: 28 days, 20 hours
Re: Programming Suggestions [Re: imachavel]
    #16141674 - 04/26/12 11:20 AM (1 year, 22 days ago)

Status update, I have gotten the code to let me input 3 numbers and have gotten it to cout 3 numbers, but the truncation part is not working. I called the round function in Main and all it did was made the first number change by moving the decimal point over example cin 1.1 turned into 11.1, so I took it out.

Code:
#include <iostream>
#include <cstdlib>

using namespace std;
int i;


inline int round(float x) {
return static_cast<int>(i+0.5);
}
void print_integers(float x[], int size)

{
for(int i=0; i<size; ++i)
std::cout <<x[i] << std::endl; // eventually call round(a[i])
}

int main(){
float numb[3];

for(int i=0; i<=2; ++i)
{
cout << "Please enter a float: ";
cin >> numb[i];
}

print_integers(numb, 3);// we will add more here

system("pause");
}



--------------------
Microsoft Certified Technology Specialist  (Windows 7)
CompTIA A+ & Security+
TrollTIA Lurking+


Post Extras: Print Post  Remind Me! Notify Moderator
OfflineSeussA
Error: divide byzero


Folding@home Statistics
Registered: 04/27/01
Posts: 23,394
Loc: Caribbean
Last seen: 3 months, 2 days
Re: Programming Suggestions [Re: cokane]
    #16142720 - 04/26/12 04:19 PM (1 year, 22 days ago)

I just tested my program and it works fine.  You forgot to call "round()" and you changed the round() function so that it no longer works with negative numbers.


--------------------
Just another spore in the wind.


Post Extras: Print Post  Remind Me! Notify Moderator
Offlinecokane
Stranger
Male User Gallery


Registered: 09/16/08
Posts: 907
Last seen: 28 days, 20 hours
Re: Programming Suggestions [Re: Seuss]
    #16143993 - 04/26/12 09:20 PM (1 year, 21 days ago)

This is truncating all numbers to zero. I am so close I can smell it.

Code:
#include <iostream>
#include <cstdlib>

using namespace std;
int i;


inline int round(float a) {
static_cast<int>((i>0.0f) ? i+0.5f : i-0.5f);
return 0;
}
void print_integers(float a[], int size)

{
for(int i=0; i<size; ++i)
std::cout <<round (a[i]) << std::endl; // eventually call round(a[i])

}

int main(){
float numb[3];

for(int i=0; i<=2; ++i)
{
cout << "Please enter a float: ";
cin >> numb[i];
}

print_integers(numb, 3);// we will add more here

system("pause");
}



--------------------
Microsoft Certified Technology Specialist  (Windows 7)
CompTIA A+ & Security+
TrollTIA Lurking+


Post Extras: Print Post  Remind Me! Notify Moderator
InvisibleEvil Clown


Registered: 03/26/12
Posts: 149
Loc: undisclosed
Re: Programming Suggestions [Re: cokane]
    #16144085 - 04/26/12 09:36 PM (1 year, 21 days ago)

remove the return zero from the round function.


Post Extras: Print Post  Remind Me! Notify Moderator
Offlinecokane
Stranger
Male User Gallery


Registered: 09/16/08
Posts: 907
Last seen: 28 days, 20 hours
Re: Programming Suggestions [Re: Evil Clown]
    #16144267 - 04/26/12 10:01 PM (1 year, 21 days ago)

If I take the return 0; out it returns 1068708535732 number bla bla which I think is its memory location.


--------------------
Microsoft Certified Technology Specialist  (Windows 7)
CompTIA A+ & Security+
TrollTIA Lurking+


Post Extras: Print Post  Remind Me! Notify Moderator
InvisibleEvil Clown


Registered: 03/26/12
Posts: 149
Loc: undisclosed
Re: Programming Suggestions [Re: cokane]
    #16144343 - 04/26/12 10:13 PM (1 year, 21 days ago)

you had the input parameter as 'a' instead of 'i' and I moved the return .


inline int round(float i) {
return static_cast<int>((i>0.0f) ? i+0.5f : i-0.5f);
}


Post Extras: Print Post  Remind Me! Notify Moderator
InvisibleEvil Clown


Registered: 03/26/12
Posts: 149
Loc: undisclosed
Re: Programming Suggestions [Re: Evil Clown]
    #16144358 - 04/26/12 10:16 PM (1 year, 21 days ago)

Code:
#include <iostream>
#include <cstdlib>

using namespace std;

inline int round(float i) {
return static_cast<int>((i>0.0f) ? i+0.5f : i-0.5f);
}

void print_integers(float a[], int size)

{
for(int i=0; i<size; ++i)
std::cout <<round (a[i]) << std::endl; // eventually call round(a[i])

}

int main(){
float numb[3];

for(int i=0; i<=2; ++i)
{
cout << "Please enter a float: ";
cin >> numb[i];
}

print_integers(numb, 3);// we will add more here

system("pause");
}



Edited by Evil Clown (04/26/12 10:23 PM)


Post Extras: Print Post  Remind Me! Notify Moderator
Offlinecokane
Stranger
Male User Gallery


Registered: 09/16/08
Posts: 907
Last seen: 28 days, 20 hours
Re: Programming Suggestions [Re: cokane]
    #16144449 - 04/26/12 10:36 PM (1 year, 21 days ago)

Very nice, thanks for the help Evil Clown, Suess, imachavel and everyone else who has been helping me. :fuckinawesome:


--------------------
Microsoft Certified Technology Specialist  (Windows 7)
CompTIA A+ & Security+
TrollTIA Lurking+


Edited by cokane (04/26/12 10:40 PM)


Post Extras: Print Post  Remind Me! Notify Moderator
OfflineSeussA
Error: divide byzero


Folding@home Statistics
Registered: 04/27/01
Posts: 23,394
Loc: Caribbean
Last seen: 3 months, 2 days
Re: Programming Suggestions [Re: cokane]
    #16145779 - 04/27/12 06:08 AM (1 year, 21 days ago)

Here is my final version:
Code:
#include <iostream>
#include <cstdlib>

using namespace std;

#define NFLOATS 3 // Number of floats to round

inline int round(float x)
{
return static_cast<int>((x>0.0f) ? x+0.5f : x-0.5f);
}

void print_integers(float x[], int size)

{
for(int i=0; i<size; ++i)
cout << "round(" << x[i] << ") = " << round(x[i]) << endl;
}

int main(){
float numb[NFLOATS];

for(int i=0; i<NFLOATS; ++i)
{
cout << "Please enter a float[" << (i+1) << "]: ";
cin >> numb[i];
}

print_integers(numb, NFLOATS);

return 0;
}



Post Extras: Print Post  Remind Me! Notify Moderator
Offlinecokane
Stranger
Male User Gallery


Registered: 09/16/08
Posts: 907
Last seen: 28 days, 20 hours
Re: Programming Suggestions [Re: Seuss]
    #16145960 - 04/27/12 08:41 AM (1 year, 21 days ago)

In the course of some of my reading I discovered a vector is basically the same thing as an array but vectors are used much more often than arrays, and are more efficient.

Code:
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>

inline int round(float a) {
return static_cast<int>(a+0.5);
}

void print_integers(const std::vector<int>& x) {
std::copy(x.begin(), x.end(),
std::ostream_iterator<int>(std::cout, "\n"));
}

int main() {
std::cout << "Please enter floating point values "
"separate by newlines ending with 'q'" << std::endl;
//input
std::vector<int> ints;
std::transform( std::istream_iterator<float>(std::cin),
std::istream_iterator<float>(),
std::back_inserter(ints), round);

print_integers(ints);
return EXIT_SUCCESS;
}



--------------------
Microsoft Certified Technology Specialist  (Windows 7)
CompTIA A+ & Security+
TrollTIA Lurking+


Post Extras: Print Post  Remind Me! Notify Moderator
InvisibleEvil Clown


Registered: 03/26/12
Posts: 149
Loc: undisclosed
Re: Programming Suggestions [Re: cokane]
    #16146001 - 04/27/12 09:17 AM (1 year, 21 days ago)

You changed your round function and it doesn't do negative numbers correctly.


Post Extras: Print Post  Remind Me! Notify Moderator
Offlinecokane
Stranger
Male User Gallery


Registered: 09/16/08
Posts: 907
Last seen: 28 days, 20 hours
Re: Programming Suggestions [Re: Evil Clown]
    #16147102 - 04/27/12 03:53 PM (1 year, 21 days ago)

This is funny http://youtu.be/x1TsOHyJPpw

C++ Programmer
Interviewing for a C++ Programming position. The sorry state of software development.


--------------------
Microsoft Certified Technology Specialist  (Windows 7)
CompTIA A+ & Security+
TrollTIA Lurking+


Post Extras: Print Post  Remind Me! Notify Moderator
Offlinecokane
Stranger
Male User Gallery


Registered: 09/16/08
Posts: 907
Last seen: 28 days, 20 hours
Re: Programming Suggestions [Re: cokane]
    #16175864 - 05/03/12 11:25 AM (1 year, 15 days ago)

I am using VIM in fedora Linux and writing a looping script that is not working. I named my file with a .sh extension, and also used the chmod +x filename to make it executable. 

Code:
#!/bin/bash
# STUDENT NAME HERE
let x=0;
while [ $x –lt 100000 ] # SOMETHING wrong here, keeps saying [: -lt: binary operator expected or command not found
do
let x=$x+1;
echo “The current loop count is : $x”
done



Full Lab Instructions

The purpose of this project is to learn about managing Unix processes.  In this project, you will:
• review the status of processes using the ps and the top commands,  kill a process
• run a process in the background  ,  change the priority level of a process
• schedule a process to run in the future using the at command
1. Review the status of processes using the ps command (with its wide variety of options) as well as the top command by executing the following commands.  Note how the output changes with the different switches (no screen shots are required for this step)
• ps • ps –f
• ps  -ef • ps –l
• ps  ax • top  (remember q to quit)
2. Using VI create the following script, naming it looper  (take a screen shot of your completed working script):
#!/bin/bash
# STUDENT NAME HERE
let x=0;
while [ $x –lt  100000 ]  # if script ends too quickly increase value
do
let x=$x+1;
      echo “The current loop count is :  $x” 
done
• Make the script executable  (using chmod)
• Once it runs successfully, comment out the ‘current loop count’ echo statement. The echo statement is only used to test the script.  It must be commented out (with a #) before proceeding!
• Copy the script to looper2 (create other copies if you wish, looper3, looper4,  etc.)
• Execute the script to see how long it runs, adjust to make it run at least 30 sec
• Rerun any of your looperX  scripts.  Press  CRTL+C  and/or CRTL+Z  during their execution, noting the difference between these two actions. 
• Use the ps command to see their status of your scripts  (take a screen shot of ps command showing non completed scripts that were stopped with CRTL/Z)
• Run any of your looper scripts several times.  Review the running processes utilizing the top command; utilize some of the interactive commands for top.  (take a screen shot for lab report.)    FYI, top should show processes using the most CPU (some of which should be the looper scripts) – it doesn’t seem to work in SUSE 11.1, and 2,  see if this has changed in the current version. 
• Kill the processes that are no longer running using the kill command  (if the plain kill command doesn’t remove the processes try other kill signals such as  -9)
• Now lets try running  processes in the background:
o Start the looper in the background several times by using the & (see p573-575 for examples ) – note the background and foreground id numbers (p575)
o Using the jobs command review  job running in the background.  Take a screen shot for the lab report
o Run looper in background  (see step above using &)  and move it to the foreground using the fg command, use CRTL/Z to stop it and return it to the background using the bg command 
• Change the priority level of a process
o Run one of your looper scripts and note the NICE and PRI levels using the ps –l command.  Take a screen shot.
o Issue a renice +15 PID  then run ps -l again, and note the difference in pri and nice.  Take a screen shot for your report
o start a process with nice –n 19 ./looper2 OR ./looper2&  (for background)
• Schedule a process to run using the at command
o start the at daemon (which isn’t running by default in SUSE) by typing rcatd start
o start a process using at 12:00pm today (use a  time a few minutes into the future), then type the following:
at>  date >  /root/attestfile
at>  who >>  /root/attestfile    (CNTL+D to end)  (take a screen shot prior to CNTL+D)
o wait for execution, then run the mail program.  You should see a mail item containing the executed commands


--------------------
Microsoft Certified Technology Specialist  (Windows 7)
CompTIA A+ & Security+
TrollTIA Lurking+


Post Extras: Print Post  Remind Me! Notify Moderator
OfflineSeussA
Error: divide byzero


Folding@home Statistics
Registered: 04/27/01
Posts: 23,394
Loc: Caribbean
Last seen: 3 months, 2 days
Re: Programming Suggestions [Re: cokane]
    #16176352 - 05/03/12 01:58 PM (1 year, 15 days ago)

You are missing a semicolon ";" after the while
You have to add an eval before the echo

Code:
#!/bin/bash
# STUDENT NAME HERE
let x=0;
while [ $x -lt 100000 ];
do
let x=$x+1;
eval echo "The current loop count is : $x"
done



--------------------
Just another spore in the wind.


Post Extras: Print Post  Remind Me! Notify Moderator
Offlinecokane
Stranger
Male User Gallery


Registered: 09/16/08
Posts: 907
Last seen: 28 days, 20 hours
Re: Programming Suggestions [Re: Seuss]
    #16176468 - 05/03/12 02:34 PM (1 year, 15 days ago)

thanks, it works now.


--------------------
Microsoft Certified Technology Specialist  (Windows 7)
CompTIA A+ & Security+
TrollTIA Lurking+


Post Extras: Print Post  Remind Me! Notify Moderator
Jump to top. Pages: < Back | 1 | 2 | 3 | 4 | 5 | Next >  [ show all ]

General Interest >> Science and Technology

Similar ThreadsPosterViewsRepliesLast post
* Statistics time: Programming Languages and Programmers
( 1 2 all )
delta9 1,687 26 05/16/07 02:17 PM
by SymmetryGroup8
* C++ Programming
( 1 2 3 all )
cortex 1,523 44 12/01/09 02:32 PM
by Chespirito
* New ATI drivers include cool solar system simulations jdirty 6,667 3 06/02/07 04:46 PM
by HELLA_TIGHT
* Programming books or tutorials? Anonymous 570 8 06/12/03 03:09 PM
by Anonymous
* Starting to program java
wiggles
547 4 04/01/07 11:20 PM
by Seuss
* Any computer programmers in here? (with a job)
( 1 2 all )
subiedude 753 22 05/13/10 01:04 AM
by kake
* Shroomery programming/math/etc. self-study peer(s)? If you want to mentor, that's fine too.
( 1 2 all )
seek 705 21 03/20/09 04:42 PM
by krypto2000
* Learning Programming Libertin 424 6 02/14/10 09:10 AM
by daytripper05

Extra information
You cannot start new topics / You cannot reply to topics
HTML is disabled / BBCode is enabled
Moderator: Lana, trendal, Diploid, automan, johnm214
1,818 topic views. 0 members, 2 guests and 0 web crawlers are browsing this forum.
[ Toggle Favorite | Print Topic ]
Search this thread:
Crestline Sales - MycoPath
Please support our sponsors.

Copyright 1997-2013 Mind Media. Some rights reserved.

Generated in 0.096 seconds spending 0.003 seconds on 19 queries.