Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# Can someone check my work?

Posted on 2004-09-26
Medium Priority
271 Views
#include <stdio.h>

const capacity = 101;

char peek();
void InputCoefficients(int*mPtr, float c[]);
void OutputCoefficients(int m, const float c[]);
void Product(int m, const float f[], int n, const float g[], int
*kPtr, float h[]);
void ReduceDegree(int* mPtr, float c[]);

int main(void)
{
int m, n;
float f[capacity], g[capacity], h[capacity];
printf("input the coefficients of f\ninput the degree n of the polynomial: ");
scanf("%d", &n);
printf("Input the polynomial coefficients c[0], ..., c[n]: ");
InputCoefficients(&n, f);
getchar();
printf("input the coefficients of g\ninput the degree m of the polynomial: ");
scanf("%d", &m);
printf("Input the polynomial coefficients c[0], ..., c[m]: ");
InputCoefficients(&m, g);
getchar();
printf("\nthe sum f + g equals\n\n");
/*Sum(sum);*/
printf("\nthe product f * g equals\n\n");
Product(m, f, n, g, &m, h);
return 0;
}

char peek(){
char c = getchar();
ungetc(c, stdin);
return c; }

void ReduceDegree(int* mPtr, float c[]){
while (*mPtr >= 1 && c[*mPtr] == 0)
(*mPtr)--; }

void InputCoefficients(int*mPtr, float c[]){
*mPtr=0;
while (peek() != '\n'){
scanf("%f", &c[*mPtr]);
*mPtr += 1; }
if (*mPtr != 0)
*mPtr -= 1;
else c[0] = 0;
ReduceDegree(mPtr, c);
getchar(); }

void OutputCoefficients(int m, const float c[]){
int i;
for (i = 0; i <= m; i++)
printf("%.1f ", c[i]);
printf("\n"); }

/*void Sum(){
int i, j;
for (i=0; i<=n; i++){
*/

void Product(int m, const float f[], int n, const float g[], int
*kPtr, float h[]){
int i, j;
*kPtr = m+n;
for (i=0; i<=*kPtr; i++)
h[i]=0;
for (i=0; i<=m; i++)
for (j=0; j<=m; j++)
h[i+j] += f[i]*g[j]; }

That what's supposed to output:

input the coefficients of f
input the degree n of the polynomial: 3
Input the polynomial coefficients c[0], ..., c[n]: 1 3 3 1
input the coefficients of g
input the degree n of the polynomial: 1
Input the polynomial coefficients c[0], ..., c[n]: 1 1
the sum f + g equals
[0] = 2.000000
[1] = 4.000000
[2] = 3.000000
[3] = 1.000000
the product f * g equals
[0] = 1.000000
[1] = 4.000000
[2] = 6.000000
[3] = 4.000000
[4] = 1.000000

Thanks
0
Question by:biloonline
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 6
• 3
• 2
• +2

Author Comment

ID: 12157358
It's not even asking to input the degree for the second polynomial. Also it's missing the sum function, can someone please assist on writting that function?

Thanks
0

Author Comment

ID: 12157487
This program is due in 45 minutes and thus there 'll be no need to submit your comments afterwards.

Thanks anyway
0

LVL 84

Expert Comment

ID: 12157511
void InputCoefficients(int*mPtr, float c[]) ends with a getchar() and the call is also followed by a getchar()  Do you need them both?
0

Author Comment

ID: 12157519
Well, I didn't think so either, but the problem was in compiling. The prompt to input the degree of the first polynomial wasn't even coming up, but instead skipping directly to the prompt for the coefficients.

I added the getchar after the call, so it asked for the degree of the first polynomial. Now it's not asking for the degree of the second one, but skipping to asking about the coefficients.

I don't know why.
0

LVL 45

Expert Comment

ID: 12157557

scanf("%d", &n);

to

scanf(" %d", &n);
^
note the space
0

Author Comment

ID: 12157567
Did not do any good.
0

