Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,664 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,165 people online right now. Registration is fast and FREE... Join Now!




Basic C++

 
Reply to this topicStart new topic

Basic C++

dodj
post 7 Aug, 2008 - 05:45 AM
Post #1


New D.I.C Head

*
Joined: 29 Jul, 2008
Posts: 2

hi im just a new user of C++. my instructor gave me some machine problems to be passed two days from now. im having a hard time figuring out the problem of my program. the program is all about simple calculation using algebra. the first if statement is ok but the second up to the last..somethings wrng. the key-in coefficient of divisor is not working and it gives wrong value of the remainder. i already checked the math and its right. the code is

cpp


#include <stdio.h>

main()
{
float a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,x,b1,b2,b3,b4,b5,b6,b7,b8,b9;
int c;

printf("\n This program will determine the remainder using synthetic division \n\n");
printf("\n\n Enter the degree of your equation \n");
scanf("%d",&c);

if (c = 1)
printf("\n\n Key-in the numerical coefficients of your equation separating them with space \n");
scanf("%f %f", &a1, &a2);
printf("\n Key-in the coefficient of your divisor \n");
scanf("%f",&x);
b1=a2+(a1*x);
printf("\n\n The remainder is equal to %f! \n\n",b1);


if (c = 2)
printf("\n\n Key-in the numerical coefficients of your equation separating them with space \n");
scanf("%f %f %f", &a1, &a2, &a3);
printf("\n Key-in the coefficient of your divisor \n");
scanf("%f",&x);
b2=a3+(a2*x)+(a1*x*x);
printf("\n\n The remainder is equal to %f! \n\n",b2);


if (c = 3)
printf("\n\n Key-in the numerical coefficients of your equation separating them with space \n");
scanf("%f %f %f %f", &a1, &a2,&a3,&a4);
printf("\n Key-in the coefficient of your divisor \n");
scanf("%f",&x);
b3=a4+(a3*x)+(a2*x*x)+(a1*x*x*x);
printf("\n\n The remainder is equal to %f! \n\n",b3);


if (c = 4)
printf("\n\n Key-in the numerical coefficients of your equation separating them with space \n");
scanf("%f %f %f %f %f", &a1, &a2,&a3,&a4, &a5);
printf("\n Key-in the coefficient of your divisor \n");
scanf("%f",&x);
b4=a5+(a4*x)+(a3*x*x)+(a2*x*x*x)+(a1*x*x*x*x);
printf("\n\n The remainder is equal to %f! \n\n",b4);


if (c = 5)
printf("\n\n Key-in the numerical coefficients of your equation separating them with space \n");
scanf("%f %f %f %f %f %f", &a1, &a2,&a3,&a4, &a5, &a6);
printf("\n Key-in the coefficient of your divisor \n");
scanf("%f",&x);
b5=a6+(a5*x)+(a4*x*x)+(a3*x*x*x)+(a2*x*x*x*x)+(a1*x*x*x*x*x);
printf("\n\n The remainder is equal to %f! \n\n",b5);

if (c = 6)
printf("\n\n Key-in the numerical coefficients of your equation separating them with space \n");
scanf("%f %f %f %f %f %f %f", &a1, &a2,&a3,&a4, &a5, &a6, &a7);
printf("\n Key-in the coefficient of your divisor \n");
scanf("%f",&x);
b6=a7+(a6*x)+(a5*x*x)+(a4*x*x*x)+(a3*x*x*x*x)+(a2*x*x*x*x*x)+(a1*x*x*x*x*x*x);
printf("\n\n The remainder is equal to %f! \n\n",b6);

if (c = 7)
printf("\n\n Key-in the numerical coefficients of your equation separating them with space \n");
scanf("%f %f %f %f %f %f %f %f", &a1, &a2,&a3,&a4, &a5, &a6, &a7, &a8);
printf("\n Key-in the coefficient of your divisor \n");
scanf("%f",&x);
b7=a8+(a7*x)+(a6*x*x)+(a5*x*x*x)+(a4*x*x*x*x)+(a3*x*x*x*x*x)+(a2*x*x*x*x*x*x)+(a
1*x*x*x*x*x*x*x);
printf("\n\n The remainder is equal to %f! \n\n",b7);

if (c = 8)
printf("\n\n Key-in the numerical coefficients of your equation separating them with space \n");
scanf("%f %f %f %f %f %f %f %f %f", &a1, &a2,&a3,&a4, &a5, &a6, &a7, &a8, &a9);
printf("\n Key-in the coefficient of your divisor \n");
scanf("%f",&x);
b8=a9+(a8*x)+(a7*x*x)+(a6*x*x*x)+(a5*x*x*x*x)+(a4*x*x*x*x*x)+(a3*x*x*x*x*x*x)+(a
2*x*x*x*x*x*x*x)+(a1*x*x*x*x*x*x*x*x);
printf("\n\n The remainder is equal to %f! \n\n",b8);

if (c = 9)
printf("\n\n Key-in the numerical coefficients of your equation separating them with space \n");
scanf("%f %f %f %f %f %f %f %f %f %f", &a1, &a2,&a3,&a4, &a5, &a6, &a7, &a8, &a9, &a10);
printf("\n Key-in the coefficient of your divisor \n");
scanf("%f",&x);
b9=a10+(a9*x)+(a8*x*x)+(a7*x*x*x)+(a6*x*x*x*x)+(a5*x*x*x*x*x)+(a4*x*x*x*x*x*x)+(
a3*x*x*x*x*x*x*x)+(a2*x*x*x*x*x*x*x*x)+(a1*x*x*x*x*x*x*x*x*x);
printf("\n\n The remainder is equal to %f! \n\n",b9);

}

