Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 11-29-2004, 07:18 AM   #1 (permalink)
verdell32
Registered User
 
verdell32's Avatar
 
Join Date: Oct 2004
Posts: 35
verdell32 is on a distinguished road
Quad project suggestion

I just had a question before I get started with my third from the last project; I wanted to know are there any threads that could help me with this project?

CSIS 121 Introduction to Computer Science

Project QUAD



Write a program that calculates the two real roots of a quadratic equation of the form:



Ax2 + Bx + C = 0



Where:
A is the coefficient of the squared term,
B is the coefficient of the linear term and
C is the coefficient of the constant term.



The quadratic formula can be used to calculate the two real roots. It follows:
_________
Root = -b ± Ö b2 – 4ac

_____________

2a
Note: the O with the periods over it is a symbol that looks like a check mark


Some sets of data for A, B and C may cause runtime errors.

The program should deal with the fact that we cannot take the square root of a negative number and we can not divide by 0.



A first algorithm for the program:

Get coefficientA, coefficientB and coefficientC from the user

If coefficientA == 0 then

Display “Coefficient of A must not be 0. This is not a quadratic equation.”

else

Calculate the discriminant

If discriminant < 0 then

Display the message “This quadratic does not have real roots,” and end

else

Calculate Root1

Calculate Root2

Display Root1

Display Root2

endif

endif
To turn in your assignment, leave a copy of your work on your login drive(M:\) in a folder called QUAD. I need the source code file QUAD.CPP and the executable file called QUAD.EXE.
verdell32 is offline   Reply With Quote
Old 11-29-2004, 04:18 PM   #2 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Write a simple main and calculate x1 and x2. Keep it simple.

Question:
` What can you say about accuracy?
` When does it typically occur?


Try this, give me your thoughts, then we move on.
__________________
Valmont is offline   Reply With Quote
Old 12-04-2004, 07:25 AM   #3 (permalink)
verdell32
Registered User
 
verdell32's Avatar
 
Join Date: Oct 2004
Posts: 35
verdell32 is on a distinguished road
I need help

This is what I have so far; I pretty sure that if you read my above post you can see what I am doing wrong.

Code:
// Program Documentation //////////////////////////////////////////////////////
//
// Project Name  : QUAD
//
// Source File   : quad.cpp
//
// Programmed By : 
//
// Last Revision : 12/4/2004
//
// Version Number: 0.1
//
// Program Description ////////////////////////////////////////////////////////
//
// This program will calculate the two real roots of a quadratic equation
// of the form.
///////////////////////////////////////////////////////////////////////////////

// Include Files //////////////////////////////////////////////////////////////
#include <iostream.h>
#include <conio.h>
#include <iomanip.h>
#include <fstream.h>
#include <math.h>

// Function Prototypes ////////////////////////////////////////////////////////

// Program Mainline ///////////////////////////////////////////////////////////

int main( )
{
   double root1;
   double root2;
   double coefficientA = 3;
   double coefficientB = 2;
   double coefficientC = 4;

   //Enter Information
   cout << "Enter coefficient a:";
   cin >> coefficientA;

   cout << "Enter coefficient b:";
   cin >> coefficientB;

   cout << "Enter coefficient c:";
   cin >> coefficientC;

   //Caculations
   root1 = coefficientB * coefficientB - 4 - coefficientA * coefficientC;
   root2 = coefficientB * coefficientB - 4 - coefficientA * coefficientC;

   if ( 2 >= 0 ) {
      coefficientA = coefficientB;

   }else{
      if ( coeffientA == '0' ) {
         coefficientC = coefficientA * 3;

   }//endif
   }//endif
   cout << "Root 1 is: " << setw(8) << root1 << endl;
   cout << "Root 2 is: " << setw(8) << root2 << endl;

   getch();
   return 0;
}//endmn

Last edited by Valmont; 12-04-2004 at 11:05 PM.
verdell32 is offline   Reply With Quote
Old 12-04-2004, 11:05 PM   #4 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Besides the errors, I am not intersested in the if's and else's. Nevermind that. Just implement a plain formula, to get things working quickly.

I have asked you two questions (typed in italics). Try to answer them. Then we move on.
The formula's are:

Code:
-b-sqrt(b^2 - 4ac)
-------------------
        2a

-b+sqrt(b^2 - 4ac)
-------------------
        2a
__________________
Valmont is offline   Reply With Quote
Old 12-05-2004, 08:19 AM   #5 (permalink)
verdell32
Registered User
 
verdell32's Avatar
 
Join Date: Oct 2004
Posts: 35
verdell32 is on a distinguished road
response to Valmont

I new I know that accuracy takes a lot of trail and error before you get it right.
and for the second question I don't know how to exactly answer that question.

For the if/else statements it is required by my professor for this project in Borland C++ 6.

I am down to the last minutes of this project because it is due tomorrow by 12pm.

The program works but, I don't know if its giving the right answer that he is looking for?

Are you telling me to include this line of code to see if it works correctly with what I have already?
verdell32 is offline   Reply With Quote
Old 12-06-2004, 07:29 AM   #6 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
I got home too late. Sorry. You could have learned interesting things.
__________________
Valmont is offline   Reply With Quote
Old 12-06-2004, 01:39 PM   #7 (permalink)
verdell32
Registered User
 
verdell32's Avatar
 
Join Date: Oct 2004
Posts: 35
verdell32 is on a distinguished road
still two days to revise it

He gave us two extra days to make any corrections; if you get anytime to help me, I would appreciate it.

Thanks.
verdell32 is offline   Reply With Quote
Old 12-06-2004, 05:17 PM   #8 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Code:
#include <iostream>
#include <string>
#include <climits>
#include <cmath>

using std::cin;
using std::cout;
using std::endl;
using std::numeric_limits;
using std::streamsize;
using std::string;

//Optional function: depends on IDE. Remove if not needed.
void wait_for_enter();

//Additional helper. Remove if not needed.
void reset_istream();

int main(int argc, char *argv[])
{
   cout<<"Please enter a, b, c seperated by spaces. Then press <enter>."<<endl;
   cout<<"a b c = ";
   double a, b, c;
   cin>>a>>b>>c;
   
   double det = b*b-4.0*a*c;
   double x1, x2;
   
   if(a == 0)
   {
      cout<<"Coefficient a is 0. This is not a quadratic equation!"<<endl;
   }
   else if (det < 0)
   {
      cout<<"This quadratic does not have real roots."<<endl;
   }
   else
   {
      x1 = (-b + sqrt(det) ) / (2.0*a);
      x2 = (-b - sqrt(det) ) / (2.0*a);
      
      cout<<"x1= "<<x1<<endl<<"x2= "<<x2<<endl;
   }

   reset_istream();
   wait_for_enter();
   return 0;
}

//---------------------------------------------------

void reset_istream()
{
   cin.clear();
   cin.ignore(numeric_limits<streamsize>::max(), '\n');
}

//---------------------------------------------------

void wait_for_enter()
{
  cout << "press <enter> to continue...";
  // Reset failstate, just in case.
  cin.clear();
  string line;
  getline( cin, line);
}
__________________
Valmont is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Project help verdell32 Standard C, C++ 2 11-16-2004 04:13 PM
Project Management Project DavH27 All Other Coding Languages 14 08-18-2004 06:58 AM
Suggestion: Suggestion thread DavH27 Feedback 3 08-17-2004 01:27 PM
Suggestion: Project Thread DavH27 Lounge 2 08-17-2004 11:23 AM
tutorials suggestion inkedmn Feedback 2 03-19-2004 12:12 AM


All times are GMT -8. The time now is 07:13 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting