Announcement

Collapse
No announcement yet.

Anyone wanna help a brotha out?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Anyone wanna help a brotha out?

    8. Write a program that promps the user to input five decimal numbers. The program should then add the five decimal numbers, convert the sum to the nearest integer, and print the result.

    This is my last problem for my C++ programing class. I've been trying to get ahold of my proof. and get some help but the fucker hasnt responded back and my class is at 6:30 tommrw night... I would use my book but the fuck that had it before me somehow rip'd out the pages (buying new books from now on) If anyone can help me with this shit I'd appreaciate it =\

  • #2
    well i only know Java so i cant give u the exact code, but it goes something like this:

    get 5 doubles from user
    add the five doubles together
    add 0.5 to the sum
    convert to integer
    output

    Comment


    • #3
      Ugh, so I've tried to post this code a few times, but everytime I do it, I get an Apache error message telling me newreply.php has some messed up permissions (hence the code in an image below). Any mods/admins know what in the code could provoke that?

      Anyway, I can't help that much. I don't code in C++, or in much else anymore. But I do believe C++ has a built in round function (unlike Perl).

      Here is how I'd do it in Perl, maybe it'll give you some ideas. Or maybe someone who knows C++ better will answer.



      You should start hitting Google for some sample code, or go to a local bookstore tomorrow and start going through all the C++ stuff.

      Good luck.
      Ferengi Rule of Acquisition #98: Every man has his price.

      Comment


      • #4
        Code:
        #include iostream.h
        #include math.h
        int main(void){
           double one,two,three,four,five,sum;
           cout << "\nPlease enter the first number\n";
           cin >> one;
           cout << "\nPlease enter the second number\n";
           cin >> two;
           //repeat that so on and so forth till five
           //i have no time and probably can't remember right now to dummy proof that, so I hope that isn't part of it. I am also too lazy to make it a fancy little loop
           sum=one+two+three+four+five;
           if(sum*10>5)
              sum=math.floor(sum);
           else
              sum=math.ciel(sum);
           cout << "The sum is " << sum << ".";
           return 0;
        }
        I am not quite full to capacity right now on the cpp, so there is likely to be a lot of compiler initialization statements I am missing, I hope you know them. Also, the entire program might be wrong.
        Have fun!
        sage

        Comment


        • #5
          there's actually a different way to do it, something like:

          create a double counter
          get a number, add it straight to counter x5
          add 0.5
          convert to int

          this way u dont create 6 objects, only 2. one for the double sum and one for the int. but the first way is clearer if you're not very confident.

          Comment


          • #6
            Code:
            // Possible soution to HoGo's problem in Java
            // Author:   Rab
            // Note: I couldn't be bothered to compile it, might be an error or two.
            
            import java.io.BufferedReader;
            import java.io.InputStreamReader;
            import java.io.IOException;
            
            public class AddFiveDoubles
            {
                    public static void main(final String[] pArgs) throws IOException
                    {
                            final BufferedReader tKeyboard 
                                 = new BufferedReader(new InputStreamReader(System.in));
            
                            double tCounter = 0;
            
                            for (int i=5; i>0; i--)
                            {
                                   System.out.print("Enter a number: "); System.out.flush();
                                   tCounter += Double.parseDouble(tKeyboard.readLine());
                            }
            
                            tCounter += 0.5;
                            final int tInt = Integer.parseInt(tCounter);
                            System.out.println("The integer sum is: " + tInt);
                    }
            }
            Last edited by Rab; 06-10-2018, 09:48 AM. Reason: Removed personal information

            Comment


            • #7
              #include <iostream.h>
              #include <stdlib.h>

              int main()
              {

              //TelCat rocks!!!
              //there coule be a better way but I don't want to spend more than 3 mins.

              float total = 0;
              float num;
              int n;
              for (int i=0;i<5;i++)
              {
              cout << "Hey C++ newb, please enter an integer" <<endl;
              cin >> num;
              total += num;
              }

              n = total;

              cout << "Wannabe nerdie, the total is " << n <<endl;

              return 0;
              }
              ☕ 🍔 🍅 🍊🍏

              Comment


              • #8
                telcat RTFQ !

                Comment


                • #9
                  Code:
                  #include "stdafx.h"
                  #include "math.h"
                  int _tmain(int argc, _TCHAR* argv[])
                  {
                  	// ------------------------------------------------------------------------------------------------------------------
                  	// Write a program that promps the user to input five decimal numbers. 
                  	// The program should then add the five decimal numbers, convert the sum to the nearest integer, and print the result.
                  	// ------------------------------------------------------------------------------------------------------------------
                  
                  	// Declare space to hold 5 numbers and a running total
                  	double number;
                  	double total = 0;
                  
                  	// Get the user to enter 5 numbers and update the total after each time
                  	std::cout << "Enter 5 numbers: " << std::endl;
                  	for(int i=0; i<5; i++)
                  	{
                  		std::cin >> number;
                  		total+= number;
                  	}
                  
                  	// Tell the user the total
                  	std::cout << "Total is: " << total << std::endl;
                  
                  	// Get the decimal part of the total
                  	double temp = fmod(total,1);
                  
                  	// Round the total up/down depending on how big the decimal part of the total is
                  	if(temp>=0.5)
                  		total = ceil(total);
                  	else
                  		total = floor(total);
                  
                  	// Print out the rounded total
                  	std::cout << "Rounded total is: " << total << std::endl;
                  	return 0;
                  }
                  Using the +0.5 method
                  Code:
                  {
                  	// ------------------------------------------------------------------------------------------------------------------
                  	// Write a program that promps the user to input five decimal numbers. 
                  	// The program should then add the five decimal numbers, convert the sum to the nearest integer, and print the result.
                  	// ------------------------------------------------------------------------------------------------------------------
                  
                  	// Declare space to hold 5 numbers and a running total
                  	double number;
                  	double total = 0;
                  	int roundedTotal = 0;
                  
                  	// Get the user to enter 5 numbers and update the total after each time
                  	std::cout << "Enter 5 numbers: " << std::endl;
                  	for(int i=0; i<5; i++)
                  	{
                  		std::cin >> number;
                  		total+= number;
                  	}
                  
                  	std::cout << "Total is: " << total << std::endl;
                  
                  	// Calculate the rounded total
                  	roundedTotal = total+0.5;
                  	std::cout << "Rounded total is: " << roundedTotal << std::endl;
                  	return 0;
                  }
                  Last edited by Nockm; 01-12-2005, 08:36 AM.

                  Comment


                  • #10
                    Originally posted by HoGo
                    convert the sum to the nearest integer[/B]
                    Originally posted by Rab
                    add the five doubles together
                    add 0.5 to the sum
                    convert to integer


                    Didn't say round up to the nearest integer. What? you haven't done primary school maths?
                    Last edited by T3l Ca7; 01-12-2005, 08:31 AM.
                    ☕ 🍔 🍅 🍊🍏

                    Comment


                    • #11
                      telcat, when you convert a double to an integer it just chops off the stuff after the decimal point, so for example:

                      4.2 = 4
                      4.7 = 4
                      28.945385938 = 28

                      so to fix it you add 0.5 first, hence

                      4.2 becomes 4.7, which then = 4
                      4.7 becomes 5.2, which then = 5

                      see? now sit.

                      Comment


                      • #12
                        Originally posted by T3l Ca7
                        Originally posted by HoGo
                        convert the sum to the nearest integer[/B]
                        Originally posted by Rab
                        add the five doubles together
                        add 0.5 to the sum
                        convert to integer


                        Didn't say round up to the nearest integer. What? you haven't done primary school maths?
                        I know nothing about programming etc but these last two posts were great
                        Originally posted by Facetious
                        edit: (Money just PMed me his address so I can go to Houston and fight him)

                        Comment


                        • #13
                          i dont believe i used to know that stuff
                          5:gen> man
                          5:gen> i didn't know shade's child fucked bluednady

                          Comment


                          • #14
                            Originally posted by Rab
                            telcat, when you convert a double to an integer it just chops off the stuff after the decimal point, so for example:

                            4.2 = 4
                            4.7 = 4
                            28.945385938 = 28

                            so to fix it you add 0.5 first, hence

                            4.2 becomes 4.7, which then = 4
                            4.7 becomes 5.2, which then = 5

                            see? now sit.
                            Busted.
                            Music and medicine, I'm living in a place where they overlap.

                            Comment


                            • #15
                              Except I used the float, not double.

                              Run the damn program and see the result yourself (compilation warning about implicit type cast, but that's okey, the program runs fine).
                              ☕ 🍔 🍅 🍊🍏

                              Comment

                              Working...
                              X