User is offlineProfile CardPM

Go to the top of the page

KYA
post 7 Aug, 2008 - 06:23 AM
Post #2


#include <nerd.h>

Group Icon
Joined: 14 Sep, 2007
Posts: 4,211



Thanked 50 times

Dream Kudos: 1150
My Contributions


Have you tried brackets around each if? Also, you may want to use this structure:

cpp

if statement
{
}
else if statement
{
}
else if statement
{
}
else
{
}
//etc...
User is offlineProfile CardPM

Go to the top of the page

Xarver
post 7 Aug, 2008 - 06:56 AM
Post #3


D.I.C Head

**
Joined: 6 Aug, 2008
Posts: 60



Thanked 2 times
My Contributions


CODE
#include <stdio.h>

main()
{
float a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,x,b1,b2,b3,b4,b5,b6,b7,b8,b9;
int c;

printf("\n This program will determine the remainder using synthetic division \n\n");
printf("\n\n Enter the degree of your equation \n");
scanf("%d",&c);

if (c = 1)
printf("\n\n Key-in the numerical coefficients of your equation separating them with space \n");
scanf("%f %f", &a1, &a2);
printf("\n Key-in the coefficient of your divisor \n");
scanf("%f",&x);
b1=a2+(a1*x);
printf("\n\n The remainder is equal to %f! \n\n",b1);


if (c = 2)
printf("\n\n Key-in the numerical coefficients of your equation separating them with space \n");
scanf("%f %f %f", &a1, &a2, &a3);
printf("\n Key-in the coefficient of your divisor \n");
scanf("%f",&x);
b2=a3+(a2*x)+(a1*x*x);
printf("\n\n The remainder is equal to %f! \n\n",b2);


if (c = 3)
printf("\n\n Key-in the numerical coefficients of your equation separating them with space \n");
scanf("%f %f %f %f", &a1, &a2,&a3,&a4);
printf("\n Key-in the coefficient of your divisor \n");
scanf("%f",&x);
b3=a4+(a3*x)+(a2*x*x)+(a1*x*x*x);
printf("\n\n The remainder is equal to %f! \n\n",b3);


if (c = 4)
printf("\n\n Key-in the numerical coefficients of your equation separating them with space \n");
scanf("%f %f %f %f %f", &a1, &a2,&a3,&a4, &a5);
printf("\n Key-in the coefficient of your divisor \n");
scanf("%f",&x);
b4=a5+(a4*x)+(a3*x*x)+(a2*x*x*x)+(a1*x*x*x*x);
printf("\n\n The remainder is equal to %f! \n\n",b4);


if (c = 5)
printf("\n\n Key-in the numerical coefficients of your equation separating them with space \n");
scanf("%f %f %f %f %f %f", &a1, &a2,&a3,&a4, &a5, &a6);
printf("\n Key-in the coefficient of your divisor \n");
scanf("%f",&x);
b5=a6+(a5*x)+(a4*x*x)+(a3*x*x*x)+(a2*x*x*x*x)+(a1*x*x*x*x*x);
printf("\n\n The remainder is equal to %f! \n\n",b5);

if (c = 6)
printf("\n\n Key-in the numerical coefficients of your equation separating them with space \n");
scanf("%f %f %f %f %f %f %f", &a1, &a2,&a3,&a4, &a5, &a6, &a7);
printf("\n Key-in the coefficient of your divisor \n");
scanf("%f",&x);
b6=a7+(a6*x)+(a5*x*x)+(a4*x*x*x)+(a3*x*x*x*x)+(a2*x*x*x*x*x)+(a1*x*x*x*x*x*x);
printf("\n\n The remainder is equal to %f! \n\n",b6);

if (c = 7)
printf("\n\n Key-in the numerical coefficients of your equation separating them with space \n");
scanf("%f %f %f %f %f %f %f %f", &a1, &a2,&a3,&a4, &a5, &a6, &a7, &a8);
printf("\n Key-in the coefficient of your divisor \n");
scanf("%f",&x);
b7=a8+(a7*x)+(a6*x*x)+(a5*x*x*x)+(a4*x*x*x*x)+(a3*x*x*x*x*x)+(a2*x*x*x*x*x*x)+(a
1*x*x*x*x*x*x*x);
printf("\n\n The remainder is equal to %f! \n\n",b7);

if (c = 8)
printf("\n\n Key-in the numerical coefficients of your equation separating them with space \n");
scanf("%f %f %f %f %f %f %f %f %f", &a1, &a2,&a3,&a4, &a5, &a6, &a7, &a8, &a9);
printf("\n Key-in the coefficient of your divisor \n");
scanf("%f",&x);
b8=a9+(a8*x)+(a7*x*x)+(a6*x*x*x)+(a5*x*x*x*x)+(a4*x*x*x*x*x)+(a3*x*x*x*x*x*x)+(a
2*x*x*x*x*x*x*x)+(a1*x*x*x*x*x*x*x*x);
printf("\n\n The remainder is equal to %f! \n\n",b8);

if (c = 9)
printf("\n\n Key-in the numerical coefficients of your equation separating them with space \n");
scanf("%f %f %f %f %f %f %f %f %f %f", &a1, &a2,&a3,&a4, &a5, &a6, &a7, &a8, &a9, &a10);
printf("\n Key-in the coefficient of your divisor \n");
scanf("%f",&x);
b9=a10+(a9*x)+(a8*x*x)+(a7*x*x*x)+(a6*x*x*x*x)+(a5*x*x*x*x*x)+(a4*x*x*x*x*x*x)+(
a3*x*x*x*x*x*x*x)+(a2*x*x*x*x*x*x*x*x)+(a1*x*x*x*x*x*x*x*x*x);
printf("\n\n The remainder is equal to %f! \n\n",b9);

}

First of all in C++ i think you should be using the cout << and cin >> statements instead of printf and scanf.
Also,
QUOTE
#include <stdio.h>

should be
CODE
#include <cstdio>

QUOTE
main()

should be
cpp
int main()

That's C style what he was doing, right?

And KYA is right.
You have to use the brackets for the conditional statements.
Unless you have only one statement in them.
I recommend you put brackets on every single conditional statement.
cpp
if statement  
{
}
else if statement
{
}
else if statement
{
}
else
{
}
//etc...


This post has been edited by Xarver: 7 Aug, 2008 - 07:02 AM
User is offlineProfile CardPM

Go to the top of the page

KYA
post 7 Aug, 2008 - 07:03 AM
Post #4


#include <nerd.h>

Group Icon
Joined: 14 Sep, 2007
Posts: 4,211



Thanked 50 times

Dream Kudos: 1150
My Contributions


Looks like he wants to use pure C, but who knows. People seem to be mixing them a lot nowadays here.
User is offlineProfile CardPM

Go to the top of the page

NickDMax
post 7 Aug, 2008 - 09:27 AM
Post #5


2B||!2B

Group Icon
Joined: 18 Feb, 2007
Posts: 2,858



Thanked 47 times

Dream Kudos: 550
My Contributions


Just an odd note. I see that you are calculating polynomials using a syntax similar to:
P = a0 + a1 * x + a2 * x * x + a3 * x * x * x
that expression uses 3 additions and 6 multiplications. Not only that but this recalculates values (for example x * x exits twice in the expression). One way you could make this a little better is to capture the powers of x:

x2 = x * x
P = a0 + a1 * x + a2 * x2 + a3 * x2 * x

Here we have reduced the multiplications to 5.

There is another neat little trick:

P_quick = (((a3 * x) + a2) * x + a1) * x + a0

here we only have 3 multiplications and 3 additions. If you work out the algebra you will find:

P_quick = ((a3 * x) + a2) * x * x + a1 * x + a0
= a3 * x * x * x + a2 * x * x + a1 * x + a0
so P_quick == P


so in the case of your b9 you are using 45 multiplications!!! We can reduce that to 9.

b9 = (((((((((a1 * x + a2) * x + a3) * x + a4) * x + a5) * x + a6) * x + a7) * x + a8) * x + a9) * x + a10

This has only 9 multiplications!!! Way less than the 45 you were using in the original version.

Another note is that you should use double and not float (especially if you are getting polynomials of order 9). There is nothing WRONG with float, it is just a small data type, and most modern CPU's actually work more efficiently with double than float.

(finally remembered the NAME. Horner's algorithm. -- this also reduces error in the calculation).

This post has been edited by NickDMax: 7 Aug, 2008 - 09:57 AM
User is offlineProfile CardPM

Go to the top of the page

Bench
post 7 Aug, 2008 - 09:54 AM
Post #6


D.I.C Addict

Group Icon
Joined: 20 Aug, 2007
Posts: 602



Thanked 10 times

Dream Kudos: 150

Expert In: C/C++

My Contributions


Careful with the = operator, which is the assignment operator. At the moment, every single one of your if statements will yield true, since you're assigning a value to your c variable

If you wish to compare c with a value, then you need the == operator, which is for equality comparison.


If you have trouble remembering to use the right operator, then a simple way to cause the compiler to tell you when you've gone wrong is to swap the constant or literal value with the variable, so that your code would read like this
if ( 9 == c )

This way, if you made a mistake, such as if ( 9 = c ) , the compiler would complain about attempting to assign to a constant value
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 05:46AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month