Announcement

Collapse
No announcement yet.

C++ help

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

    C++ help

    I will continuously go back to and revive this thread for help throughout the year in my Engineering Computing and Programming course. Its basic C++ to solve mathmatical problems like for example lab 1 is create a program that can take the area of a square and subtract the area of a circle inside of it.

    Well to the actual question....

    Can someone explain to me the function of the char command?

    And what this line does exactly it is in one of my professors templates and examples and the guys office hours are during my football lifts.

    char dummy = ‘ ’;

    the rest of the code i get but i cant figure out what this line is... Also is there a way to input pi into C++ I have #include <math.h> already at top.

    #2
    Try a Google search, they're you're friend for programming needs Not sure I ever referenced Pi while doing C++, and like I said, I'd always start with Google, probably get more results and better explained than we'd do here :P

    char dummy = ''; creates a char variable type named dummy that doesn't have a value at this time.
    [img]http://img.photobucket.com/albums/v337/Igorod/troopdod.jpg[/img]
    [url=http://profile.xfire.com/trooper110][img]http://miniprofile.xfire.com/bg/co/type/1/trooper110.png[/img][/url]

    Comment


      #3
      alright i guess that helps, and the problem with googling something like this is i can prob get the answer but not understand why that is that way

      Comment


        #4
        I would just create a pi variable and set it = to 3.141592.
        float pi = 3.141592;

        char is a variable type generally used for ASCII characters. I believe it is an 8bit variable.
        [url=http://www.enjin.com/bf3-signature-generator][img]http://sigs.enjin.com/sig-bf3/1fad512dc784c11c.png[/img][/url]

        Comment


          #5
          Google= Excuse for people that don't want to answer the question and help others

          Comment


            #6
            #include <iostream> //enables C++ format
            #include <string> //enables readable english text
            #include <math.h> //enables math
            using namespace std; //enables cout<< and cin>>
            int main ( )
            {

            double x = 0.00;
            double y = 0.00;
            double d = 0.00;
            double a = 0.00;
            double p = 3.14159;
            double s = 0.00; //defines variables
            char blank = ' '; //creates blank space to return to

            cout<< "Enter the radius as a positive real number:"; //ask for radius as a real number x
            cin >> x; //x input

            if ( x < 0 )
            {
            cout << "Radius must be a positive number.";
            return 0;
            } //end if statement
            else
            {
            d = x * 2; //d will be the diameter of the circle and also a side of the square
            s = d * d; //calculation of the squares area; raduis times two, times itself. With s being the area of the square

            /* cout << "Area of Square:";
            cout << s << endl; //show area of square */

            y = d * p; //area of circle = diameter times pi

            /* cout << "Area of the Circle";
            cout << y << endl; */

            a = s - y;

            cout << "Area of the shaded region";
            cout << a << endl;
            } //end else statement

            cout << "Enter any character to exit";
            return 0;

            } // end main

            Check it im fancy it is used to solve the area of a sq minus the area of a circle given the radius. Not sure if he wants it to show the other area's or not so i have those nullified for now so i can easily put them back in

            Comment


              #7
              Sounds like you'll be doing a lot of development for this course, are you using an IDE (NetBeans, Eclipse, etc) or are you using emacs? notepad? vi?

              Comment


                #8
                using some program called jgrasp its alright, points out errors and what line said error occurs on and the such. Nice feature about it is it changes colors of different things like the reserved words like "if, else, cin, cout" all show up in purple and the stuff after // is all in orange I like it enough. This class is stupid simple though with little to no problems we havent even started if statements in class i just used google and with this lab am 2 weeks ahead of the class.

                Comment


                  #9
                  Originally posted by Pumpkin_Jack View Post
                  Can someone explain to me the function of the char command?

                  And what this line does exactly it is in one of my professors templates and examples and the guys office hours are during my football lifts.

                  char dummy = ‘ ’;

                  the rest of the code i get but i cant figure out what this line is... Also is there a way to input pi into C++ I have #include <math.h> already at top.
                  That creates a character variable, that is initialized to a space.

                  The math header file does not define the constant pi. Do that yourself like so:

                  const float PI = 3.14;

                  Comment


                    #10
                    Alright i got another lab I'm working on and this one has me stuck.

                    The first part is draw a triangle using @ signs, so if user inserts 4 it goes....
                    @
                    @@
                    @@@
                    @@@@
                    @@@
                    @@
                    @

                    What im trying to do is take a number x and when it is less than the inputted number add one when it reaches it use another loop statement that will x = x-1 until its at 0.
                    Out teacher says for us to use a void statement and gave us this hint.....
                    void drawLine (int lineLength, char displayChar)
                    {
                    for (int x=1; x <= lineLength; x=x+1)
                    {
                    cout << displayChar;
                    }
                    cout << endl;
                    }

                    but i that doesnt seem to want to compile unless I put a ; at the end of the void line. But then if i enter say 4 as lineLength i get....
                    @@@@
                    doesnt climb to it. Any help or if someone can please explain to me this dang void statement that would help greatly.

                    Comment


                      #11
                      You're creating a function that doesn't return anything. That's why it's return type is void.

                      Alternatively, here is a function that doubles a number and returns it.

                      int double (int x) {
                      return x * 2;
                      }

                      What's your compile error?

                      Comment


                        #12
                        Got it used a little different method than you suggesting Harpy...
                        Asked user for symbol and Length
                        for ( x=1; x <= Length; x = x+1 )
                        {
                        for (int y=1; y < x + 1; y = y+1)
                        {
                        cout << Symbol;
                        } // close for
                        cout << endl;
                        }
                        for ( x = Length; x > 0; x = x-1 )
                        {
                        for (int y = x; y > 1; y = y-1)
                        {
                        cout << Symbol;
                        } // close for
                        cout << endl;
                        }

                        Comment


                          #13
                          This is a neat task. Reminds me of my freshman compsci year.


                          Here is how mine looks. Remember, less code == better.

                          If you have any questions, just ask.


                          #include <iostream>

                          using namespace std;

                          void draw_triangle(char c, int h);
                          void draw_line(char c, int len);


                          int main() {
                          /*
                          * Draw a series of triangles. Notice that we draw triangles of 0 and 1
                          * length. In computer science, there is a rule called the 0, 1, infinity
                          * rule. This rule essentially means you should test your program / code
                          * with 0 inputs, 1 input and "a lot" of input.
                          */
                          for (int x = 0; x < 5; x++) {
                          draw_triangle('*', x);
                          cout << endl;
                          }
                          return 0;
                          }


                          /*
                          * Draw a "horizontal" triangle that is `h` tall.
                          */
                          void draw_triangle(char c, int h)
                          {
                          /* Sanity check */
                          if (h < 1) return;

                          int x;
                          /*
                          * Draw the first half of the triangle. This includes the
                          * tallest segment.
                          */
                          for (x = 0; x < h; x++) {
                          draw_line(c, x+1);
                          }

                          /*
                          * Handle the last half.
                          */
                          for (x = h - 1; x; x--) {
                          draw_line(c, x);
                          }
                          }


                          void draw_line(char c, int len)
                          {
                          int x;
                          for (x = 0; x < len; x++) {
                          cout << c;
                          }
                          cout << endl;
                          }

                          Comment


                            #14
                            draw_line function we have not been taught yet
                            also x++ idk what that is.

                            Lastly mine is spaced like it is bc my prof wants it that way so its easy to read

                            Comment


                              #15
                              draw_line isn't taught. It's implemented . This is what you referred to as a `void statement`. Technically it was a function definition.

                              That code is hard to read because it lost it's formatting.

                              ++ is the increment operator. It's essentially adding 1 to a variable:

                              x++

                              You're on your way. Keep it up.

                              Comment

                              Cain's Lair Forums Statistics

                              Collapse

                              Topics: 26,182   Posts: 269,814   Members: 6,176   Active Members: 3
                              Welcome to our newest member, 28Farrell8.

                              Today's Birthdays

                              Collapse

                              There are no members with birthdays today.

                              Top Active Users

                              Collapse

                              There are no top active users.
                              widgetinstance 184 (More Posts) skipped due to lack of content & hide_module_if_empty option.
                              Working...
                              X