LVL 84

Expert Comment

ID: 12157576
You read  &n and then InputCoefficients immediately sets it to 0
the peek() != '\n' is probably reading the '\n' you typed after scanf("%d", &n);
0

Author Comment

ID: 12157592
I don't really understand how to fix that.
0

LVL 45

Expert Comment

ID: 12157599
>printf("input the coefficients of f\ninput the degree n of the polynomial: ");
^
remove this \n
0

Author Comment

ID: 12157611
That will only make the statement one line... there is no problem in that part of the program. It's the second half.
0

LVL 45

Expert Comment

ID: 12157663
InputCoefficients(&n, f);
>>     fscanf(stdin,"%*[^\n]",(char *)f);  << add this line
printf("input the coefficients of g\ninput the degree m of the polynomial: ");
0

LVL 8

Accepted Solution

ssnkumar earned 1000 total points
ID: 12157817
Hi,

I have modified your code a little bit. It is taking inputs now.
The buffering is the problem. After scanf(), there will be a '\n' character remaining in the buffer, which has to be read, if you want to make use of getchar().

Hope it helps.

-ssnkumar

Here is the corrected code:

#include <stdio.h>

const int capacity = 101;

char peek();
void InputCoefficients(int*mPtr, float c[]);
void OutputCoefficients(int m, const float c[]);
void Product(int m, const float f[], int n, const float g[], int
*kPtr, float h[]);
void ReduceDegree(int* mPtr, float c[]);

int main(void)
{
int m, n;
float f[capacity], g[capacity], h[capacity];

printf("input the coefficients of f\ninput the degree n of the polynomial: ");
scanf("%d", &n);
getchar();
printf("Input the polynomial coefficients c[0], ..., c[n]: ");
InputCoefficients(&n, f);
//       getchar();
printf("input the coefficients of g\ninput the degree m of the polynomial: ");
scanf("%d", &m);
getchar();
printf("Input the polynomial coefficients c[0], ..., c[m]: ");
InputCoefficients(&m, g);
//       getchar();
printf("\nthe sum f + g equals\n\n");
/*Sum(sum);*/
printf("\nthe product f * g equals\n\n");
Product(m, f, n, g, &m, h);
return 0;
}

char peek(){
char c = getchar();
if (c != '\n') ungetc(c, stdin);
return c; }

void ReduceDegree(int* mPtr, float c[]){
while (*mPtr >= 1 && c[*mPtr] == 0)
(*mPtr)--; }

void InputCoefficients(int*mPtr, float c[]){
*mPtr=0;
while (peek() != '\n'){
scanf("%f", &c[*mPtr]);
*mPtr += 1; }
if (*mPtr != 0)
*mPtr -= 1;
else c[0] = 0;
ReduceDegree(mPtr, c);}
//       getchar(); }

void OutputCoefficients(int m, const float c[]){
int i;
for (i = 0; i <= m; i++)
printf("%.1f ", c[i]);
printf("\n"); }

/*void Sum(){
int i, j;
for (i=0; i<=n; i++){
*/

void Product(int m, const float f[], int n, const float g[], int
*kPtr, float h[]){
int i, j;
*kPtr = m+n;
for (i=0; i<=*kPtr; i++)
h[i]=0;
for (i=0; i<=m; i++)
for (j=0; j<=m; j++)
h[i+j] += f[i]*g[j]; }
0

LVL 46

Expert Comment

ID: 12174142

Hi Sunny,

I'm certainly glad that you made the suggestion to add the fscanf() statement with the enhanced format string.  I was thinking just a short time ago that the board sees enough scanf() issues because the NewLine character remains buffered that the standard answer should be to use the correct format string, not the throw in getchar() statements to skip the NewLine.

Go forth and multiply, or in your case, "do long division".
Kent

0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were smallâ€¦
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn hoâ€¦
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
###### Suggested Courses
Course of the Month10 days, 20 hours left to enroll